From ab668c1e481cf4fd15e7a1ceb6d554c323f8b179 Mon Sep 17 00:00:00 2001 From: Darrell Abrau Date: Fri, 29 Aug 2014 04:05:16 -0500 Subject: [PATCH 1/2] completed find sum 3 --- array_problems/arrays.rb | 35 ++++++++++++++++++++++++++++++++ set1/set1.rb | 44 +++++++++++++++++++++++++++++++++++++++- set1/spec/set1_spec.rb | 20 +++++++++--------- 3 files changed, 88 insertions(+), 11 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..35ce1e2 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -2,12 +2,47 @@ require 'pry-byebug' module ArrayUtil + + + def self.max(array) + + x = array[0] + + array.each_with_index do |item, index| + if (array[index] > x) + x = array[index] + end + end + x end + + + + + + + def self.middle_element(array) + + if array.length % 2 == 1 + return array[array.length] + # else + # return [array[z], array[(z+1)]] + end end + + + + + + + + + def self.sum_arrays(array1, array2) + end end diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..37102ff 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,10 +1,52 @@ module Set1 + + #O(n) def self.swap_small(array) - end + first_element=array[0] + + smallest_value = array[0] + smallest_index = 0 + array.each_with_index do |item, index| + if array[index] < smallest_value + smallest_value = array[index] + smallest_index = index + end + end + + array.insert(0, smallest_value) + array.delete_at(1) + array.delete_at(smallest_index) + array.insert(smallest_index, first_element) + end + #O(n^2) def self.find_sum_2(array, sum = 0) + + + array.each do |x| + array.each do |a| + if x + a == 0 + return true + end + end + end + + return false + end + #O(n^3) def self.find_sum_3(array) + array.each do |a| + array.each do |b| + array.each do |c| + if a + b + c == 0 + return true + end + 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 a95f6f67dac72b7cf58e7c13bcf3be86a5487fb6 Mon Sep 17 00:00:00 2001 From: Darrell Abrau Date: Fri, 29 Aug 2014 05:29:48 -0500 Subject: [PATCH 2/2] initial commit of arrays exercise --- array_problems/arrays.rb | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 35ce1e2..728dd46 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -11,10 +11,12 @@ def self.max(array) array.each_with_index do |item, index| if (array[index] > x) - x = array[index] - end + x = array[index] + end end - x + + x + end @@ -25,12 +27,18 @@ def self.max(array) def self.middle_element(array) - - if array.length % 2 == 1 - return array[array.length] - # else - # return [array[z], array[(z+1)]] - end + + mid_index_neg_length = array.length / 2 + mid_neg_value = array[mid_index_neg_length] + + if array.empty? + nil + elsif array.length.odd? + mid_neg_value + else + (mid_neg_value + array[(mid_index_neg_length-1)]) / 2.0 + end + end @@ -43,6 +51,15 @@ def self.middle_element(array) def self.sum_arrays(array1, array2) + + array3 = [] + + array1.each_with_index do |x,y| + array3 << (x + array2[y]) + end + + array3 end + end