From 298e4f4972e6a9e251134287aeefaf643f73b2d8 Mon Sep 17 00:00:00 2001 From: Jeffrey Penkar Date: Wed, 27 Aug 2014 22:57:13 -0500 Subject: [PATCH 1/3] max method created --- algorithms.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 algorithms.rb diff --git a/algorithms.rb b/algorithms.rb new file mode 100644 index 0000000..0dd49a1 --- /dev/null +++ b/algorithms.rb @@ -0,0 +1,8 @@ +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 + + From 4cecc666273501542d9014cfae8c92b0aeb28f4f Mon Sep 17 00:00:00 2001 From: Jeffrey Penkar Date: Wed, 27 Aug 2014 23:07:24 -0500 Subject: [PATCH 2/3] middle element created --- algorithms.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/algorithms.rb b/algorithms.rb index 0dd49a1..00ffc0c 100644 --- a/algorithms.rb +++ b/algorithms.rb @@ -6,3 +6,14 @@ def max(array) 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 \ No newline at end of file From 704e77f822d8c1921697113a1876eab9ad5e9756 Mon Sep 17 00:00:00 2001 From: Jeffrey Penkar Date: Wed, 27 Aug 2014 23:16:08 -0500 Subject: [PATCH 3/3] Sum Arrays method finished. --- algorithms.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/algorithms.rb b/algorithms.rb index 00ffc0c..0058388 100644 --- a/algorithms.rb +++ b/algorithms.rb @@ -16,4 +16,11 @@ def middle_element(array) 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