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

Kelsey McAlpine - Whiteboard Practice Solutions #14

Open
wants to merge 2 commits 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
40 changes: 40 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(array)
raise ArgumentError.new("You must input an array") if array.class != Array
print array[0..-1]
end

list = [3, 19, 27, 4, 98, 304, -9, 72]
print_array(list)
```

## 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,36 @@ 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(array)
raise ArgumentError.new("You must input an array") if array.class != Array

index = 0
repeat = array.length
new_array = []

repeat.times do
if array[index] == 0
new_num1 = 0
new_num2 = 0
elsif array[index] % 2 == 0
new_num1 = array[index] / 2
new_num2 = new_num1
else
new_num1 = array[index] / 2
new_num2 = new_num1 + 1
end

new_array << new_num1
new_array << new_num2

index += 1
end

print new_array
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 Down