From 12cb85706c272813b0458ee43224883c5b157787 Mon Sep 17 00:00:00 2001 From: Nick Damiano Date: Thu, 28 Aug 2014 15:32:36 -0500 Subject: [PATCH 1/6] solved first problem --- array_problems/arrays.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..624d1f0 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,9 +3,23 @@ module ArrayUtil def self.max(array) + biggest= array[0] + array.each_with_index do |x, i| + if array[(i+1)] > biggest + biggest = x + binding.pry + end + end + biggest end def self.middle_element(array) + if array.length % 2 == 0 + #array is even length + array.each_with_index do |x,i| + + end + end def self.sum_arrays(array1, array2) From 6f89ff7bf1dba226f511f7d13141b22ac9808f44 Mon Sep 17 00:00:00 2001 From: Nick Damiano Date: Thu, 28 Aug 2014 18:47:54 -0500 Subject: [PATCH 2/6] fucking butchered the hell out of it but the first problem is done --- set1/set1.rb | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..c4fc091 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,10 +1,35 @@ module Set1 - def self.swap_small(array) + def self.swap_small(array) #darrell & Jimmy + if array.count == 1 + return array + end + flag = false + smallest_number = array[0] + smallest_index = 0 + array.each_with_index do |value, index| + if value < smallest_number + smallest_number = value + smallest_index = index + flag = true + end + end + if !flag + return array + end + array.delete_at(smallest_index) + temp = array[0] + array.delete(array[0]) + array.unshift(smallest_number) + array.insert(smallest_index, temp) + array end def self.find_sum_2(array, sum = 0) + end def self.find_sum_3(array) end end + + From 65b2bfb4efddb97e14cd60e6a4bdfec98eda7a53 Mon Sep 17 00:00:00 2001 From: Nick Damiano Date: Thu, 28 Aug 2014 22:58:45 -0500 Subject: [PATCH 3/6] added set1 with problem 2 solved and specs open on problem 2 --- set1/set1.rb | 46 ++++++++++++++++++++++++++++++++++++++---- set1/spec/set1_spec.rb | 20 +++++++++--------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index c4fc091..bb13241 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -3,17 +3,15 @@ def self.swap_small(array) #darrell & Jimmy if array.count == 1 return array end - flag = false smallest_number = array[0] smallest_index = 0 array.each_with_index do |value, index| if value < smallest_number smallest_number = value smallest_index = index - flag = true end end - if !flag + if smallest_number == array[0] return array end array.delete_at(smallest_index) @@ -25,10 +23,50 @@ def self.swap_small(array) #darrell & Jimmy end def self.find_sum_2(array, sum = 0) - + #Given an array of numbers, find out if there are two numbers in + #the array that add up to 0 (just return true or false). You can use + #the same number multiple times. + if array.count == 0 + false + elsif array.include?(0) + true + elsif array[0] == 0 + true + else + flag = false + array.each do |x| + array.map do |y| + if x + y == sum + flag = true + end + end + end + flag + end end def self.find_sum_3(array) + if array.count == 0 + false + elsif array.include?(0) + true + elsif array[0] == 0 + true + else + flag = false + array.each do |x| + array.map do |y| + array.map do |z| + sum = x + y + z + if sum == 0 + flag = true + end + end + end + flag + end + end + 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 From a3c568477186d5c6ad4df0d2773c00d49ed680ae Mon Sep 17 00:00:00 2001 From: Nick Damiano Date: Thu, 28 Aug 2014 23:16:22 -0500 Subject: [PATCH 4/6] added code for the third one with three nested loops but it returns the same array for some reason i can't figure out. --- set1/set1.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index bb13241..0ad6b5f 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -35,7 +35,7 @@ def self.find_sum_2(array, sum = 0) else flag = false array.each do |x| - array.map do |y| + array.each do |y| if x + y == sum flag = true end @@ -55,19 +55,18 @@ def self.find_sum_3(array) else flag = false array.each do |x| - array.map do |y| - array.map do |z| + array.each do |y| + array.each do |z| sum = x + y + z if sum == 0 flag = true + end end end + return flag end - flag end end - - end end From 0a644d7ecc5ea40ae2609ede8e2502429daf4c88 Mon Sep 17 00:00:00 2001 From: Nick Damiano Date: Thu, 28 Aug 2014 23:22:07 -0500 Subject: [PATCH 5/6] removed the word return so Patrick could see the array returning --- set1/set1.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/set1/set1.rb b/set1/set1.rb index 0ad6b5f..293ce2a 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -63,7 +63,7 @@ def self.find_sum_3(array) end end end - return flag + flag end end end From 2b642311e13e77c3e35f2e5db4cfb0551d8f6bd0 Mon Sep 17 00:00:00 2001 From: Nick Damiano Date: Fri, 29 Aug 2014 10:02:24 -0500 Subject: [PATCH 6/6] set1.rb with all three solutions and additional notes --- set1/set1.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index 293ce2a..ab58e14 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,5 +1,6 @@ module Set1 def self.swap_small(array) #darrell & Jimmy + #.each_index if array.count == 1 return array end @@ -37,7 +38,9 @@ def self.find_sum_2(array, sum = 0) array.each do |x| array.each do |y| if x + y == sum - flag = true + flag = true #inefficient because it will continue to run + #even after the conditions has been satisified until + #it hits the end end end end @@ -63,10 +66,12 @@ def self.find_sum_3(array) end end end - flag end + flag end end end - +#WHILE WORKING THROUGH THESE TYPES OF PROBLEMS. Think about how +#we are thinking about them. Examine our own thought process +#because we will be evaluated on that during interviews.