From 793416ba210f388fe0b8ceac37a4090384cf9227 Mon Sep 17 00:00:00 2001 From: Josh Dirkx Date: Thu, 28 Aug 2014 17:15:24 -0500 Subject: [PATCH] completed algorithm problems and adjusted arrays.rb. all files working via rspec --- array_problems/arrays.rb | 37 +++++++++++++++++++++++++++++++++ set1/set1.rb | 45 ++++++++++++++++++++++++++++++++++++++++ set1/spec/set1_spec.rb | 20 +++++++++--------- 3 files changed, 92 insertions(+), 10 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..5e801cb 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,11 +3,48 @@ module ArrayUtil def self.max(array) + + # total_count = array.length + max_value = array[0] + counter = 0 + + # until counter == total_count do + array.each_index do |i| + if array[i] > max_value + max_value = array[i] + end + # counter += 1 + end + # end + + return max_value end def self.middle_element(array) + #[3,2,1,4,5] = 1 + #[3,2,1,2,4,5] = 1.5 + if array.length == 0 + return nil + end + + # array_length = array.length + array_middle = array.length / 2 #middle for odd numbered arrays + + if array.length.even? + average_val = (array[array_middle] + array[array_middle - 1]) / 2.0 + return average_val + else + return array[array_middle] + end end def self.sum_arrays(array1, array2) + array3 = [ ] + array1.each_index do |i| + # array2.each_index do |i| + array3 << array1[i] + array2[i] + # end + end + return array3 end end diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..3a10cce 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,10 +1,55 @@ module Set1 def self.swap_small(array) + min_value = array[0] + min_value_index = 0 + + array.each_index do |i| + if array[i] < min_value + min_value = array[i] + min_value_index = i + end + array[min_value_index] = array[0] + array[0] = min_value + end end def self.find_sum_2(array, sum = 0) + if array.length == 0 + return false + end + + array.each_index do |i| + array.each_index do |x| + if array[i] + array[x] == 0 + return true + elsif array[i] == 0 || array[x] == 0 + return true + # else + # return false + end + end + end + false end def self.find_sum_3(array) + if array.length == 0 + return false + end + + array.each_index do |i| + array.each_index do |x| + array.each_index do |r| + if array[i] + array[x] + array[r] == 0 + return true + elsif array[i] == 0 || array[x] == 0 || array[r] == 0 + return true + # else + # return false + end + end + end + end + false end end diff --git a/set1/spec/set1_spec.rb b/set1/spec/set1_spec.rb index 4d90f4e..1284488 100644 --- a/set1/spec/set1_spec.rb +++ b/set1/spec/set1_spec.rb @@ -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