From 61148248e94c41b479e93ee23297bd056c74c96f Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 14:57:04 -0500 Subject: [PATCH 1/8] Added max method to ArrayUtil --- array_problems/arrays.rb | 9 +++++++++ array_problems/spec/arrays_spec.rb | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..66c4ea9 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,6 +3,15 @@ module ArrayUtil def self.max(array) + if array.count > 0 + max = array[0] + array.each_index do |index| + if array[index] > max + max = array[index] + end + end + max + end end def self.middle_element(array) diff --git a/array_problems/spec/arrays_spec.rb b/array_problems/spec/arrays_spec.rb index ab975cc..bd02687 100644 --- a/array_problems/spec/arrays_spec.rb +++ b/array_problems/spec/arrays_spec.rb @@ -62,15 +62,15 @@ end describe ".sum_arrays" do - it "should return an empty array if the inputs are empty" do + xit "should return an empty array if the inputs are empty" do expect(ArrayUtil.sum_arrays([], [])).to eq([]) end - it "should return an array with the sum of two arrays with arrays size 1" do + xit "should return an array with the sum of two arrays with arrays size 1" do expect(ArrayUtil.sum_arrays([5], [6])).to eq([11]) end - it "should return an array with the sum of two arrays with larger arrays" do + xit "should return an array with the sum of two arrays with larger arrays" do expect(ArrayUtil.sum_arrays([5, 6, 7, 8], [6, 9, 12, 15])).to eq([11, 15, 19, 23]) end end From 6351182ac62dbc095d23c9a05fa58b274987cbf4 Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 15:11:32 -0500 Subject: [PATCH 2/8] Implemented middle_element class method to find the middle element of an array --- array_problems/arrays.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 66c4ea9..17d315e 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -15,6 +15,18 @@ def self.max(array) end def self.middle_element(array) + if array.count == 1 + array[0] + elsif 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 end def self.sum_arrays(array1, array2) From bf995240864904099ac9e6d4411dd82a21e47ec5 Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 15:19:35 -0500 Subject: [PATCH 3/8] finished sum_arrays method which takes the sum of two arrays and returns one array --- array_problems/arrays.rb | 9 +++++++++ array_problems/spec/arrays_spec.rb | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 17d315e..1770733 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -30,5 +30,14 @@ def self.middle_element(array) end def self.sum_arrays(array1, array2) + if array1.count == 0 + array1 + else + sum = [] + array1.each_index do |index| + sum.push(array1[index] + array2[index]) + end + sum + end end end diff --git a/array_problems/spec/arrays_spec.rb b/array_problems/spec/arrays_spec.rb index bd02687..ab975cc 100644 --- a/array_problems/spec/arrays_spec.rb +++ b/array_problems/spec/arrays_spec.rb @@ -62,15 +62,15 @@ end describe ".sum_arrays" do - xit "should return an empty array if the inputs are empty" do + it "should return an empty array if the inputs are empty" do expect(ArrayUtil.sum_arrays([], [])).to eq([]) end - xit "should return an array with the sum of two arrays with arrays size 1" do + it "should return an array with the sum of two arrays with arrays size 1" do expect(ArrayUtil.sum_arrays([5], [6])).to eq([11]) end - xit "should return an array with the sum of two arrays with larger arrays" do + it "should return an array with the sum of two arrays with larger arrays" do expect(ArrayUtil.sum_arrays([5, 6, 7, 8], [6, 9, 12, 15])).to eq([11, 15, 19, 23]) end end From 679ee78d93b0228b734615dbf286548b942974eb Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 15:25:55 -0500 Subject: [PATCH 4/8] Refactored code to use one-line if statements --- array_problems/arrays.rb | 46 +++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 1770733..e83a06b 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,41 +3,35 @@ module ArrayUtil def self.max(array) - if array.count > 0 - max = array[0] - array.each_index do |index| - if array[index] > max - max = array[index] - end + return nil if array.count == 0 + max = array[0] + array.each_index do |index| + if array[index] > max + max = array[index] end - max end + max end def self.middle_element(array) - if array.count == 1 - array[0] - elsif 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 + 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) - if array1.count == 0 - array1 - else - sum = [] - array1.each_index do |index| - sum.push(array1[index] + array2[index]) - end - sum + return array1 if array1.count == 0 + sum = [] + array1.each_index do |index| + sum.push(array1[index] + array2[index]) end + sum end end From c4845e47d7c684264a2a3b1b091aba163cd188b6 Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 16:50:51 -0500 Subject: [PATCH 5/8] Implemented swap_small method in Set1 module, tests are passing --- set1/set1.rb | 9 +++++++++ set1/spec/set1_spec.rb | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..29458b4 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,5 +1,14 @@ 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 def self.find_sum_2(array, sum = 0) diff --git a/set1/spec/set1_spec.rb b/set1/spec/set1_spec.rb index 4d90f4e..46a19a1 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 From 8948d4add1c4198e4e88cde76367219637484b27 Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 17:07:34 -0500 Subject: [PATCH 6/8] Finished find_sum_2 method, but it is currently at O(n^2) speed. --- set1/set1.rb | 12 ++++++++++++ set1/spec/set1_spec.rb | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/set1/set1.rb b/set1/set1.rb index 29458b4..105acb4 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -12,6 +12,18 @@ def self.swap_small(array) end def self.find_sum_2(array, sum = 0) + return false if array.length == 0 + return true if (array.length == 1) && (array[0] == 0) + less_than_zero = [] + greater_than_zero = [] + + 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 def self.find_sum_3(array) diff --git a/set1/spec/set1_spec.rb b/set1/spec/set1_spec.rb index 46a19a1..995e2df 100644 --- a/set1/spec/set1_spec.rb +++ b/set1/spec/set1_spec.rb @@ -41,7 +41,7 @@ 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 From 660be9a46919bac611333b79c57589b19c1b211d Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 17:10:23 -0500 Subject: [PATCH 7/8] Finished find_sum_3 method with Big O notation of O(n^3) --- set1/set1.rb | 14 ++++++++++++++ set1/spec/set1_spec.rb | 10 +++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index 105acb4..0b69485 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -11,6 +11,7 @@ def self.swap_small(array) 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) @@ -26,6 +27,19 @@ def self.find_sum_2(array, sum = 0) 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 diff --git a/set1/spec/set1_spec.rb b/set1/spec/set1_spec.rb index 995e2df..1284488 100644 --- a/set1/spec/set1_spec.rb +++ b/set1/spec/set1_spec.rb @@ -47,25 +47,25 @@ 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 From d5c04a281458b3695863fd8955f2de539c9be3c5 Mon Sep 17 00:00:00 2001 From: Yusef Ouda Date: Thu, 28 Aug 2014 17:32:19 -0500 Subject: [PATCH 8/8] Removed unnecessary variables --- set1/set1.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index 0b69485..fcd13ca 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -15,8 +15,6 @@ def self.swap_small(array) def self.find_sum_2(array, sum = 0) return false if array.length == 0 return true if (array.length == 1) && (array[0] == 0) - less_than_zero = [] - greater_than_zero = [] array.each_index do |index| return true if array[index] == 0