Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solutions for 1, 2, 4 #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions numUnique.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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?
if my_h[num]
my_h[num] += 1
else
my_h[num] = 1
end
return my_h.length
end
15 changes: 15 additions & 0 deletions numUnique2.rb
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions print_list.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 22 additions & 0 deletions stretch.rb
Original file line number Diff line number Diff line change
@@ -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