Skip to content

updated ask.rb #491

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
26 changes: 24 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
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
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?'

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

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)

roman
end

puts old_roman_numeral(1991)
42 changes: 40 additions & 2 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

thousand = num/1000
hundred = num %1000 /100
ten = num%100 / 10
ones = num % 10 / 1

roman = 'M' * thousand
if hundred ==9
roman << 'CM'
elsif hundred == 4
roman << 'CD'
else
roman << 'D' * (num %1000 /500)
roman << 'C' * (num %500 /100)
end

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

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

roman


end

puts roman_numeral(1984)
27 changes: 25 additions & 2 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
def dictionary_sort arr
# your code here
end
rec_dict_sort arr,[]
end

def rec_dict_sort unsorted_array, sorted_array
if unsorted_array.length <=0
return sorted_array
end

smallest = unsorted_array.pop
still_unsorted = []

unsorted_array.each do |word|
if word.downcase < smallest.downcase
still_unsorted.push smallest
smallest = word
else
still_unsorted.push word
end
end

sorted_array.push smallest
rec_dict_sort still_unsorted, sorted_array
end

puts(dictionary_sort(['can','feel','like','singing','A','can']))
63 changes: 62 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,64 @@
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 =""

ones_place = ['one','two','three','four','five','six','seven','eight','nine']
tens_place = ['ten','twenty','thirty','fourty','fifty','sixty','seventy','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',39],
['duodecillion',39],['tredecillion',42],['quattuordecillion',45],['quindecillion',48],
['sexdecillion',51],['septendectillion',54],['octodecillion',57],['novemdecillion',60],
['vigintillion',63],['googol',100]]

#'left' is how much left of the number we still have t write
#'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
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

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

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

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

num_string
end

puts english_number(999)
puts english_number(4352)
76 changes: 75 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
# your code here
# your code heredef 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','fourty','fifty','sixty','seventy','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',39],
['duodecillion',39],['tredecillion',42],['quattuordecillion',45],['quindecillion',48],
['sexdecillion',51],['septendectillion',54],['octodecillion',57],['novemdecillion',60],
['vigintillion',63],['googol',100]]

#'left' is how much left of the number we still have t write
#'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
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

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

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

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

num_string
end

num_at_start = 5
num_now = num_at_start
while num_now >2
puts enlgish_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!"
5 changes: 3 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
def shuffle arr
# your code here
end
arr.sort_by{rand}
end
puts shuffle(['this', 'is', 'a','shuffle','test'])
26 changes: 24 additions & 2 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
def sort arr
# your code here
end
rec_sort arr, []
end

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

smallest = unsorted.pop
still_unsorted = []

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

sorted.push smallest
rec_sort still_unsorted, sorted
end
puts(sort(['can','feel','singing','like','a','can']))
57 changes: 56 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,58 @@
def music_shuffle filenames
# your code here
songs_and_paths = filenames.map do |s|
[s, s.split('/')] # [song, path]
end

tree = {:root => []}

# put each song into the tree
insert_into_tree = proc do |branch, song, path|
if path.length == 0 # add to current branch
branch[:root] << song
else # delve deeper
sub_branch = path[0]
path.shift # like "pop", but pops off the front

if !branch[sub_branch]
branch[sub_branch] = {:root => []}
end

insert_into_tree[branch[sub_branch], song, path]
end
end

songs_and_paths.each{|sp| insert_into_tree[tree, *sp]}

# recursively:
# - shuffle sub-branches (and root)
# - weight each sub-branch (and root)
# - merge (shuffle) these groups together
shuffle_branch = proc do |branch|
shuffled_subs = []

branch.each do |key, unshuffled|
shuffled_subs << if key == :root
unshuffled # At this level, these are all duplicates.
else
shuffle_branch[unshuffled]
end
end

weighted_songs = []

shuffled_subs.each do |shuffled_songs|
shuffled_songs.each_with_index do |song, idx|
num = shuffled_songs.length.to_f
weight = (idx + rand) / num
weighted_songs << [song, weight]
end
end

weighted_songs.sort_by{|s,v| v}.map{|s,v| s}
end
shuffle_branch[tree]
end

# songs = ['aa/bbb', 'aa/ccc', 'aa/ddd',
# 'AAA/xxxx', 'AAA/yyyy', 'AAA/zzzz', 'foo/bar']
# puts(music_shuffle(songs))
10 changes: 9 additions & 1 deletion ch11-reading-and-writing/build_your_own_playlist.rb
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# your code here
all_oggs = shuffle(Dir['**/*.ogg'])

File.open 'playlist.m3u', 'w' do |f|
all_oggs.each do |ogg|
f.write ogg+"\n"
end
end

puts 'Done!'
Loading