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

if reply == "yes"
return true
elsif reply == "no"
return false
end

puts "Please answer \"yes\" or \"no\"."
end
answer
end
20 changes: 18 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,19 @@
def old_roman_numeral num
# your code here
end

x = ""

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

x

end

puts old_roman_numeral 141
puts old_roman_numeral 3000
puts old_roman_numeral 6
45 changes: 43 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,44 @@
def roman_numeral num
# your code here
end

thou = (num / 1000)
hund = (num % 1000 / 100)
tens = (num % 100 / 10)
ones = (num % 10)

x = "M" * thou

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

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

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

x # returns roman numeral
end

# test criteria below
puts (roman_numeral 4)
puts (roman_numeral 1949)
puts (roman_numeral 2654)
puts (roman_numeral 9)
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
recursive_sort arr, []
end

def recursive_sort unsorted, sorted
if unsorted.length <= 0
return sorted
end

small = unsorted.pop

remain_unsorted = []

unsorted.each do | x |
if x.downcase < small.downcase
remain_unsorted.push small
small = x
else
remain_unsorted.push x
end
end

sorted.push small
recursive_sort remain_unsorted, sorted
end

puts(dictionary_sort(["beans","Eggs","Brunch","apple","Acorn","badger"]))
116 changes: 115 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,117 @@
def english_number number
# your code here
if number < 0
return 'Please enter a number that isn\'t negative.'
end
if number == 0
return 'zero'
end

num_string = '' # String to be returned.

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.
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
# 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]
# 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]
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]
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)
puts english_number(109238745102938560129834709285360238475982374561034)
110 changes: 109 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,109 @@
# your code here
# English number:
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']

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

# 99 Bottles Starts Here:
start_num = 9999
current_num = start_num

while current_num > 2
puts english_number(current_num).capitalize + " bottles of beer on the wall" +
english_number(current_num) + " bottles of beer!"
current_num = current_num - 1
puts "Take one down, pass it around, " + english_number(current_num) +
" 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!"
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
shuffled = []

while arr.length > 0
rand_index = rand(arr.length)
index = 0
new_arr = []

arr.each do |x|
if index == rand_index
shuffled.push x
else
new_arr.push x
end

index = index + 1
end

arr = new_arr
end

shuffled

end

puts(shuffle([1,2,3,4,5,6,7,8,9]))
Loading