Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arrays #56

Open
wants to merge 8 commits into
base: arrays
Choose a base branch
from
24 changes: 24 additions & 0 deletions array_problems/arrays.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,35 @@

module ArrayUtil
def self.max(array)
return nil if array.count == 0
max = array[0]
array.each_index do |index|
if array[index] > max
max = array[index]
end
end
max
end

def self.middle_element(array)
return nil if array.count == 0
return array[0] if array.count == 1
length = array.count
if length % 2 == 0
first_middle = (length / 2).to_i - 1
second_middle = first_middle + 1
(array[first_middle] + array[second_middle]) / 2.0
else
array[length / 2]
end
end

def self.sum_arrays(array1, array2)
return array1 if array1.count == 0
sum = []
array1.each_index do |index|
sum.push(array1[index] + array2[index])
end
sum
end
end
33 changes: 33 additions & 0 deletions set1/set1.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,43 @@
module Set1
# Big O notation: O(n)
def self.swap_small(array)
return array if array.length == 1
array.each_index do |index|
if array[index] < array[0]
temp = array[0]
array[0] = array[index]
array[index] = temp
end
end
end

# Big O notation: O(n^2)
def self.find_sum_2(array, sum = 0)
return false if array.length == 0
return true if (array.length == 1) && (array[0] == 0)

array.each_index do |index|
return true if array[index] == 0
array.each_index do |second_index|
return true if array[index] + array[second_index] == 0
end
end
false
end

# Big O notation: O(n^3)
def self.find_sum_3(array)
return false if array.length == 0
return true if (array.length == 1) && (array[0] == 0)

array.each_index do |index|
return true if array[index] == 0
array.each_index do |second_index|
array.each_index do |third_index|
return true if array[index] + array[second_index] + array[third_index] == 0
end
end
end
false
end
end
20 changes: 10 additions & 10 deletions set1/spec/set1_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,48 @@
end

describe ".find_sum_2" do
xit "should return false for an empty array" do
it "should return false for an empty array" do
expect(Set1.find_sum_2([])).to eq(false)
end

xit "should return true for an array with just the number 0" do
it "should return true for an array with just the number 0" do
expect(Set1.find_sum_2([0])).to eq(true)
end

xit "should return true for an array with the number 0 in it" do
it "should return true for an array with the number 0 in it" do
expect(Set1.find_sum_2([5, 2, 0, -100])).to eq(true)
end

xit "should return true if a number and it's negative are in the arrray" do
it "should return true if a number and it's negative are in the arrray" do
expect(Set1.find_sum_2([5, 20, -5, 100])).to eq(true)
expect(Set1.find_sum_2([5, 20, -3, 100, -20, 2])).to eq(true)
end

xit "should return false if none of the numbers add to 0" do
it "should return false if none of the numbers add to 0" do
expect(Set1.find_sum_2([5, 6, 7, 8, -1, -2, -3, -4])).to eq(false)
end
end

describe ".find_sum_3" do
xit "should return false for an empty array" do
it "should return false for an empty array" do
expect(Set1.find_sum_3([])).to eq(false)
end

xit "should return true for an array with just the number 0" do
it "should return true for an array with just the number 0" do
expect(Set1.find_sum_3([0])).to eq(true)
end

xit "should return true for an array with the number 0 in it" do
it "should return true for an array with the number 0 in it" do
expect(Set1.find_sum_3([5, 2, 0, -100])).to eq(true)
end

xit "should return true if 3 numbers in the array add to 0" do
it "should return true if 3 numbers in the array add to 0" do
expect(Set1.find_sum_3([10, 2, 100, -200, -102, 5])).to eq(true)
expect(Set1.find_sum_3([10, -51, 100, -201, 102, 5])).to eq(true)
expect(Set1.find_sum_3([10, 51, 100, -201, -102, 5])).to eq(true) # 51, 51, -102
end

xit "should return false if no 3 numbers in the array add to 0" do
it "should return false if no 3 numbers in the array add to 0" do
expect(Set1.find_sum_3([10, 51, 100, 201, 102, 5])).to eq(false)
end
end
Expand Down