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 #490

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
14 changes: 12 additions & 2 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
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
14 changes: 12 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,13 @@
def old_roman_numeral num
# your code here
end

oldrom = ""

oldrom = oldrom + 'M' * (num / 1000)
oldrom = oldrom + 'D' * (num % 1000 / 500)
oldrom = oldrom + 'C' * (num % 500 / 100)
oldrom = oldrom + 'L' * (num % 100 / 50)
oldrom = oldrom +'X' * (num % 50 / 10)
oldrom = oldrom + 'V' * (num % 10 / 5)
oldrom = oldrom + 'I' * (num % 5 / 1)
oldrom
end
43 changes: 41 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,42 @@
def roman_numeral num
# your code here
end
#changes only to 4 and 9 in each tier

newrom = ""

thou = (num /1000)
hund = (num %1000 / 100)
ten = (num % 100 /10)
one = (num % 10)

newrom = 'M' * thou

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

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

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

puts roman_numeral 1992
12 changes: 10 additions & 2 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
def dictionary_sort arr
# your code here
end
return arr if arr.length <=1

middle = arr.pop
less = arr.select{|x| x.downcase < middle.downcase}
more = arr.select{|x| x.downcase >= middle.downcase}

dictionary_sort(less) + [middle] + dictionary_sort(more)
end

puts dictionary_sort ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
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 '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

puts english_number 25
puts english_number 0
puts english_number 1000000000
puts english_number 55986745241657
97 changes: 96 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,96 @@
# 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']

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

num_at_start = 10
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!"
26 changes: 24 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
def shuffle arr
# your code here
end
shuf = []
while arr.length > 0
rand_index = rand (arr.length)

curr_index = 0
new_arr = []

arr.each do |item|
if curr_index == rand_index
shuf.push item
else
new_arr.push item
end

curr_index = curr_index + 1
end

arr = new_arr
end

shuf
end

puts shuffle [10,11,12,13,14,15,16,17,18,19,20]
12 changes: 10 additions & 2 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
def sort arr
# your code here
end
return arr if arr.length <= 1

middle = arr.pop
less = arr.select{|x| x < middle}
more = arr.select{|x| x >= middle}

sort(less) + [middle] + sort(more)
end

puts(sort(['i', 'feel', 'like', 'dancing']).join(' '))
37 changes: 35 additions & 2 deletions ch11-reading-and-writing/build_a_better_playlist.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
def music_shuffle filenames
# your code here
end
filenames = filenames.sort
len = filenames.length

2.times do
l_index = 0
r_index = len/2
shuf = []

while shuf.length < len
if shuf.length%2 == 0
shuf.push(filenames[r_index])
r_index = r_index + 1
else
shuf.push(filenames[l_index])
l_index = l_index + 1
end
end

filenames = shuf
end

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

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

arr
end
songs = ['RayCharles/HitTheRoadJack', 'RayCharles/GeorgiaOnMyMind', 'RayCharles/IGotAWoman', 'ArethaFranklin/Respect', 'ArethaFranklin/NaturalWoman', 'ArethaFranklin/ISayALittlePrayer']

puts(music_shuffle(songs))
14 changes: 13 additions & 1 deletion ch11-reading-and-writing/build_your_own_playlist.rb
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# your code here
def shuffle arr
arr.shuffle
end

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