From 5221688a6fbaeee039209ae7462ed2c6f6ec7ccc Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Tue, 6 Sep 2016 16:47:57 +0100 Subject: [PATCH 01/25] ask completed --- ch09-writing-your-own-methods/ask.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..b92a4d625 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,15 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + + if reply == "yes" + return true + elsif reply == "no" + return false + end + + puts "Please answer \"yes\" or \"no\"." + end + answer +end From e290711f973c6d021cb1974b0679ee6ae51e46c4 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Tue, 6 Sep 2016 17:23:32 +0100 Subject: [PATCH 02/25] roman numberals complete --- .../old_school_roman_numerals.rb | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index ca6589f2d..6bcf82f42 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,3 +1,19 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + x = "" + + x = x + "M" * (num / 1000) + x = x + "D" * (num % 1000 / 500) + x = x + "C" * (num % 500 / 100) + x = x + "L" * (num % 100 / 50) + x = x + "X" * (num % 50 / 10) + x = x + "V" * (num % 10 / 5) + x = x + "I" * (num % 5 / 1) + + puts x + +end + +old_roman_numeral 141 +old_roman_numeral 3000 +old_roman_numeral 6 From c1ab31c53ef08e37bfa0ecffe35856d6ebef6047 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 11:32:47 +0100 Subject: [PATCH 03/25] new roman numerals completed --- .../old_school_roman_numerals.rb | 8 ++-- .../roman_numerals.rb | 45 ++++++++++++++++++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index 6bcf82f42..23e270c90 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -10,10 +10,10 @@ def old_roman_numeral num x = x + "V" * (num % 10 / 5) x = x + "I" * (num % 5 / 1) - puts x + x end -old_roman_numeral 141 -old_roman_numeral 3000 -old_roman_numeral 6 +puts old_roman_numeral 141 +puts old_roman_numeral 3000 +puts old_roman_numeral 6 diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..333adbd51 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,44 @@ def roman_numeral num - # your code here -end \ No newline at end of file + + thou = (num / 1000) + hund = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10) + + x = "M" * thou + + if hund == 4 + x = x + "CD" + elsif hund == 9 + x = x + "CM" + else + x = x + "D" * (num % 1000 / 500) + x = x + "C" * (num % 500 / 100) + end + + if tens == 4 + x = x + "XL" + elsif tens == 9 + x = x + "XC" + else + x = x + "L" * (num % 100 / 50) + x = x + "X" * (num % 50 / 10) + end + + if ones == 4 + x = x + "IV" + elsif ones == 9 + x = x + "IX" + else + x = x + "V" * (num % 10 / 5) + x = x + "I" * (num % 5 / 1) + end + + x # returns roman numeral +end + +# test criteria below +puts (roman_numeral 4) +puts (roman_numeral 1949) +puts (roman_numeral 2654) +puts (roman_numeral 9) From 237f0feac2d8c1d0b95fb4063ad1754d1a14ac08 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 12:33:32 +0100 Subject: [PATCH 04/25] sort completed --- ch10-nothing-new/sort.rb | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..6571b1a1e 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,27 @@ def sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +def recursive_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + + small = unsorted.pop + + remain_unsorted = [] + + unsorted.each do | x | + if x < small + remain_unsorted.push small + small = x + else + remain_unsorted.push x + end + end + + sorted.push small + recursive_sort remain_unsorted, sorted +end + +puts(sort(["beans","Eggs","Brunch","apple","Acorn","badger"])) From 8e9cafbc0b3c174477cfc3395b3cd420b03ea38c Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 12:43:07 +0100 Subject: [PATCH 05/25] dictionary sort completed --- ch10-nothing-new/dictionary_sort.rb | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..85b45ff55 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,27 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +def recursive_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + + small = unsorted.pop + + remain_unsorted = [] + + unsorted.each do | x | + if x.downcase < small.downcase + remain_unsorted.push small + small = x + else + remain_unsorted.push x + end + end + + sorted.push small + recursive_sort remain_unsorted, sorted +end + +puts(dictionary_sort(["beans","Eggs","Brunch","apple","Acorn","badger"])) From 1d51b94997d1f71eedfb7092c06d8effe70863bd Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 13:26:13 +0100 Subject: [PATCH 06/25] shuffle completed --- ch10-nothing-new/shuffle.rb | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..566576ab1 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,26 @@ def shuffle arr - # your code here -end \ No newline at end of file + shuffled = [] + + while arr.length > 0 + rand_index = rand(arr.length) + index = 0 + new_arr = [] + + arr.each do |x| + if index == rand_index + shuffled.push x + else + new_arr.push x + end + + index = index + 1 + end + + arr = new_arr + end + + shuffled + +end + +puts(shuffle([1,2,3,4,5,6,7,8,9])) From be28fc5f0aa23ca1b84baa504a21539a7b8f6af5 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 14:37:02 +0100 Subject: [PATCH 07/25] English number completed --- ch10-nothing-new/english_number.rb | 116 ++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..103f2e88e 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,117 @@ 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 = '' # String to be returned. + + 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" is how much of the number we still have left to write out. + # "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 # How many zillions left? + left = left - write*zil_base # Subtract off those zillions. + + if write > 0 + # Recursion: + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end + end + + write = left/10 # How many tens left? + left = left - write*10 # Subtract off those tens. + + if write > 0 + if ((write == 1) and (left > 0)) + # Since we can't write "tenty-two" instead of + # "twelve", we have to make a special exception for these. + num_string = num_string + teenagers[left-1] + # Since we took care of the digit in the + # ones place already, we have nothing left to write. + left = 0 + else + num_string = num_string + tens_place[write-1] + end + + if left > 0 + # So we don't write 'sixtyfour'... + num_string = num_string + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + num_string = num_string + ones_place[write-1] + end + + # Now we just return "num_string"... + num_string end + +puts english_number( 0) +puts english_number( 9) +puts english_number( 10) +puts english_number( 11) +puts english_number( 17) +puts english_number( 32) +puts english_number( 88) +puts english_number( 99) +puts english_number(100) +puts english_number(101) +puts english_number(234) +puts english_number(3211) +puts english_number(999999) +puts english_number(1000000000000) +puts english_number(109238745102938560129834709285360238475982374561034) From d5524ff2136054829a65c7177ed80c1137016edc Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 14:53:54 +0100 Subject: [PATCH 08/25] 99 beers completed --- .../ninety_nine_bottles_of_beer.rb | 110 +++++++++++++++++- 1 file changed, 109 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..4f622a271 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,109 @@ -# your code here \ No newline at end of file +# English number: +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 + +# 99 Bottles Starts Here: +start_num = 9999 +current_num = start_num + +while current_num > 2 + puts english_number(current_num).capitalize + " bottles of beer on the wall" + + english_number(current_num) + " bottles of beer!" + current_num = current_num - 1 + puts "Take one down, pass it around, " + english_number(current_num) + + " 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!" From 57038aa21d7be4d3d050908f59a2dd35c52b0bc3 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 16:30:15 +0100 Subject: [PATCH 09/25] Safer pic downloading completed --- .../safer_picture_downloading.rb | 54 ++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..e4cc32333 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,53 @@ -# your code here \ No newline at end of file +# This is where she stores her pictures before +# she gets her YAML on and moves them to the server. +# Just for my own convenience, I'll go there now. + +Dir.chdir '/home/stevie/Test2' + +# First we find all of the pictures to be moved. +pic_names = Dir['/home/stevie/Test1/*.jpg'] + +puts 'What would you like to call this batch?' +batch_name = gets.chomp + +puts +print "Downloading #{pic_names.length} files: " + +# This will be our counter. We'll start at 1 today, +# though normally I like to count from 0. +pic_number = 1 + +pic_names.each do |name| + print '.' # This is our "progress bar". + + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}" + else + "#{batch_name}#{pic_number}" + end + + save_name = new_name + '.jpg' + while FileTest.exist? save_name + new_name += 'a' + save_name = new_name + '.jpg' + end + + # This renames the picture, but since "name" + # has a big long path on it, and "new_name" + # doesn't, it also moves the file to the + # current working directory, which is now + # Katy's PictureInbox folder. + # Since it's a *move*, this effectively + # downloads and deletes the originals. + # And since this is a memory card, not a + # hard drive, each of these takes a second + # or so; hence, the little dots let her + # know that my program didn't hose her machine. + + File.rename name, save_name + # Finally, we increment the counter. + pic_number = pic_number + 1 +end + +puts # This is so we aren't on progress bar line. +puts 'Done, cutie!' From ec4aaf6f382502413b05a4ac2943818d094f68af Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Wed, 7 Sep 2016 17:27:55 +0100 Subject: [PATCH 10/25] Build playlist completed --- .../build_your_own_playlist.rb | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..1d158f17b 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,36 @@ -# your code here \ No newline at end of file +# Shuffle method: +def shuffle arr + shuffled = [] + + while arr.length > 0 + rand_index = rand(arr.length) + index = 0 + new_arr = [] + + arr.each do |x| + if index == rand_index + shuffled.push x + else + new_arr.push x + end + + index = index + 1 + end + + arr = new_arr + end + + shuffled + +end + +# Playlist: +all_mp3s = shuffle(Dir['**/*.mp3']) + +File.open 'playlist.m3u', 'w' do |f| + all_mp3s.each do |mp3| + f.write mp3+"\n" + end +end + +puts "Complete!" From 0054d0102c5af68af4a897e6505012937d8aa4b1 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Thu, 8 Sep 2016 12:32:44 +0100 Subject: [PATCH 11/25] Better playlist --- .../build_a_better_playlist.rb | 50 ++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..edfcb8baf 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,51 @@ +# solution visited - fully understood + def music_shuffle filenames - # your code here + # We don't want a perfectly random shuffle, so let's + # instead do a shuffle like card-shuffling. Let's + # shuffle the "deck" twice, then cut it once. That's + # not enough times to make a perfect shuffle, but it + # does mix things up a bit. + # Before we do anything, let's actually *sort* the + # input, since we don't know how shuffled it might + # already be, and we don't want it to be *too* random. + filenames = filenames.sort + len = filenames.length + + # Now we shuffle twice. + 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 + # And cut the deck. + 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 +# songs = ['aa/bbb', 'aa/ccc', 'aa/ddd', +# 'AAA/xxxx', 'AAA/yyyy', 'AAA/zzzz', 'foo/bar'] +# puts(music_shuffle(songs)) From 3663c23d5748553cc44985e197aefe03b1fcd694 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Thu, 8 Sep 2016 12:48:55 +0100 Subject: [PATCH 12/25] One billion seconds --- ch12-new-classes-of-objects/one_billion_seconds.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..301d9f1d6 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,5 @@ -# your code here \ No newline at end of file +# I am this many seconds old: +puts((Time.new) - Time.gm(1988,9,28,13,58)) + +# I will be a billion seconds old on: +puts(Time.gm(1988,9,28,13,58) + 10**9) From ab1a21d27fd4bfb86ecee3f4f249b1d1fbc91577 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Thu, 8 Sep 2016 13:06:12 +0100 Subject: [PATCH 13/25] Happy birthday --- ch12-new-classes-of-objects/happy_birthday.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..ce6b560b5 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,15 @@ -# your code here \ No newline at end of file +puts "What year were you born?" +year = gets.chomp.to_i + +puts "What month were you born (1-12)?" +month = gets.chomp.to_i + +puts "And finally, on what day of that month?" +day = gets.chomp.to_i + +age = (Time.new - Time.local(year,month,day))/(60*60*24*365.25).to_i + +while age > 1 + puts "SPANK!" + age = age - 1 +end From 6aa4ed194941d1627b2e890b2716547b9213c62a Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Sun, 11 Sep 2016 18:58:27 +0100 Subject: [PATCH 14/25] party like its... --- ...party_like_its_roman_to_integer_mcmxcix.rb | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb b/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb index 037b6cb09..0531696e6 100644 --- a/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb +++ b/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb @@ -1,3 +1,30 @@ def roman_to_integer roman - # your code here -end \ No newline at end of file + 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 From 33017ad86df8d57941a05b47667d4587450c32ac Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Sun, 11 Sep 2016 21:07:28 +0100 Subject: [PATCH 15/25] Birthday helper --- .../birthday_helper.rb | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..16bcbcae3 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +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] = date +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 From 42de86e3b02338998e26d934ecd48385446bc9c7 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 04:21:06 +0100 Subject: [PATCH 16/25] built in classes --- .../extend_built_in_classes.rb | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..b297d5286 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,43 @@ class Integer - # your code here -end \ No newline at end of file + def roman_num + + thou = (self / 1000) + hund = (self % 1000 / 100) + tens = (self % 100 / 10) + ones = (self % 10) + + x = "M" * thou + + if hund == 4 + x = x + "CD" + elsif hund == 9 + x = x + "CM" + else + x = x + "D" * (self % 1000 / 500) + x = x + "C" * (self % 500 / 100) + end + + if tens == 4 + x = x + "XL" + elsif tens == 9 + x = x + "XC" + else + x = x + "L" * (self % 100 / 50) + x = x + "X" * (self % 50 / 10) + end + + if ones == 4 + x = x + "IV" + elsif ones == 9 + x = x + "IX" + else + x = x + "V" * (self % 10 / 5) + x = x + "I" * (self % 5 / 1) + end + + x # returns roman numeral + end +end + +puts 4.roman_num +puts 1999.roman_num From 4c72c814d51cbd842678d03bac7471a45d892711 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 05:05:22 +0100 Subject: [PATCH 17/25] Baby dragon --- .../interactive_baby_dragon.rb | 135 +++++++++++++++++- 1 file changed, 134 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..90cc9e59d 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,134 @@ -# your code here \ No newline at end of file +# Dragon Class from Chapter 13: +class Dragon + + def initialize name + @name = name + @asleep = false + @stuff_in_belly = 10 # He's full. + @stuff_in_intestine = 0 # He doesn't need to go. + + puts "#{@name} is born." + end + + def feed + puts "You feed #{@name}." + @stuff_in_belly = 10 + passage_of_time + end + + def walk + puts "You walk #{@name}." + @stuff_in_intestine = 0 + passage_of_time + end + + def put_to_bed + puts "You put #{@name} to bed." + @asleep = true + 3.times do + if @asleep + passage_of_time + end + if @asleep + puts "#{@name} snores, filling the room with smoke." + end + end + if @asleep + @asleep = false + puts "#{@name} wakes up slowly." + end + end + + def toss + puts "You toss #{@name} up into the air." + puts 'He giggles, which singes your eyebrows.' + passage_of_time + end + + def rock + puts "You rock #{@name} gently." + @asleep = true + puts 'He briefly dozes off...' + passage_of_time + if @asleep + @asleep = false + puts '...but wakes when you stop.' + end + end + + private + # "private" means that the methods defined here are + # methods internal to the object. (You can feed your + # dragon, but you can't ask him whether he's hungry.) + + def hungry? + # Method names can end with "?". + # Usually, we do this only if the method + # returns true or false, like this: + @stuff_in_belly <= 2 + end + + def poopy? + @stuff_in_intestine >= 8 + end + + def passage_of_time + if @stuff_in_belly > 0 + # Move food from belly to intestine. + @stuff_in_belly = @stuff_in_belly - 1 + @stuff_in_intestine = @stuff_in_intestine + 1 + else # Our dragon is starving! + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name} is starving! In desperation, he ate YOU!" + exit # This quits the program. + end + if @stuff_in_intestine >= 10 + @stuff_in_intestine = 0 + puts "Whoops! #{@name} had an accident..." + end + if hungry? + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name}'s stomach grumbles..." + end + if poopy? + if @asleep + @asleep = false + puts 'He wakes up suddenly!' + end + puts "#{@name} does the potty dance..." + end + end +end + +#Interactive Baby Dragon +puts 'What would you like to name your baby dragon?' +name = gets.chomp +pet = Dragon.new name + +while true + puts + puts 'commands: feed, toss, walk, rock, put to bed, exit' + command = gets.chomp.downcase # to eliminate case sensitivity + + if command == 'exit' + exit + elsif command == 'feed' + pet.feed + elsif command == 'toss' + pet.toss + elsif command == 'walk' + pet.walk + elsif command == 'rock' + pet.rock + elsif command == 'put to bed' + pet.put_to_bed + else + puts 'Huh? Please type one of the commands.' + end +end From f7fc7a8eae8dd778b5ca4e07e89fc7891b2d1cfc Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 05:54:33 +0100 Subject: [PATCH 18/25] Orange tree + built in classes modified to pass rspec --- .../extend_built_in_classes.rb | 52 ++++--------- ch13-creating-new-classes/orange_tree.rb | 75 ++++++++++++++++++- 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index b297d5286..c4cef265b 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,43 +1,23 @@ class Integer - def roman_num - thou = (self / 1000) - hund = (self % 1000 / 100) - tens = (self % 100 / 10) - ones = (self % 10) - - x = "M" * thou - - if hund == 4 - x = x + "CD" - elsif hund == 9 - x = x + "CM" - else - x = x + "D" * (self % 1000 / 500) - x = x + "C" * (self % 500 / 100) - end - - if tens == 4 - x = x + "XL" - elsif tens == 9 - x = x + "XC" - else - x = x + "L" * (self % 100 / 50) - x = x + "X" * (self % 50 / 10) - end + def to_roman + x = "" + x = x + "M" * (self / 1000) + x = x + "D" * (self % 1000 / 500) + x = x + "C" * (self % 500 / 100) + x = x + "L" * (self % 100 / 50) + x = x + "X" * (self % 50 / 10) + x = x + "V" * (self % 10 / 5) + x = x + "I" * (self % 5 / 1) + x + end - if ones == 4 - x = x + "IV" - elsif ones == 9 - x = x + "IX" + def factorial + if self <= 1 + 1 else - x = x + "V" * (self % 10 / 5) - x = x + "I" * (self % 5 / 1) + self * (self-1).factorial end - - x # returns roman numeral end -end -puts 4.roman_num -puts 1999.roman_num +end diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..c3330920f 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,7 +5,78 @@ # have the tree die after 25 years. # check out the rspec spec/ch13/orange_tree_spec.rb to see what strings we're looking for in the responses. - class OrangeTree - # your code here + def initialize + @height = 0 + @orange_count = 0 + @alive = true + end + + def height + if @alive + @height.round(1) + else + 'A dead tree is not very tall. :(' + end + end + + def count_the_oranges + if @alive + @orange_count + else + 'A dead tree has no oranges. :(' + end + end + + def one_year_passes + if @alive + @height = @height + 0.4 + @orange_count = 0 # old oranges fall off + if @height > 10 && rand(2) > 0 + # tree dies + @alive = false + 'Oh, no! The tree is too old, and has died. :(' + elsif @height > 2 + # new oranges grow + @orange_count = (@height * 15 - 25).to_i + "This year your tree grew to #{@height.round(1)}m tall," + + " and produced #{@orange_count} oranges." + else + "This year your tree grew to #{@height.round(1)}m tall," + + " but is still too young to bear fruit." + end + else + 'A year later, the tree is still dead. :(' + end + end + + def pick_an_orange + if @alive + if @orange_count > 0 + @orange_count = @orange_count - 1 + 'You pick a juicy, delicious orange!' + else + 'You search every branch, but find no oranges.' + end + else + 'A dead tree has nothing to pick. :(' + end + end end + + ot = OrangeTree.new + 23.times do + ot.one_year_passes + end + + puts(ot.one_year_passes) + puts(ot.count_the_oranges) + puts(ot.height) + puts(ot.one_year_passes) + puts(ot.one_year_passes) + puts(ot.one_year_passes) + puts(ot.one_year_passes) + puts(ot.one_year_passes) + puts(ot.height) + puts(ot.count_the_oranges) + puts(ot.pick_an_orange) From 87dc0912b62be82bbece8759a7cc4dd07f7dac44 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 06:43:04 +0100 Subject: [PATCH 19/25] Even better profiling --- ch14-blocks-and-procs/even_better_profiling.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..c158fe7c0 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,11 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + on = false + if on + start_time = Time.new + block.call + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end +end From 88918addc34ac64c609b8c6ca1908b99acd1d968 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 06:53:21 +0100 Subject: [PATCH 20/25] grandfather clock --- ch14-blocks-and-procs/grandfather_clock.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..f2da114c5 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,9 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + hr = (Time.new.hour + 11) % 12 + 1 + hr.times(&block) +end + +grandfather_clock { puts 'DONG!' } +grandfather_clock { puts 'BEEP!'} +grandfather_clock { puts 'AH-HA!' } +grandfather_clock { puts 'BOOM!' } From ddefdffc1467fceb3355e8e858c8f3873331c084 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 07:43:45 +0100 Subject: [PATCH 21/25] program logger --- ch14-blocks-and-procs/program_logger.rb | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..4ae2e5d43 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,15 @@ def log desc, &block - # your code here -end \ No newline at end of file + puts 'Beginning "' + desc + '"...' + x = block.call + puts '..."' + desc + '" finished, returning: ' + x.to_s +end + +log 'outer block' do + log 'some little block' do + 5 + end + log 'yet another block' do + 'I like Thai food!' + end + false +end From af4e86f79b9f31567f7087e03316433d2d1ef924 Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 08:05:01 +0100 Subject: [PATCH 22/25] Profiling set to true --- ch14-blocks-and-procs/even_better_profiling.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index c158fe7c0..125c68ece 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,5 +1,5 @@ def profile block_description, &block - on = false + on = true if on start_time = Time.new block.call From 84b269c872b85b00620a3371c3ec0817e6b242fc Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 08:16:56 +0100 Subject: [PATCH 23/25] better program logger --- .../better_program_logger.rb | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..ac47e8bf0 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,28 @@ +$nesting_depth = 0 + def log desc, &block - # your code here -end \ No newline at end of file + + indent = ' ' * $nesting_depth + + puts indent + 'Beginning "' + desc + '"...' + + $nesting_depth = $nesting_depth + 1 + x = block.call + + $nesting_depth = $nesting_depth - 1 + puts '..."' + desc + '" finished, returning: ' + x.to_s +end + +log 'outer block' do + log 'some little block' do + log 'teeny-tiny block' do + 'lots of love' + end + 42 +end + + log 'yet another block' do + 'I love Indian food!' + end + false +end From 72c33a5495d593862370ecd97fd75f34878b37ac Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 08:27:00 +0100 Subject: [PATCH 24/25] method names altered to suit rspec --- ch14-blocks-and-procs/better_program_logger.rb | 12 ++++++------ ch14-blocks-and-procs/program_logger.rb | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index ac47e8bf0..219532e28 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,6 +1,6 @@ $nesting_depth = 0 -def log desc, &block +def better_log desc, &block indent = ' ' * $nesting_depth @@ -10,18 +10,18 @@ def log desc, &block x = block.call $nesting_depth = $nesting_depth - 1 - puts '..."' + desc + '" finished, returning: ' + x.to_s + puts indent + '..."' + desc + '" finished, returning: ' + x.to_s end -log 'outer block' do - log 'some little block' do - log 'teeny-tiny block' do +better_log 'outer block' do + better_log 'some little block' do + better_log 'teeny-tiny block' do 'lots of love' end 42 end - log 'yet another block' do + better_log 'yet another block' do 'I love Indian food!' end false diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 4ae2e5d43..d1bccc320 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,14 +1,14 @@ -def log desc, &block +def program_log desc, &block puts 'Beginning "' + desc + '"...' x = block.call puts '..."' + desc + '" finished, returning: ' + x.to_s end -log 'outer block' do - log 'some little block' do +program_log 'outer block' do + program_log 'some little block' do 5 end - log 'yet another block' do + program_log 'yet another block' do 'I like Thai food!' end false From 0ee0d42320cb1f04557f7482c10edfa04877018b Mon Sep 17 00:00:00 2001 From: Stevie Winston-Gore Date: Mon, 12 Sep 2016 08:38:29 +0100 Subject: [PATCH 25/25] Orange tree solution added --- ch13-creating-new-classes/orange_tree.rb | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index c3330920f..92372fbdd 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -64,19 +64,19 @@ def pick_an_orange end end - ot = OrangeTree.new - 23.times do - ot.one_year_passes - end +# ot = OrangeTree.new +# 23.times do +# ot.one_year_passes +# end - puts(ot.one_year_passes) - puts(ot.count_the_oranges) - puts(ot.height) - puts(ot.one_year_passes) - puts(ot.one_year_passes) - puts(ot.one_year_passes) - puts(ot.one_year_passes) - puts(ot.one_year_passes) - puts(ot.height) - puts(ot.count_the_oranges) - puts(ot.pick_an_orange) +# puts(ot.one_year_passes) +# puts(ot.count_the_oranges) +# puts(ot.height) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.one_year_passes) +# puts(ot.height) +# puts(ot.count_the_oranges) +# puts(ot.pick_an_orange)