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

Whiteboard problems #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ Then the call of `print(list)` should produce the following output:

Your method should produce a single line of output (may wrap with long lists).

def print_array(list)
str = ''
str += '[ '
for i in 0..list.length
str += arr[list] + ","
end
str += ']'
puts str
end

## Problem #2
Write a method named `stretch` that accepts an array of integers as a
parameter and returns a **new** array twice as large as the original, where
Expand All @@ -49,6 +59,21 @@ is stretched into the pair 9, 9, the number 7 is stretched into 4, 3,
the number 4 is stretched into 2, 2, the number 24 is stretched into 12,
12 and the number 11 is stretched into 6, 5.)

def stretch(list)
new_list = []
for i in 0..list.length
if list[i] % 2 == 0
2.times do
new_list << list[i]
end
else
new_list << list[i] + 1
new_list << list[i]
end
end
return new_list
end

## Problem #3
Write a method named `numUnique` that accepts a sorted array of integers
as a parameter and **utilizes a hash to** calculate and return the number of
Expand All @@ -68,6 +93,18 @@ because this list contains 15 different values.

If passed an empty list, your method should return **0**.

def numUnique(list)
count = Hash.new

return 0 if list.length == 0

for i in 0..list.length
count[list[i]] = "present"
end

return count.length
end

## Problem #4
Write a method named `numUnique2` that functions the same as `numUnique`
except that it **does NOT use a hash** to solve the problem. In this version,
Expand All @@ -76,3 +113,17 @@ outside of fixnum variables, if needed.

Remember that you can assume that the values in the array appear in
sorted (nondecreasing) order.
`list = [1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41]`


def numUnique2(list)
num = list[0]
count = 1
for i in 0..list.length
if list[i] != num
count += 1
num = list[i]
end
end
return count
end