From 5660d29c35dc12d2de3cb47e6616dd30dd18cacb Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sat, 10 Sep 2016 21:23:38 +0000 Subject: [PATCH 01/27] make superficial alteration so can make pull request --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index da258f2fc..1084101c5 100644 --- a/README.md +++ b/README.md @@ -59,3 +59,5 @@ rspec ``` but be prepared for potentially quite a lot of output! :-) + +-- From 40b6702884da60a4fe512020dd91ca7c3ceb4af6 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 12:29:10 +0000 Subject: [PATCH 02/27] ask.rb exercise --- ch09-writing-your-own-methods/ask.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..a256da065 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,17 @@ def ask question - # your code here -end \ No newline at end of file + 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 + +choc = ask 'Do you like chocolate?' +puts choc + + From 1ad37ba7648b6c7c3cc5801eb8b204afc746cc99 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 12:36:22 +0000 Subject: [PATCH 03/27] Remove superficial changes from README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index 1084101c5..da258f2fc 100644 --- a/README.md +++ b/README.md @@ -59,5 +59,3 @@ rspec ``` but be prepared for potentially quite a lot of output! :-) - --- From 37587e7bfb45b709aef64c3631fdc21ed9718a38 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 12:38:56 +0000 Subject: [PATCH 04/27] Comment out calling method --- ch09-writing-your-own-methods/ask.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index a256da065..7dad4947d 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -11,7 +11,7 @@ def ask question answer end -choc = ask 'Do you like chocolate?' -puts choc +#puts(ask ('Do you like chocolate?')) + From 3e40cc1b3c4a3c16d4e9c873f8b217e0bbaa2254 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 13:37:34 +0000 Subject: [PATCH 05/27] old_school_roman_numerals.rb --- .../old_school_roman_numerals.rb | 18 ++++++++++++++++-- 1 file changed, 16 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..4b8d0c221 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,17 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + 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) + +end + + +#puts (old_roman_numeral(2679)) + From 374a769b44ba722c9f3da998db5e65078d0ec010 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 14:18:03 +0000 Subject: [PATCH 06/27] roman_numerals.rb --- .../roman_numerals.rb | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..afba9ba5a 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,39 @@ def roman_numeral num - # your code here -end \ No newline at end of file + thous = (num / 1000) + hunds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10) + + roman = 'M' * thous + + if hunds == 9 + roman = roman + 'CM' + elsif hunds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens ==4 + roman = roman + 'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end + roman +end + + +puts (roman_numeral(2999)) \ No newline at end of file From 079a85c3e3ef2e0b5ba624e611d15aa74f365280 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 20:10:55 +0000 Subject: [PATCH 07/27] dictionary_sort.rb --- ch10-nothing-new/dictionary_sort.rb | 31 +++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..3963fabe3 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,30 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + 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 + +veg = ['carrot', 'apple', 'Pear', 'banana', 'Leek'] + +puts dictionary_sort(veg) \ No newline at end of file From 6055c7e2abaa77c6f5515633e29cd7ef898d5a2a Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 21:31:37 +0000 Subject: [PATCH 08/27] shuffle.rb --- ch10-nothing-new/shuffle.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..2144b3cfc 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,7 @@ def shuffle arr - # your code here -end \ No newline at end of file + arr.shuffle +end + +veg = ['apple', 'banana', 'carrot', 'Leek', 'Pear',] + +puts(shuffle(veg)) From ec813793f0797291e1ab7bb1615e01f73462da73 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 21:37:17 +0000 Subject: [PATCH 09/27] english_number.rb --- ch10-nothing-new/english_number.rb | 95 +++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..c4ddc7082 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,96 @@ 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 + + numString = '' + + onesPlace = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + tensPlace = ['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 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end + end + + write = left/10 + left = left-write*10 + + if write > 0 + if ((write == 1) and (left == 0)) + numString = numString + teenagers[left-1] + left = 0 + else + numString = numString + tensPlace[write-1] + end + if left > 0 + numString = numString + ' ' + end + end + + + write = left + left = 0 + + if write > 0 + numString = numString + onesPlace[write-1] + end + + numString end + + +puts english_number (12) +puts english_number (1) +puts english_number (0) +puts english_number (27) +puts english_number (100) +puts english_number (512) +puts english_number(3211) +puts english_number(999999) +puts english_number(1000000000000) +puts english_number(109238745102938560129834709285360238475982374561034) + From b12d58a5701c81de920cb04a07ca8034874d7730 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 11 Sep 2016 21:50:16 +0000 Subject: [PATCH 10/27] ninety_nine_bottles_of_beer.rb --- .../ninety_nine_bottles_of_beer.rb | 130 +++++++++++++++++- 1 file changed, 129 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..49ecb91ef 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,129 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 # No negative numbers. + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end + + # No more special cases! No more returns! + + num_string = '' # This is the string we will return. + + 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. + # write and left...get it? :) + 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 + # Now here's the 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] + # The "-1" is because teenagers[3] is + # 'fourteen', not 'thirteen'. + + # 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] + # The "-1" is because tens_place[3] is + # 'forty', not 'thirty'. + 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] + # The "-1" is because ones_place[3] is + # 'four', not 'three'. + end + + # Now we just return "num_string"... + num_string +end + +num_at_start = 9999 # change to 9999 if you want +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!" From d17dba84a53af83d5642adb89259baefb5ac4986 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Mon, 12 Sep 2016 08:55:43 +0000 Subject: [PATCH 11/27] safer_picture_downloading.rb --- .../safer_picture_downloading.rb | 36 ++++++++++++++++++- 1 file changed, 35 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..188730b87 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,35 @@ -# your code here \ No newline at end of file +# your code here +Dir.chdir '/home/ubuntu/workspace/week2' + +pic_names = Dir['/home/ubuntu/workspace/week2/hedgehogs/**/*.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 '.' + + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" + end + + if File.exists?(new_name) + exit + else + File.rename name, new_name + end + +pic_number = pic_number + 1 +end + +puts +puts "All done!" + From ed70b99df0bc3a156f51559bef1278ac4395bcfb Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Tue, 20 Sep 2016 18:02:50 +0000 Subject: [PATCH 12/27] Add my examples folder to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9a2f2e013..8c0732b7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /**/.DS_Store +my_examples From b143cdd04758ff3611b119916c251d3d3791408a Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Tue, 20 Sep 2016 18:04:01 +0000 Subject: [PATCH 13/27] Complete build your own playlist exercise --- ch11-reading-and-writing/build_your_own_playlist.rb | 10 +++++++++- ch11-reading-and-writing/playlist.m3u | 0 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 ch11-reading-and-writing/playlist.m3u diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..814b94f2f 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,9 @@ -# your code here \ No newline at end of file +music = (Dir['/home/ubuntu/workspace/week2/music/**/*.mp3,MP3']).shuffle + +File.open '/home/ubuntu/workspace/week2/music/playlist.m3u', 'w' do |f| + music.each do |song| + f.write song+"\n" + end +end + +puts "Playlist created" \ No newline at end of file diff --git a/ch11-reading-and-writing/playlist.m3u b/ch11-reading-and-writing/playlist.m3u new file mode 100644 index 000000000..e69de29bb From f644644a250f615762f632fb54d41d214e7ecdf8 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Tue, 20 Sep 2016 19:32:36 +0000 Subject: [PATCH 14/27] Add curly braces to line 1 search so will search for mp3 and MP3, also remove brackets from around file path as this prevented program for searching for songs. --- ch11-reading-and-writing/build_your_own_playlist.rb | 2 +- 1 file changed, 1 insertion(+), 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 814b94f2f..09e514d88 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1,4 +1,4 @@ -music = (Dir['/home/ubuntu/workspace/week2/music/**/*.mp3,MP3']).shuffle +music = Dir['/home/ubuntu/workspace/week2/music/**/*.{mp3,MP3}'].shuffle File.open '/home/ubuntu/workspace/week2/music/playlist.m3u', 'w' do |f| music.each do |song| From 6d67ae8982d37e13ea7d10678c88f1c425708447 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Tue, 20 Sep 2016 19:52:26 +0000 Subject: [PATCH 15/27] Complete build a better playlist exercise --- .../build_a_better_playlist.rb | 33 ++++++++++++++++++- 1 file changed, 32 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..1970037e0 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,34 @@ 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 % 2 == 0 + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + shuf.push(filenames[l_idx]) + l_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 + +music = Dir['/home/ubuntu/workspace/week2/music/**/*.mp3'] + +puts (music_shuffle(music)) From e91e1333f41fc9c58c3b99eaa1570c28b480f52f Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Tue, 20 Sep 2016 20:19:57 +0000 Subject: [PATCH 16/27] Calculate when I will be one billion seconds old --- ch12-new-classes-of-objects/one_billion_seconds.rb | 7 ++++++- 1 file changed, 6 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..779bace44 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,6 @@ -# your code here \ No newline at end of file +# 19/06/1986, 03:30:21, GMT +birthday = Time.gm(1986,06,19, 3, 00, 22) + +one_billion = birthday + 1000000000 + +puts one_billion \ No newline at end of file From c65e629fee4ef9fd888c2c0d9fa43fa5ceaa90b2 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Tue, 20 Sep 2016 20:38:28 +0000 Subject: [PATCH 17/27] Complete happy_birthday.rb exercise --- ch12-new-classes-of-objects/happy_birthday.rb | 24 ++++++++++++++++++- 1 file changed, 23 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..786f13426 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +puts "What year were you born? (e.g. 1982)" +year = gets.chomp +puts "What month were you born? (e.g. if you were born in February, write '2')" +month = gets.chomp +puts "What day were you born?(e.g. if you were born on 19th of month, write '19')" +day = gets.chomp + +birthday = Time.gm(year, month, day) + +age_seconds = Time.new - birthday + +age_years = age_seconds/60/60/24/365 + +spank = age_years.to_i + +puts "Happy birthday! You are #{spank} years old!" + +spank.times do + puts "SPANK!" +end + +#puts birthday +#puts Time.new \ No newline at end of file From 13c4e88dcf4a2a903536bfd05aa0396d4f2bf21d Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Thu, 22 Sep 2016 15:09:42 +0000 Subject: [PATCH 18/27] Write program that converts roman numerals to integers --- ...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..9c4879ee0 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 + roman.upcase! + digits = {'M' => 1000, + 'D' => 500, + 'C' => 100, + 'L' => 50, + 'X' => 10, + 'V' => 5, + 'I' => 1} + + total = 0 + prev = 0 + roman.reverse.each_char do |char| + if !digits.has_key?(char) + puts "Please enter a valid roman numeral" + else + val = digits[char] + if val < prev + val *= -1 + else + prev = val + end + end + total += val + end + total +end + +puts roman_to_integer("mcmxcix") +puts roman_to_integer('MMXiv') \ No newline at end of file From 79dd5a77ab0590c5bc4100bfc898c0966101d702 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sat, 24 Sep 2016 17:48:30 +0000 Subject: [PATCH 19/27] birthday_helper.rb --- .../birthday_helper.rb | 20 ++++++++++++++++++- 1 file changed, 19 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..db7253677 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,19 @@ -# your code here \ No newline at end of file +#read in file and convert to hash +@chrises = {} +file = File.open("../../chris_bday.txt", "r") +file.readlines.each do |line| +name, date, year = line.chomp.split(',') +@chrises [name.to_s] = date.to_s + year.to_s +end +file.close + + +#get input from user +puts "Whose birthday are you looking for?" +chris = gets.chomp + +if !@chrises.has_key?(chris) + puts "I'm sorry but I don't know that person's date of birth" +else + puts "#{chris}'s birthday will be on#{@chrises[chris][0..-6]}." +end From b8a9a7865b6706366a7e3b85482eb7785c864d7a Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sat, 24 Sep 2016 18:13:05 +0000 Subject: [PATCH 20/27] exted_build_in_classes.rb --- .../extend_built_in_classes.rb | 43 ++++++++++++++++++- 1 file changed, 41 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..864ba4114 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,42 @@ class Integer - # your code here -end \ No newline at end of file + def to_roman_numeral + thous = (self / 1000) + hunds = (self % 1000 / 100) + tens = (self % 100 / 10) + ones = (self % 10) + + roman = 'M' * thous + + if hunds == 9 + roman = roman + 'CM' + elsif hunds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (self % 1000 / 500) + roman = roman + 'C' * (self % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens ==4 + roman = roman + 'XL' + else + roman = roman + 'L' * (self % 100 / 50) + roman = roman + 'X' * (self % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (self % 10 / 5) + roman = roman + 'I' * (self % 5 / 1) + end + roman +end +end + +puts 12.to_roman_numeral +puts 2016.to_roman_numeral +puts 735.to_roman_numeral \ No newline at end of file From 0ac9a7b2623722ecc6e83da592a191ec19be357a Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sat, 24 Sep 2016 21:23:05 +0000 Subject: [PATCH 21/27] orange_tree.rb --- ch13-creating-new-classes/orange_tree.rb | 75 +++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..a7f25d326 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,78 @@ 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 + if @height > 10 && rand(2) > 0 + @alive = false + 'Oh, no! The tree is too old, and has died. :(' + elsif @height > 2 + @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)}, tall, but is still to 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.pick_an_orange) +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 f43c214a887783191e4ca2d7df8be02d6b1e97e8 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 25 Sep 2016 12:05:24 +0000 Subject: [PATCH 22/27] interactive_baby_dragon.rb --- .../interactive_baby_dragon.rb | 122 +++++++++++++++++- 1 file changed, 121 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..3a2d9a450 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,121 @@ -# your code here \ No newline at end of file +class Dragon + + def initialize name + @name = name + @asleep = false + @stuff_in_belly = 10 + @stuff_in_intestine = 0 + + 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 + + def hungry? + @stuff_in_belly <= 2 + end + + def poopy? + @stuff_in_intestine >= 8 + end + + def passage_of_time + if @stuff_in_belly > 0 + @stuff_in_belly = @stuff_in_belly - 1 + @stuff_in_intestine = @stuff_in_intestine + 1 + else + if @asleep + @asleep = false + puts "He wakes up suddenly!" + end + puts "#{@name} is starving! In desperation, he ate YOU!" + exit + 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 + +puts "What would you like to call your baby dragon?" +name = gets.chomp +pet = Dragon.new name + +while true + puts "commands: feed, walk, rock, toss, put to bed, exit" + command = gets.chomp + + case command + when "feed" then pet.feed + when "walk" then pet.walk + when "rock" then pet.rock + when "toss" then pet.toss + when "put to bed" then pet.put_to_bed + when "exit" then exit +else puts "Please type a valid command" + end +end From 8eabc8d20da34c18fe953bf962799bd0b99fad44 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 25 Sep 2016 13:28:06 +0000 Subject: [PATCH 23/27] even_better_profiling.rb --- .../even_better_profiling.rb | 27 +++++++++++++++++-- 1 file changed, 25 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..e9b9c3a1d 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,26 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + profiling = false #set to true to do profiling + if profiling + start_time = Time.new + block.call + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end +end + +profile '25000 doublings' do + number = 1 + 25000.times do + number = number + number + end + puts "#{number.to_s.length} digits" +end + +profile 'count to a million' do + number = 0 + 1000000.times do + number = number + 1 + end +end From 0d595458aa247e21a7b82beba71b358f629d89ab Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 25 Sep 2016 13:43:30 +0000 Subject: [PATCH 24/27] grandfather_clock.rb --- ch14-blocks-and-procs/grandfather_clock.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..cebcd73d8 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,17 @@ def grandfather_clock &block - # your code here + hours = Time.new.hour + if hours <= 12 + hours.times do + block.call + end + else + hours = hours - 12 + hours.times do + block.call + end + end +end + +grandfather_clock do + puts "DONG!" end \ No newline at end of file From 56d8c30acc097e135f5a40715c561c40ff8b8699 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 25 Sep 2016 14:27:55 +0000 Subject: [PATCH 25/27] program_logger.rb --- ch14-blocks-and-procs/program_logger.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..92381134d 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,16 @@ def log desc, &block - # your code here + puts "Beginning '#{desc}'" + output = block.call + puts "...'#{desc}' finished, returning: \n#{output}" + +end + +log "outer block" do + log "some little block" do + 5 + end + log "yet another block" do + "I like Thai food!" + end + false end \ No newline at end of file From 86bf93cbef1343321bc010107bf643447392bad4 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 25 Sep 2016 14:41:04 +0000 Subject: [PATCH 26/27] better_program_logger.rb --- .../better_program_logger.rb | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..c6e9b5b0a 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,24 @@ +$log_depth = 0 + def log desc, &block - # your code here + prefix = ' ' * $log_depth + puts prefix + "Beginning '#{desc}'..." + $log_depth = $log_depth + 1 + output = block.call + $log_depth = $log_depth - 1 + puts prefix + "...'#{desc}' finished, returning: #{output}" + +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 like Thai food!" + end + false end \ No newline at end of file From 21012b454d511fb2c432c3a111ce2f8cc03784f4 Mon Sep 17 00:00:00 2001 From: Elizabeth Venner Date: Sun, 25 Sep 2016 15:35:36 +0000 Subject: [PATCH 27/27] sort.rb --- ch10-nothing-new/sort.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..ca89cfcce 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,16 @@ def sort arr - # your code here -end \ No newline at end of file + 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 + +fruit = ['pear', 'apple', 'grape', 'banana'] +veg = ['carrot'] + +p sort(fruit) +p sort([]) +p sort(veg) \ No newline at end of file