From 936fdfd82b85ce026620847ab4c71cfcb9f6eb57 Mon Sep 17 00:00:00 2001 From: jjousun Date: Wed, 19 Apr 2017 23:57:22 -0700 Subject: [PATCH 1/2] Solutions for 1, 2, 4 --- numUnique.rb | 24 ++++++++++++++++++++++++ numUnique2.rb | 15 +++++++++++++++ print_list.rb | 30 ++++++++++++++++++++++++++++++ stretch.rb | 22 ++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 numUnique.rb create mode 100644 numUnique2.rb create mode 100644 print_list.rb create mode 100644 stretch.rb diff --git a/numUnique.rb b/numUnique.rb new file mode 100644 index 0000000..f934544 --- /dev/null +++ b/numUnique.rb @@ -0,0 +1,24 @@ +# list = [5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41] +# numUnique(list) = 9 + +# list = [1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41] +# numUnique(list) = 15 + +# list = [] +# numUnique = 0 + +def numUnique(arr) + # empty hash + my_h = {} + # go through the Array + arr.each do |number| + # first number: the key is the value of the first number, value is 1 + # second number: does it equal the first key? + # if so, first key's value increments one + # if not, the new key is the value of the second number, value is 1 + # third number: does it equal the second key? + # if so, value of second key increments one + # if not, the new key is the value of the third number, value is 1 + # etc + # What is the size of the hash? +end diff --git a/numUnique2.rb b/numUnique2.rb new file mode 100644 index 0000000..02fc8ce --- /dev/null +++ b/numUnique2.rb @@ -0,0 +1,15 @@ +def numUnique2(arr) + place_marker = 0 + counter = 0 + + arr.each do |number| + if arr[place_marker] != arr [place_marker-1] + counter += 1 + place_marker += 1 + else + place_marker += 1 + end + end + + return counter +end diff --git a/print_list.rb b/print_list.rb new file mode 100644 index 0000000..6a8eb48 --- /dev/null +++ b/print_list.rb @@ -0,0 +1,30 @@ +# list = [3, 19, 27, 4, 98, 304, -9, 72] +# print_list(list) = [3, 19, 27, 4, 98, 304, -9, 72] +# +# Questions I had: +# Is the return value a string, or another array? +# If it's a string, is string interpolation ok? + +# Solution for string output +def print_list(arr) + my_s = "[" + # beginning of the string has the opening bracket + arr[0...arr.length-1].each { |x| my_s += "#{x}, " } + # add on each value in the array with a comma and space, except for the last value + my_s += "#{arr[-1]}" + # add on the last value, which doesn't have a comma and space + my_s += "]" + # add on the closing bracket + return my_s + # the entire string +end + +# Solution for array output (not sure if this is allowed, though) +def print_list(arr) + string = [] + # new empty array + arr[0...arr.length].each { |x| string << x } + # shovel each value into new empty array + return string + # prints the new array (arrays are printed as comma-separated lists enclosed in brackets) +end diff --git a/stretch.rb b/stretch.rb new file mode 100644 index 0000000..8fbbc3f --- /dev/null +++ b/stretch.rb @@ -0,0 +1,22 @@ +# list = [18, 7, 4, 24, 11] +# stretch(list) = [9, 9, 4, 3, 2, 2, 12, 12, 6, 5] + +def stretch(arr) + # new empty array + new_arr = [] + # go through each element + arr.each do |number| + # if even, divide by two + if number % 2 == 0 + new_arr << (number / 2) + new_arr << (number / 2) # this isn't DRY, I think + # if odd, subtract one, divide by two. Add one to the first half + else + new_arr << (((number - 1) / 2) + 1) + new_arr << ((number - 1) / 2) # DRY-ness issue here, maybe + # store both halves + end + end + # return new array + return new_arr +end From 46bf810f91cc8a6d80a41f0241e6f6af3b06972d Mon Sep 17 00:00:00 2001 From: jjousun Date: Sun, 23 Apr 2017 09:54:38 -0700 Subject: [PATCH 2/2] Did problem 3 --- numUnique.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/numUnique.rb b/numUnique.rb index f934544..cebf85f 100644 --- a/numUnique.rb +++ b/numUnique.rb @@ -21,4 +21,10 @@ def numUnique(arr) # if not, the new key is the value of the third number, value is 1 # etc # What is the size of the hash? + if my_h[num] + my_h[num] += 1 + else + my_h[num] = 1 + end + return my_h.length end