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

Learn to Program exercises #483

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

if (reply == "yes") || (reply == "no")
if reply == "yes"
return true
else
return false
end
break
else
puts 'Please answer "yes" or "no".'
end
end
end

puts "Hello, and thank you for..."
puts
ask "Do you like eating tacos?"
ask "Do you like eating burritos?"
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 "DEBRIEFING:"
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

old_roman = ""

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

old_roman

end
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
new_roman = ""

thousands = num / 1000
hundreds = num % 1000 / 100
tens = num % 100 / 10
ones = num % 10

new_roman = "M" * thousands

if hundreds == 9
new_roman = new_roman + "CM"
elsif hundreds == 4
new_roman = new_roman + "CD"
else
new_roman = new_roman + "D" * (num % 1000 / 500)
new_roman = new_roman + "C" * (num % 500 / 100)
end

if tens == 9
new_roman = new_roman + "XC"
elsif tens == 4
new_roman = new_roman + "XL"
else
new_roman = new_roman + "L" * (num % 100 / 50)
new_roman = new_roman + "X" * (num % 50 / 10)
end

if ones == 9
new_roman = new_roman + "IX"
elsif ones == 4
new_roman = new_roman + "IV"
else
new_roman = new_roman + "V" * (num % 10 / 5)
new_roman = new_roman + "I" * (num % 5)
end
new_roman
end
28 changes: 26 additions & 2 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
def dictionary_sort arr
# your code here
end
rec_dic_sort arr, []
end

def rec_dic_sort unsorted, sorted

if unsorted.length < 1
return sorted
end

still_unsorted = []
smallest = unsorted.pop # smallest placeholder item

unsorted.each do |i|
if i.downcase < smallest.downcase
still_unsorted << smallest
smallest = i
else
still_unsorted << i
end
end

sorted << smallest
rec_dic_sort still_unsorted, sorted

end

63 changes: 61 additions & 2 deletions ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,62 @@
def english_number number
# your code here
end
if number < 0
return "Please eneter a number that isn't negative."
end
if number == 0
return "zero"
end

num_string = ""

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], ["quadrillion", 15], ["quintillion", 18], ["sextillion", 21],["septillion", 24], ["octillion", 27], ["nonillion", 30], ["decillion", 33], ["undecillion", 36], ["duodecillion", 39], ["tredecillion", 42], ["quattuordecillion", 45], ["quindecillion", 48], ["sexdecillion", 51], ["septendecillion", 54], ["octodecillion", 57], ["novemdecillion", 60], ["vigintillion", 63], ["googol", 100]]



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
left = left - write * zil_base

if write > 0
prefix = english_number write
num_string = num_string + prefix + " " + zil_name

if left > 0
num_string = num_string + " "
end
end
end


write = left / 10
left = left - write * 10

if write > 0
if ((write == 1) and (left > 0))
num_string = num_string + teenagers[left-1]
left = 0
else
num_string = num_string + tens_place[write-1]
end
if left > 0
num_string = num_string + "-"
end
end

write = left
left = 0

if write > 0
num_string = num_string + ones_place[write-1]
end

num_string

end
113 changes: 112 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
# your code here
def english_number number
if number < 0
return "Please enter a number that isn't negative."
end
if number == 0
return "zero"
end

num_string = ""

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 = number
write = left / 1000000000000 # how many trllions?
left = left - write * 1000000000000 # substract the millions

if write > 0
trillions = english_number write # recursion!
num_string = num_string + trillions + " trillion"
if left > 0
num_string = num_string + " "
end
end

write = left / 1000000000 # how many billions?
left = left - write * 1000000000 # substract the billions

if write > 0
billions = english_number write # recursion!
num_string = num_string + billions + " billion"
if left > 0
num_string = num_string + " "
end
end


write = left / 1000000 # how many millions?
left = left - write * 1000000 # substract the millions

if write > 0
millions = english_number write # recursion!
num_string = num_string + millions + " million"
if left > 0
num_string = num_string + " "
end
end

write = left / 1000 # how many thousands?
left = left - write * 1000 # substract the thousands

if write > 0
thousands = english_number write # recursion!
num_string = num_string + thousands + " thousand"
if left > 0
num_string = num_string + " "
end
end

write = left / 100 # how many hundreds?
left = left - write * 100 # substract the hundreds

if write > 0
hundreds = english_number write # recursion!
num_string = num_string + hundreds + " hundred"
if left > 0
num_string = num_string + " "
end
end

write = left / 10
left = left - write * 10

if write > 0
if ((write == 1) and (left > 0))
num_string = num_string + teenagers[left-1]
left = 0
else
num_string = num_string + tens_place[write-1]
end
if left > 0
num_string = num_string + "-"
end
end

write = left
left = 0

if write > 0
num_string = num_string + ones_place[write-1]
end

num_string

end

bbottles = 99

while bbottles >1
bb_eng = english_number bbottles
bb_next = english_number (bbottles-1)
puts (bb_eng + " bottles of beer on the wall, " + bb_eng + " bottles of beer!").capitalize
puts "Take one down and pass it around, " + bb_next + " bottles of beer on the wall!"
puts
bbottles -=1
end
puts "One bottle of beer on the wall, one bottle of beer!"
puts "Take one down, pass it around, no more bottles of beer on the wall!"



14 changes: 12 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
def shuffle arr
# your code here
def shuffle (array)
shuffled = []
if array.length == 0
return array
else
while array.length > 0
n = rand(array.length - 1)
shuffled << array[n]
array.delete_at(n)
end
end
shuffled
end
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 < 1
return sorted
end

still_unsorted = []
smallest = unsorted.pop # smallest placeholder item

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

# this returns smallest item,store it in sorted and use recursion to find the next smallest item.

sorted << smallest
rec_sort still_unsorted, sorted

end

Loading