From c6dce5d35e3fde7af0d071acd48ae0372749c516 Mon Sep 17 00:00:00 2001 From: Penkar Date: Thu, 28 Aug 2014 14:50:50 -0500 Subject: [PATCH 1/5] Array methods added. --- algorithms.rb | 26 ++++++++++++++++++++++++++ array_problems/arrays.rb | 28 +++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 algorithms.rb diff --git a/algorithms.rb b/algorithms.rb new file mode 100644 index 0000000..0058388 --- /dev/null +++ b/algorithms.rb @@ -0,0 +1,26 @@ +def max(array) + return "No array provided." if array.length == 0 + highest = array[0] + array[1..-1].each {|x| highest = x if highest < x } + return highest +end + + +def middle_element(array) + return "No array provided." if array.length == 0 + length = array.sort!.length + p array + p length + if length % 2 == 0 + return (array[length/2-1] + array[length/2])/2.0 + else + return array[length/2] + end +end + +def sum_arrays(array1, array2) + arr = [] + l = array1.length + 0.upto(l-1) {|x| arr[x] = array1[x]+array2[x]} + return arr +end \ No newline at end of file diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..850f52e 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -2,12 +2,30 @@ require 'pry-byebug' module ArrayUtil - def self.max(array) - end +def max(array) + return "No array provided." if array.length == 0 + highest = array[0] + array[1..-1].each {|x| highest = x if highest < x } + return highest +end - def self.middle_element(array) - end - def self.sum_arrays(array1, array2) +def middle_element(array) + return "No array provided." if array.length == 0 + length = array.sort!.length + p array + p length + if length % 2 == 0 + return (array[length/2-1] + array[length/2])/2.0 + else + return array[length/2] end end + +def sum_arrays(array1, array2) + arr = [] + l = array1.length + 0.upto(l-1) {|x| arr[x] = array1[x]+array2[x]} + return arr +end +end From fce31259c19c2dff7f2aa9b217d2938e1134989c Mon Sep 17 00:00:00 2001 From: Penkar Date: Thu, 28 Aug 2014 15:09:30 -0500 Subject: [PATCH 2/5] Arrays.rb passes all tests. --- array_problems/arrays.rb | 42 +++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 850f52e..305843b 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -2,30 +2,28 @@ require 'pry-byebug' module ArrayUtil -def max(array) - return "No array provided." if array.length == 0 - highest = array[0] - array[1..-1].each {|x| highest = x if highest < x } - return highest -end + def self.max(array) + return nil if array.length == 0 + highest = array[0] + array[1..-1].each {|x| highest = x if highest < x } + return highest + end -def middle_element(array) - return "No array provided." if array.length == 0 - length = array.sort!.length - p array - p length - if length % 2 == 0 - return (array[length/2-1] + array[length/2])/2.0 - else - return array[length/2] + def self.middle_element(array) + return nil if array.length == 0 + length = array.sort!.length + if length % 2 == 0 + return (array[length/2-1] + array[length/2])/2.0 + else + return array[length/2] + end end -end -def sum_arrays(array1, array2) - arr = [] - l = array1.length - 0.upto(l-1) {|x| arr[x] = array1[x]+array2[x]} - return arr -end + def self.sum_arrays(array1, array2) + arr = [] + l = array1.length + 0.upto(l-1) {|x| arr[x] = array1[x]+array2[x]} + return arr + end end From 102cf647c68cff28183539804f5ee5d4445f0baf Mon Sep 17 00:00:00 2001 From: Penkar Date: Thu, 28 Aug 2014 16:47:03 -0500 Subject: [PATCH 3/5] all tests pass. Huzzah! And I was on such a roll that I forgot to commit prior to completion. Boo... --- set1/set1.rb | 28 ++++++++++++++++++++++++++++ set1/spec/set1_spec.rb | 20 ++++++++++---------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..04762ba 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,10 +1,38 @@ module Set1 def self.swap_small(array) + small = array[0] + ar2 = [] + array[1..-1].each do |x| + if x < small + ar2.push(small) + small = x + else + ar2.push(x) + end + end + ar2.unshift(small) + ar2 end def self.find_sum_2(array, sum = 0) + return false if array.empty? + array.each do |ar1| + array.each do |ar2| + return true if (ar1 + ar2) == 0 + end + end + return false end def self.find_sum_3(array) + return false if array.empty? + array.each do |ar1| + array.each do |ar2| + array.each do |ar3| + return true if (ar1+ar2+ar3)==0 + end + end + end + return 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 From 28aefbb64dc4dac4ecd19cdffec9df3a4c5011e7 Mon Sep 17 00:00:00 2001 From: Penkar Date: Thu, 28 Aug 2014 17:02:21 -0500 Subject: [PATCH 4/5] Finished Project --- set1/set1.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/set1/set1.rb b/set1/set1.rb index 04762ba..c3f1fbd 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,7 +1,7 @@ module Set1 def self.swap_small(array) small = array[0] - ar2 = [] + ar2 = [0] array[1..-1].each do |x| if x < small ar2.push(small) @@ -10,7 +10,7 @@ def self.swap_small(array) ar2.push(x) end end - ar2.unshift(small) + ar2[0]=(small) ar2 end From 49861157765a6a2d1cec71ae424a55f3f5719acd Mon Sep 17 00:00:00 2001 From: Penkar Date: Fri, 29 Aug 2014 09:16:39 -0500 Subject: [PATCH 5/5] Slightly updated, stillworks. --- array_problems/arrays.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 305843b..83aca6f 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,7 +3,7 @@ module ArrayUtil def self.max(array) - return nil if array.length == 0 + return nil if array.empty? highest = array[0] array[1..-1].each {|x| highest = x if highest < x } return highest @@ -11,7 +11,7 @@ def self.max(array) def self.middle_element(array) - return nil if array.length == 0 + return nil if array.empty? length = array.sort!.length if length % 2 == 0 return (array[length/2-1] + array[length/2])/2.0 @@ -22,8 +22,7 @@ def self.middle_element(array) def self.sum_arrays(array1, array2) arr = [] - l = array1.length - 0.upto(l-1) {|x| arr[x] = array1[x]+array2[x]} + 0.upto(array1.length-1) {|x| arr[x] = array1[x]+array2[x]} return arr end end