Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
8a30391
def ask question
laurent-bouhnik-gh Sep 5, 2016
ee514da
def old_school_roman_numeral
laurent-bouhnik-gh Sep 5, 2016
28c3e50
def old_school_roman_numeral
laurent-bouhnik-gh Sep 5, 2016
afaadc3
def roman numeral
laurent-bouhnik-gh Sep 5, 2016
8e4c53e
def shuffle
laurent-bouhnik-gh Sep 6, 2016
5c60426
dictionary sort
laurent-bouhnik-gh Sep 7, 2016
edc7762
english number
laurent-bouhnik-gh Sep 7, 2016
b82daca
sort
laurent-bouhnik-gh Sep 7, 2016
4afec0b
ninety_nine_bottles_of_beer
laurent-bouhnik-gh Sep 8, 2016
cd2cbc7
safer_picture_downloading
laurent-bouhnik-gh Sep 8, 2016
a2b72a6
build_your_own_playlist
laurent-bouhnik-gh Sep 8, 2016
e594fe6
one_billion_seconds
laurent-bouhnik-gh Sep 8, 2016
e45428b
happy_birthday
laurent-bouhnik-gh Sep 8, 2016
9d15daf
ask
laurent-bouhnik-gh Sep 8, 2016
f66992c
final happy_birthday
laurent-bouhnik-gh Sep 9, 2016
7a12057
party_like_its_roman_to_integer_mcmxcix
laurent-bouhnik-gh Sep 9, 2016
6b72123
birthdate & birthday_helper
laurent-bouhnik-gh Sep 9, 2016
d7a7970
extend_built_in_classes
laurent-bouhnik-gh Sep 9, 2016
91a6999
final happy_birthday
laurent-bouhnik-gh Sep 10, 2016
0c39985
interactive baby dragon
laurent-bouhnik-gh Sep 10, 2016
e1b03bc
even_better_profiling
laurent-bouhnik-gh Sep 10, 2016
8b50b71
grand father clock
laurent-bouhnik-gh Sep 10, 2016
0bd7363
program logger
laurent-bouhnik-gh Sep 10, 2016
7baf849
better program logger
laurent-bouhnik-gh Sep 10, 2016
9797ab2
built a better playlist
laurent-bouhnik-gh Sep 10, 2016
e56f177
orange tree final
laurent-bouhnik-gh Sep 10, 2016
84d9e78
resal final orange tree
laurent-bouhnik-gh Sep 10, 2016
0feaa4e
real final orange tree
laurent-bouhnik-gh Sep 10, 2016
8ca896a
final extend the build in class
laurent-bouhnik-gh Sep 10, 2016
86bda90
final even better profiling
laurent-bouhnik-gh Sep 10, 2016
c5216cf
program logger
laurent-bouhnik-gh Sep 10, 2016
2c6cd4b
real final better logger
laurent-bouhnik-gh Sep 10, 2016
a05bd29
better programme logger
laurent-bouhnik-gh Sep 10, 2016
6101c09
change name of function
laurent-bouhnik-gh Sep 11, 2016
2a3969c
copying the solution to hopefully pass the test
laurent-bouhnik-gh Sep 11, 2016
c7ad0c9
better log
laurent-bouhnik-gh Sep 12, 2016
8b70fae
better log
laurent-bouhnik-gh Sep 12, 2016
9305ca4
program logger
laurent-bouhnik-gh Sep 12, 2016
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
15 changes: 12 additions & 3 deletions ch09-writing-your-own-methods/ask.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
def ask question
# your code here
end
def ask question
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
answer
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

thousand = 'M' * (num / 1000)
fivehundreds = thousand + 'D' * (num % 1000 / 500)
hundreds = fivehundreds + 'C' * (num % 500 / 100)
fifties = hundreds + 'L' * (num % 100 / 50)
tenth = fifties + 'X' * (num % 50 / 10)
fifth = tenth + 'V' * (num % 10 / 5)
units = fifth + 'I' * (num % 5 / 1)

units

end
37 changes: 35 additions & 2 deletions ch09-writing-your-own-methods/roman_numerals.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
def roman_numeral num
# your code here
end
thousand = num / 1000
hundreds = num % 1000 / 100
tenth = num % 100 / 10
units = num % 10

roman = 'M' * thousand

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

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

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

20 changes: 19 additions & 1 deletion 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
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_object|
if tested_object.downcase < smallest.downcase
still_unsorted.push smallest
smallest = tested_object
else
still_unsorted.push tested_object
end
end
sorted.push smallest
rec_dict_sort still_unsorted, sorted
end
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', '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
11 changes: 10 additions & 1 deletion ch10-nothing-new/ninety_nine_bottles_of_beer.rb
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# your code here
num_at_start = 9999
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
19 changes: 18 additions & 1 deletion ch10-nothing-new/shuffle.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
def shuffle arr
# your code here
final_array = []
while arr.length > 0
rand_index = rand(arr.length)
intial_index = 0
array = []
arr.each do |w|
if intial_index == rand_index
final_array.push(w)
else
array.push(w)
end

intial_index = intial_index + 1
end
arr = array
end

return final_array
end
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
def sort arr
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
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

2.times do
l_idx = 0 # index of next card in left pile
r_idx = len/2 # index of next card in right pile
shuf = []
# NOTE: If we have an odd number of "cards",
# then the right pile will be larger.

while shuf.length < len
if shuf.length%2 == 0
# take card from right pile
shuf.push(filenames[r_idx])
r_idx = r_idx + 1
else
# take card from left pile
shuf.push(filenames[l_idx])
l_idx = l_idx + 1
end
end

filenames = shuf
end

arr = []
cut = rand(len) # index of card to cut at
idx = 0

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

arr
end

7 changes: 6 additions & 1 deletion ch11-reading-and-writing/build_your_own_playlist.rb
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# your code here
musics = shuffle(Dir['**/*.mp3'])
File.open 'playlist.m3u', 'w' do |f|
musics.each do |mp3|
f.write mp3+"\n"
end
end
25 changes: 24 additions & 1 deletion ch11-reading-and-writing/safer_picture_downloading.rb
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
# your code here
Dir.chdir 'C:\Users\Laurent\Downloads'
pic_names = Dir['*.jpg']

puts 'What would you like to call this batch?'
batch_name = gets.chomp
puts
print "Downloading #{pic_names.length} files: "

pic_number = 1
pic_names.each do |name|
print '.'
if pic_number < 10
new_name = "#{batch_name}0#{pic_number}.jpg"
else
new_name = "#{batch_name}#{pic_number}.jpg"
end

if File.exist?(new_name) == false
File.rename name, new_name
elsif File.exist?(new_name) == true
exit
end
pic_number = pic_number + 1
end
8 changes: 8 additions & 0 deletions ch12-new-classes-of-objects/birthdates.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Christopher Alexander, Oct 4, 1936
Christopher Lambert, Mar 29, 1957
Christopher Lee, May 27, 1922
Christopher Lloyd, Oct 22, 1938
Christopher Pine, Aug 3, 1976
Christopher Plummer, Dec 13, 1927
Christopher Walken, Mar 31, 1943
The King of Spain, Jan 5, 1938
15 changes: 14 additions & 1 deletion ch12-new-classes-of-objects/birthday_helper.rb
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# your code here
birth_dates = {}
File.read('birthdates.txt').each_line do |line|
line = line.chomp
comma = line.index(',')
$name = line[0..(comma - 1)]
$dob = line[-12..-1]
birth_dates[$name] = $dob
end
puts "Enter a name"
name = gets.chomp
puts (birth_dates[name])[0..5]



24 changes: 23 additions & 1 deletion ch12-new-classes-of-objects/happy_birthday.rb
Original file line number Diff line number Diff line change
@@ -1 +1,23 @@
# your code here
puts "Enter your birth year in the following format: YYYY"
year = gets.chomp.to_i
puts "Enter your birth month in the following format: MM"
month = gets.chomp.to_i
puts "Enter your birth day in the following format: DD"
day = gets.chomp.to_i

y = Time.new.year - year
m = Time.new.month - month
d = Time.new.day - day
if m - month > 0
puts "You are #{y}years old"
puts "SPANK!" * y
elsif m -month == 0 and d - day >= 0
puts "You are #{y}years old"
puts "SPANK!" * y
elsif m -month == 0 and d - day < 0
puts "You are #{y-1}years old"
puts "SPANK!" * (y-1)
elsif m-month < 0
puts "You are #{y-1}years old"
puts "SPANK!" * (y-1)
end
3 changes: 2 additions & 1 deletion ch12-new-classes-of-objects/one_billion_seconds.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# your code here
billion_seconds = (Time.local(1976, 8, 15, 21) + 3600) + 1000000000
puts billion_seconds
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
def roman_to_integer roman
# your code here
digit_vals = {'i' => 1,
'v' => 5,
'x' => 10,
'l' => 50,
'c' => 100,
'd' => 500,
'm' => 1000}
total = 0
prev = 0
index = roman.length - 1
while index >= 0
c = roman[index].downcase
index = index - 1
val = digit_vals[c]
if !val
puts 'This is not a valid roman numeral!'
return
end

if val < prev
val = val * -1
else
prev = val
end
total = total + val
end

total
end
Loading