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

Chris Pine Ch9-14 #494

Open
wants to merge 7 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: 28 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
def ask question
# your code here
end
while true
puts question
reply = gets.chomp.downcase

return true if reply == 'yes'
return false if reply == 'no'

puts 'Please answer "yes" or "no".'
end

answer # This is the return of true or false
end
puts 'Hello, and thank you for...'
puts
ask 'Do you like eating tacos?'
ask 'Do you like eating burrittos?'
wets_bed = ask 'Do you wet the bed?'
ask 'Do you like eating chimichangas?'
ask 'Do you like eating sopapillas?'
puts 'Just a few more questions...'
ask 'Do you like drinking horchata?'
ask 'Do you like eating flautas?'

puts
puts 'DEBREIFING:'
puts 'Thank you for...'
puts
puts wets_bed
16 changes: 14 additions & 2 deletions ch09-writing-your-own-methods/old_school_roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
def old_roman_numeral num
# your code here
end
numeral = ''

numeral = numeral + 'M' * (num / 1000)
numeral = numeral + 'D' * (num % 1000 / 500)
numeral = numeral + 'C' * (num % 500 / 100)
numeral = numeral + 'L' * (num % 100 / 50)
numeral = numeral + 'X' * (num % 50 / 10)
numeral = numeral + 'V' * (num % 10 / 5)
numeral = numeral + 'I' * (num % 5 / 1)

numeral
end

puts old_roman_numeral(2016)
39 changes: 37 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
def roman_numeral num
# your code here
end
thousand = (num / 1000)
hundred = (num % 1000 / 100)
ten = (num % 100 / 10)
one = (num % 10)

numeral = 'M' * thousand

if hundred == 9
numeral = numeral + 'CM'
elsif hundred == 4
numeral = numeral + 'CD'
else
numeral = numeral + 'D' * (num % 1000 / 500)
numeral = numeral + 'C' * (num % 500 / 100)
end

if ten == 9
numeral = numeral + 'XC'
elsif ten == 4
numeral = numeral + 'XL'
else
numeral = numeral + 'L' * (num % 100 / 50)
numeral = numeral + 'X' * (num % 50 / 10)
end

if one == 9
numeral = numeral + 'IX'
elsif numeral == 4
numeral = numeral = 'IV'
else
numeral = numeral + 'V' * (num % 10 / 5)
numeral = numeral + 'I' * (num % 5 / 1)
end
numeral
end

puts roman_numeral(2016)
13 changes: 11 additions & 2 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
def dictionary_sort arr
# your code here
end
return arr if arr.length <= 1

middle = arr.pop
less = arr.select{|x| x.downcase < middle.downcase}
more = arr.select{|x| x.downcase >= middle.downcase}

dictionary_sort(less) + [middle] + dictionary_sort(more)
end

words = ['hakuna', 'Matata', 'what', 'a', 'wonderful', 'phrase']
puts(dictionary_sort(words).join(' '))
104 changes: 103 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,105 @@
def english_number number
# your code here
if number < 0 # No negative numbers.
return 'Please enter a number that isn\'t negative.'
end

if number == 0
return 'zero'
end

# No more special cases! No more returns!
num_string = '' # This is the string we will return.
ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']

zillions = [['hundred', 2],
['thousand', 3],
['million', 6],
['billion', 9],
['trillion', 12]]
# "left" is how much of the number we still have left to write out.
# "write" is the part we are writing out right now.
# write and left!!

left = number
while zillions.length > 0
zil_pair = zillions.pop
zil_name = zil_pair[0]
zil_base = 10 ** zil_pair[1]
write = left/zil_base # How many zillions left?
left = left - write*zil_base # Subtract off those zillions.

if write > 0
# Now here's the recursion:
prefix = english_number write
num_string = num_string + prefix + ' ' + zil_name

if left > 0
# So we don't write 'two billionfifty-one'...
num_string = num_string + ' '
end
end
end

write = left/100 # How many hundreds left?
left = left - write*100 # Subtract off those hundreds.
if write > 0
# Now here's the recursion:
hundreds = english_number write
num_string = num_string + hundreds + ' hundred'

if left > 0
# So we don't write 'two hundredfifty-one'...
num_string = num_string + ' '
end
end

write = left/10 # How many tens left?
left = left - write*10 # Subtract off those tens.

if write > 0
if ((write == 1) and (left > 0))
# Since we can't write "tenty-two" instead of "twelve", we have to make a special exception
# for these.
num_string = num_string + teenagers[left-1]
# The "-1" is because teenagers[3] is 'fourteen', not 'thirteen'.

# Since we took care of the digit in the # ones place already, we have nothing left to write.
left = 0
else
num_string = num_string + tens_place[write-1]
# The "-1" is because tens_place[3] is 'forty', not 'thirty'.
end

if left > 0
# So we don't write 'sixtyfour'...
num_string = num_string + '-'
end
end

write = left # How many ones left to write out?
left = 0 # Subtract off those ones.

if write > 0
num_string = num_string + ones_place[write-1]
# The "-1" is because ones_place[3] is 'four', not 'three'.
end
# Now we just return "num_string"...
num_string
end

puts english_number( 0)
puts english_number( 9)
puts english_number( 10)
puts english_number( 11)
puts english_number( 17)
puts english_number( 32)
puts english_number( 88)
puts english_number( 99)
puts english_number(100)
puts english_number(101)
puts english_number(234)
puts english_number(3211)
puts english_number(999999)
puts english_number(1000000000000)
79 changes: 78 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,78 @@
# your code here
# def english_number number
if number < 0 # No negative numbers.
return 'Please enter a number that isn\'t negative.'
end

if number == 0
return 'zero'
end

# No more special cases! No more returns!
num_string = '' # This is the string we will return.
ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']

# "left" is how much of the number
# we still have left to write out.
# "write" is the part we are
# writing out right now.
# write and left...get it? :)

left = number
write = left/100 # How many hundreds left?
left = left - write*100 # Subtract off those hundreds.
if write > 0
# Now here's the recursion:
hundreds = english_number write
num_string = num_string + hundreds + ' hundred'

if left > 0
# So we don't write 'two hundredfifty-one'...
num_string = num_string + ' '
end
end

write = left/10 # How many tens left?
left = left - write*10 # Subtract off those tens.

if write > 0
if ((write == 1) and (left > 0))
# Since we can't write "tenty-two" instead of "twelve", we have to make a special exception
# for these.
num_string = num_string + teenagers[left-1]
# The "-1" is because teenagers[3] is 'fourteen', not 'thirteen'.

# Since we took care of the digit in the # ones place already, we have nothing left to write.
left = 0
else
num_string = num_string + tens_place[write-1]
# The "-1" is because tens_place[3] is 'forty', not 'thirty'.
end

if left > 0
# So we don't write 'sixtyfour'...
num_string = num_string + '-'
end
end

write = left # How many ones left to write out?
left = 0 # Subtract off those ones.

if write > 0
num_string = num_string + ones_place[write-1]
# The "-1" is because ones_place[3] is 'four', not 'three'.
end
# Now we just return "num_string"...
num_string
end

num_at_start = 10 # change to 9999 if you want
num_now = num_at_start
while num_now > 2
puts english_number(num_now).capitalize + ' bottles of beer on the wall, ' +
english_number(num_now) + ' bottles of beer!'
num_now = num_now - 1
puts 'Take one down, pass it around, ' +
english_number(num_now) + ' bottles of beer on the wall!'
end
27 changes: 25 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
def shuffle arr
# your code here
end
shuf = []
#shuffle an array of any size above zero
while arr.length > 0
rand_index = rand(arr.length)
#iterate each item placing all bar the random one in the new array which goes into shuf
curr_index = 0
new_arr = []

arr.each do |item|
if curr_index == rand_index
shuf.push item
else
new_arr.push item
end

curr_index = curr_index + 1
end
#call back new array instead of original array
arr = new_arr
end

shuf
end

puts(shuffle([1,9,2,8,3,7,4,6,5]))
30 changes: 28 additions & 2 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
def sort arr
# your code here
end
rec_sort arr, []
end

def rec_sort unsorted, sorted
if unsorted.length <= 0
return sorted
end
# So if we got here, then it means we still # have work to do.
smallest = unsorted.pop
still_unsorted = []

unsorted.each do |tested_object|
if tested_object < smallest
still_unsorted.push smallest
smallest = tested_object
else
still_unsorted.push tested_object
end
end

# Now "smallest" really does point to the
# smallest element that "unsorted" contained,
# and all the rest of it is in "still_unsorted". l
sorted.push smallest

rec_sort still_unsorted, sorted
end
puts(sort([' can' ,' feel' ,' singing' ,' like' ,' a' ,' can' ]))
Loading