Skip to content
12 changes: 11 additions & 1 deletion 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
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
puts "Oops! Try using a positive integer!" if num <= 0
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)

return roman
end
31 changes: 29 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def roman_numeral num
# your code here
end
roman = ""
roman = roman + "M" * (num / 1000)
if (900..999) === (num % 1000)
roman = roman + "CM"
elsif (400..499) === (num % 500)
roman = roman + "CD"
else
roman = roman + "D" * ((num % 1000) / 500)
roman = roman + "C" * ((num % 500) / 100)
end
if (90..99) === (num % 100)
roman = roman + "XC"
elsif
(40..49) === (num % 50)
roman = roman + "XL"
else
roman = roman + "L" * ((num % 100) / 50)
roman = roman + "X" * ((num % 50) / 10)
end
if (num % 10) == 9
roman = roman + "IX"
elsif (num % 5) == 4
roman = roman + "IV"
else
roman = roman + "V" * ((num % 10) / 5)
roman = roman + "I" * ((num % 5) / 1)
end
roman
end
24 changes: 24 additions & 0 deletions ch10-nothing-new/Rite_passage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
def some_array
recursive_sort some_array, []
end

def recursive_sort unsorted_array, sorted_array
if unsorted.length <= 0
return "Sorted"
end

smallest = unsorted_array.pop
still_unsorted =[]

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

sorted_array.push smallest

end
22 changes: 20 additions & 2 deletions ch10-nothing-new/dictionary_sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
def dictionary_sort arr
# your code here
end
rec_dict_sort arr, []
end
def rec_dict_sort unsorted, sorted
if unsorted.length <= 0
return sorted
end
smallest = unsorted.pop
still_unsorted = []

unsorted.each do |tested|
if tested.downcase < smallest.downcase
still_unsorted.push smallest
smallest = tested
else
still_unsorted.push tested
end
end
sorted.push smallest
rec_dict_sort still_unsorted, sorted
end
57 changes: 56 additions & 1 deletion ch10-nothing-new/english_number.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
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
15 changes: 14 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# your code here
num_at_start = 5
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!"
25 changes: 23 additions & 2 deletions ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
def shuffle arr
# your code here
end
shuffled = []
while arr.length > 0
rand_index = rand(arr.length)

curr_index = 0
new_array =[]

arr.each do |item|
if curr_index == rand_index
shuffled.push item
else
new_array.push item
end

curr_index = curr_index + 1
end

arr = new_array
end
shuffled
end

puts shuffle [1, 2, 3, 4, 5, 6, 7]
25 changes: 23 additions & 2 deletions ch10-nothing-new/sort.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
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 |tested_object|
if tested_object < smallest
still_unsorted.push smallest
smallest = tested_object
else
still_unsorted.push tested_object
end
end

sorted.push smallest
rec_sort still_unsorted, sorted
end
47 changes: 46 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,48 @@
def music_shuffle filenames
# your code here
filenames = filenames.sort
len = filenames.length

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

while shuf.length < len
if shuf.length % == 0
shuf.push(filenames[r_idx])
r_idx = r_idx + 1
else
shuf.push(filenames[r_idx])
r_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













end
end
8 changes: 7 additions & 1 deletion ch11-reading-and-writing/build_your_own_playlist.rb
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# your code here
# your code here
songs = shuffle(Dir["**/*.mp3"])
File.open 'playlist.m3u', 'w' do |f|
songs.each do |mp3|
f.write mp3 + "/n"
end
end
24 changes: 23 additions & 1 deletion ch11-reading-and-writing/safer_picture_downloading.rb
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# your code here
pic_names = Dir["**/*.{JPG,jpg}"]
puts "How would you want to call this batch?"
batch_name = gets.chomp
puts
puts "Downloading #{pic_names.length} files"
pic_number = 1
pic_names.each do |name|
print "."
new_name = if pic_number < 10
"#{batch_name}0#{pic_number}.jpg"
else
"#{batch_name}#{pic_number}.jpg"
end
if File.exist?(new_name) == true
puts "Sorry, but you would be overwriting a picture!"
exit
else
File.rename name, new_name
pic_number = pic_number + 1
end
end

puts "Your batch have been saved with the following name: #{batch_name}!"
24 changes: 23 additions & 1 deletion ch12-new-classes-of-objects/birthday_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# your code here
birth_dates = {}
File.read("birthdates.txt").each_line do |line|
line = line.chomp

first_comma = 0
while line[first_comma] != "," && first_comma < line.length
first_comma = first_comma + 1
end

name = line[0..(first_comma - 1)]
date = line[-12..-1]
birth_dates[name] = name
end

puts "Whose birthday would you like to know?"
name = gets.chomp
date = birth_dates[name]

if date == nil
puts "Oooh, I don't know that one..."
else
puts date [0..5]
end
17 changes: 17 additions & 0 deletions ch12-new-classes-of-objects/happy_birthday.rb
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
puts "Which year did you get born?"
year = gets.chomp.to_i
puts "Which month did you get born? Please give it in a numerical way (1-12)"
month = gets.chomp.to_i
puts "Which day did you get born?"
day = gets.chomp.to_i


birthday = Time.local(year, month, day)
older = 1
now = Time.new


while Time.local(year + older, month, day) < now
puts "PRANK!!"
older = older + 1
end
# your code here
5 changes: 4 additions & 1 deletion ch12-new-classes-of-objects/one_billion_seconds.rb
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# your code here
time = Time.gm(1989, 5, 18)
time2 = time + 1000000000
puts time
puts time2
Loading