diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..52b6073 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,11 +3,32 @@ module ArrayUtil def self.max(array) + # if array.empty? dont' need + # return nil + # else + max = array.first + array.each do |x| + if x >= max + max = x + end + end + end + return max end def self.middle_element(array) + return nil if array.empty? + mid = array.length / 2 + if array.length.even? + return (array[mid] + array[mid - 1]) / 2.0 + else + return array[mid] + end + end end def self.sum_arrays(array1, array2) + solution = [array1.each_index { |x| array1[x] + array2[x]}] + solution end end diff --git a/array_problems/spec/arrays_spec.rb b/array_problems/spec/arrays_spec.rb index ab975cc..7c1e378 100644 --- a/array_problems/spec/arrays_spec.rb +++ b/array_problems/spec/arrays_spec.rb @@ -35,6 +35,7 @@ expect(ArrayUtil.max([-1, -5, -10, -2])).to eq(-1) end end +end describe ".middle_element" do it "should return nil for an empty array" do diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..49db259 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,10 +1,71 @@ module Set1 def self.swap_small(array) + if array.length == 1 + return array + else + min_num = array.first + array.each do |x| + if x <= min_num + min_num = x + end + end + array[array.index(min_num)] = array.first + array[0] = min_num + array + end end + # def self.find_sum_2(array, sum = 0) + # if array.length == 0 + # return false + # elsif array.include?(0) + # return true + # elsif array.each { |x| array.include?(x) && array.include?(-x) } + # return true + # else # Can not get this one to work!!! + # array.each { |x| sum += x } + # if sum.equ?(0) + # return false + # else + # return true + # end + # end + # end + def self.find_sum_2(array, sum = 0) + array.each do |elem1| + array.each do |elem2| + if elem1 + elem2 == sum + return true + end + end + end + false + end + + def self.fin_sum_2(array, sum = 0) + hash = {} + array.each do |elem1| + hash[elem1] = true + end + array.each do |elem1| + if hash[sum - elem1] + return true + end + end + false end def self.find_sum_3(array) + array.each do |elem1| + array.each do |elem2| + array.each do |elem3| + if elem1 + elem2 + elem3 == 0 + return true + end + end + end + end + false end end diff --git a/set1/spec/set1_spec.rb b/set1/spec/set1_spec.rb index 4d90f4e..7b35c1a 100644 --- a/set1/spec/set1_spec.rb +++ b/set1/spec/set1_spec.rb @@ -24,19 +24,19 @@ 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 @@ -47,11 +47,11 @@ 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