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

initial pull request #488

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

puts "Hello, and thank you for..."
puts
ask "Do you like eating tacos?" # Ignore this return value
ask "Do you like eating burritos?" # And this one
wets_bed = ask "Do you wet the bed?" # Save this return value
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"
21 changes: 19 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,20 @@
def old_roman_numeral num
# your code here
end
# reference:
# I = 1, V = 5, X = 10, L = 50
# C = 100, D = 500, M = 1000

roman = ""

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

#puts "The number #{num}, is converted to:\n#{roman} in roman numerals"
roman
end

puts old_roman_numeral 555
55 changes: 53 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,54 @@
def roman_numeral num
# your code here
end
# reference:
# I = 1, V = 5, X = 10, L = 50
# C = 100, D = 500, M = 1000

thous = (num / 1000)
hunds = (num % 1000 / 100)
tens = (num % 100 / 10)
ones = (num % 10 )

roman = 'M' * thous # start building the roman number
# starting with the largest (thousands)


# conversion code for integer to roman is the same
# as the old_school_roman_numerals

# 9 takes 1 before 10
# 4 takes 1 before 5
# giving the code instructions for case of 9 and 4 for each group
# respectively (hundreds, tens, ones)


if hunds == 9
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

7 changes: 5 additions & 2 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
def dictionary_sort arr
# your code here
end
arr.sort_by { |word| word.downcase }
end

#words = ['Can','feel','singing.','like','A','can',"b","B"]
#puts dictionary_sort words
81 changes: 80 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,82 @@
def english_number number
# your code here
if number < 0 # No negative numbers.
return "Please enter a number that isn't negative."
elsif number == 0
return "zero"
end

num_string = "" # start with empy number 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 = number

while zillions.length > 0
zil_pair = zillions.pop #removes last array pair until reaches correct
zil_name = zil_pair[0] # takes the name of number

# how many
zil_base = 10 ** zil_pair[1]
write = left_number/zil_base
left_number = left_number - write*zil_base

if write > 0
prefix = english_number write # call method
num_string = num_string + prefix + " " + zil_name
if left_number > 0

num_string = num_string + " " # building the number
end
end

end

write = left_number/10
left_number = left_number - write*10
if write > 0
if ((write == 1) and (left_number > 0))
num_string = num_string + teenagers[left_number-1]
left_number = 0
else
num_string = num_string + tens_place[write-1]
end
if left_number > 0
num_string = num_string + "-"
end
end
write = left_number
left_number = 0
if write > 0
num_string = num_string + ones_place[write-1]
end

num_string
end

#puts english_number(101)
#puts english_number(234)
#puts english_number(3211)
#puts english_number(999999)
100 changes: 99 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
# your code here
def english_number number
if number < 0 # No negative numbers.
return "Please enter a number that isn't negative."
elsif number == 0
return "zero"
end

num_string = "" # start with empy number 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 = number

while zillions.length > 0
zil_pair = zillions.pop #removes last array pair until reaches correct
zil_name = zil_pair[0] # takes the name of number

# how many
zil_base = 10 ** zil_pair[1]
write = left_number/zil_base
left_number = left_number - write*zil_base

if write > 0
prefix = english_number write # call method
num_string = num_string + prefix + " " + zil_name
if left_number > 0

num_string = num_string + " " # building the number
end
end

end

write = left_number/10
left_number = left_number - write*10
if write > 0
if ((write == 1) and (left_number > 0))
num_string = num_string + teenagers[left_number-1]
left_number = 0
else
num_string = num_string + tens_place[write-1]
end
if left_number > 0
num_string = num_string + "-"
end
end
write = left_number
left_number = 0
if write > 0
num_string = num_string + ones_place[write-1]
end

num_string
end

# use any number below to set the lyrics of the song
num_at_start = 99 # 99 beers!
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!"

num_now = num_at_start

# using a while loop to call the enlish_number function
# converting number to word_number for a given number
# up to 3
# then change text for last 2 bottles

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!"
10 changes: 7 additions & 3 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
def shuffle arr
# your code here
end
def shuffle(array)
array.shuffle
end


#test_array = ["hi",5,"6",["hey","you"],"Einstein"]
#puts shuffle(test_array)
29 changes: 27 additions & 2 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
def sort arr
# your code here
end
recursive_sort(arr, [])
end

def recursive_sort(unsorted_arr, sorted_arr)
if unsorted_arr.length < 1
return sorted_arr
end

last_unsorted_word = unsorted_arr.pop
new_arr = []

unsorted_arr.each do |word|
if word < last_unsorted_word
new_arr << last_unsorted_word
last_unsorted_word = word
else
new_arr << word
end
end

sorted_arr << last_unsorted_word
recursive_sort(new_arr, sorted_arr)
end

#test_array = ["orange","apple","banana","cherry","orange","watermelon"]
#puts test_array.length
#puts sort(test_array)
37 changes: 36 additions & 1 deletion ch11-reading-and-writing/build_a_better_playlist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
def music_shuffle filenames
# your code here

filenames = filenames.sort
len = filenames.length

# shuffle twice.
2.times do
l_idx = 0
r_idx = len/2 #
shuf = []


while shuf.length < len
if shuf.length%2 == 0

shuf.push(filenames[r_idx])
r_idx = r_idx + 1
else

shuf.push(filenames[l_idx])
l_idx = l_idx + 1
end
end

filenames = shuf
end

arr = []
cut = rand(len)
idx = 0

while idx < len
arr.push(filenames[(idx+cut)%len])
idx = idx + 1
end

arr
end
Loading