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

Pull request #505

Open
wants to merge 27 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
41 changes: 39 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
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
else
puts 'Please answer "yes" or "no".'
end

end

answer

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 chimicngas?'
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


27 changes: 25 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,26 @@
def old_roman_numeral num
# your code here
end
# empty string is assigned to the variable roman.
roman = ''

# num is divided by 1000. Using the / method that leaves no remainders. The answer is then multplied by the string 'M'.
roman = roman + 'M' * (num / 1000)
# roman now has the new value of 'M'. Now num is divided by 1000, but this time using the % method that returns the remainder.
#The remaineder is then diided by 500 using the /method that doesn't return a remainder. The answer is then multiplied by the string 'D'
# Which is added to the previous roman variable value 'MD'
roman = roman + 'D' * (num % 1000 / 500)
# The method continues to add the new set of strings.
roman = roman + 'C' * (num % 500 / 100)

roman = roman + 'L' * (num % 100 / 50)

roman = roman + 'X' * (num % 50 / 10)

roman = roman + 'V' * (num % 10 / 5)

roman = roman + 'I' * (num % 5 / 1)

roman # The complete set.

end

puts(old_roman_numeral(1999))
44 changes: 41 additions & 3 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
def roman_numeral num
# your code here
end
=begin
This takes a number and returns the roman numerals but with the small numbers going before the big numbers and subtracting the value to equal the total.
=end

def roman_numeral num # this method takes a number (num)
thous = (num / 1000) # it divides the num by 1000 leaving no remainders and asigns that value to the variable thous
hunds = (num % 1000 / 100) # the variable hunds is assigned to the sum of the number divided by 1000 and its remainder divided by 100.
tens = (num % 100 / 10) # ect
ones = (num % 10 )
roman = 'M' * thous # the value of thousand is multiplied by 'M' and assigned to the variable roman

if hunds == 9 # if statment uses a bolean. If the value of hunds is equal to 9 then it asigns roman the new value of
roman = roman + 'CM'
elsif hunds == 4
roman = roman + 'CD'
else
roman = roman + 'D' * (num % 1000 / 500)
roman = roman + 'C' * (num % 500 / 100)
end

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

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

roman
end
puts(roman_numeral(1999))
46 changes: 43 additions & 3 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
def dictionary_sort arr
# your code here
end
def dictionary_sort arr # takes an array
rec_dict_sort arr, [] # calls the recursive function
end

def rec_dict_sort unsorted, sorted # takes the array that isn't sorted and an unsorted array.
if unsorted.length <= 0 # if the unsorted array is empty it returns the sorted array. Function finnished.
return sorted
end

# So if we got here, then it means we still

# have work to do.

smallest = unsorted.pop # Takes the last element of the array. removes it and holds it in the variable 'smallest'

still_unsorted = []

unsorted.each do |tested_object| # block of code. Takes each element from the array and iterates through them.

if tested_object.downcase <= smallest.downcase # If statment tests to see if each object in downcase compares to the smallest in down.case

still_unsorted.push smallest # It then takes the smallest and pushes it into the still_unsorted array.

smallest = tested_object # new object assigned as smallest and used to compare the rest of the code to.

else
still_unsorted.push tested_object # if the object is not smaller than put it straight into the still_unsorted array.
end
end

# Now "smallest" really does point to the

# smallest element that "unsorted" contained,

# and all the rest of it is in "still_unsorted".

sorted.push smallest

rec_dict_sort still_unsorted, sorted

end

puts(dictionary_sort(['can','feel','singing.','like','A','can']))
86 changes: 85 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@

def english_number number
# your code here
if number < 0
return "Oops! Please use a positive integer!"
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
# Now here's the recursion:
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
121 changes: 120 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,120 @@
# 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']
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" 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

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/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 = 5 # 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

puts "Two bottles of beer on the wall, two bottles of beer!"
puts "Take one down, pass it around, one bottle of beer on the wall!"
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!"
Loading