From 807d78a1d43d46aa6486596932779e0e51465bd8 Mon Sep 17 00:00:00 2001 From: "Alexandria A. Johnson" Date: Thu, 28 Aug 2014 14:57:29 -0500 Subject: [PATCH 1/6] Enables ArrayUtil Max method for arrays --- array_problems/arrays.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 7a4a2cc..ad70367 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,6 +3,8 @@ module ArrayUtil def self.max(array) + array.sort! + array[-1] end def self.middle_element(array) From 9ae54f79b6f3f6730f5113dcfa51fa1e6f208153 Mon Sep 17 00:00:00 2001 From: "Alexandria A. Johnson" Date: Thu, 28 Aug 2014 15:30:16 -0500 Subject: [PATCH 2/6] Max method rewritten to avoid sort method; array middle method defined --- array_problems/arrays.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index ad70367..0c1b4e1 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -3,13 +3,25 @@ module ArrayUtil def self.max(array) - array.sort! - array[-1] + #I found this solution in the Pry Debugger notes. I think it would take some time to execute because I assume it goes through every item in the array, since it's an inject. + array.inject {|y, x| x > y ? x : y } end def self.middle_element(array) + #I don't think these would require too much time because the if/else is based on the array length, which is pre-defined in the array. + if array.length == 0 + nil + elsif array.length % 2 == 0 + mid = array.length/2 + (array[mid] + array[mid - 1])/2.to_f + else + mid = array.length/2 + array[mid] + end end def self.sum_arrays(array1, array2) + # array1.each_index do |x| + # array1[x] end end From 864c025436b6bdbc9142023fb8fa33e976f1e06c Mon Sep 17 00:00:00 2001 From: "Alexandria A. Johnson" Date: Thu, 28 Aug 2014 15:57:13 -0500 Subject: [PATCH 3/6] Adds sum method to add the elements of two arrays by corresponding indexes --- array_problems/arrays.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/array_problems/arrays.rb b/array_problems/arrays.rb index 0c1b4e1..9037f27 100644 --- a/array_problems/arrays.rb +++ b/array_problems/arrays.rb @@ -21,7 +21,9 @@ def self.middle_element(array) end def self.sum_arrays(array1, array2) - # array1.each_index do |x| - # array1[x] + #This one might take some time because the each will have to run through every element of two arrays. + sum = [] + array1.each_index {|i| sum.push(array1[i] + array2[i]) } + sum end end From c62936122486d7ce4dea355f8cd4d02cdccbe3db Mon Sep 17 00:00:00 2001 From: "Alexandria A. Johnson" Date: Thu, 28 Aug 2014 17:37:03 -0500 Subject: [PATCH 4/6] Defines swap_small method in Set1 module --- set1/set1.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/set1/set1.rb b/set1/set1.rb index 0ca2970..72d7f1a 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,6 +1,19 @@ module Set1 def self.swap_small(array) + if array.length == 1 + return array + else + min = array.first + array.each do |n| + if n < min + min = n + end + end + array[array.index(min)] = array.first + array[0] = min + array end +end def self.find_sum_2(array, sum = 0) end From e819853fe0b26c7772c591343a1e78927aa0255f Mon Sep 17 00:00:00 2001 From: "Alexandria A. Johnson" Date: Thu, 28 Aug 2014 18:09:54 -0500 Subject: [PATCH 5/6] Defines find_sum_2 and find_sum_3 methods to determine if any of the elements in the array add up 0 --- set1/set1.rb | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/set1/set1.rb b/set1/set1.rb index 72d7f1a..c39f37b 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -16,8 +16,44 @@ def self.swap_small(array) end def self.find_sum_2(array, sum = 0) + if array.include?(sum) + return true + elsif array.empty? + return false + else + array.each_index do |x| + array.each_index do |y| + if array[x] + array[y] == 0 + return true + end + end + end + end + false end def self.find_sum_3(array) + if array.include?(0) + return true + elsif array.empty? + return false + else + array.each_index do |x| + array.each_index do |y| + array.each_index do |z| + if array[x] + array[y] + array[z] == 0 + return true + end + end + end + end + end + false end + end + + + + + From 30408e7580e9bb1e67edb1012e73edaa2286a233 Mon Sep 17 00:00:00 2001 From: thealaj Date: Thu, 28 Aug 2014 18:31:36 -0500 Subject: [PATCH 6/6] Update set1.rb Patrick: Forgot to add my Big O guesses initially. They are now included. --- set1/set1.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/set1/set1.rb b/set1/set1.rb index c39f37b..8811706 100644 --- a/set1/set1.rb +++ b/set1/set1.rb @@ -1,4 +1,5 @@ module Set1 + # I think this method is O(n) def self.swap_small(array) if array.length == 1 return array @@ -14,7 +15,7 @@ def self.swap_small(array) array end end - +# I think this method is O(n^2) def self.find_sum_2(array, sum = 0) if array.include?(sum) return true @@ -32,6 +33,7 @@ def self.find_sum_2(array, sum = 0) false end +#I think this method is O(n^3) def self.find_sum_3(array) if array.include?(0) return true