From 951ace1725d097e16c34428b714157ec7b0a4c69 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Fri, 9 Sep 2016 17:36:07 +0000 Subject: [PATCH 01/28] Chapter_9_Exercise1 --- Chapter_9_Exercise1 | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Chapter_9_Exercise1 diff --git a/Chapter_9_Exercise1 b/Chapter_9_Exercise1 new file mode 100644 index 000000000..e69de29bb From 2ca9e0fb79783ddbbedcdf8fe01f6b1bbf0f2916 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 11:52:55 +0000 Subject: [PATCH 02/28] ch09-writing-your-own-methods/ --- ch09-writing-your-own-methods/ask.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..832596dae 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,18 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + + if (reply == 'yes' || reply == 'no') + if reply == 'yes' + answer = true + else + answer = false + end + break + else + puts 'Please answer "yes" or "no". ' + end + end +end +load './ask.rb' From 38f0cc99edb93a2da77020c35177b555bfae06ad Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 12:29:29 +0000 Subject: [PATCH 03/28] ch09-writing-your-own-methods/ --- .../old_school_roman_numerals.rb | 14 ++++++++++++-- 1 file changed, 12 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..a6efbbde3 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,13 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + roman = '' + + roman = roman + 'M' * (num / 1000) + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + roman = roman + 'L' * (num % 100/ 50) + roman = roman + 'X' * (num % 50/ 10) + roman = roman + 'V' * (num % 10/ 5) + roman = roman + 'I' * (num % 5/ 1) + roman +end +puts(old_roman_numeral(1999)) From 82a8138c876dd6c0bef29b62503f899fd33fc0e5 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 13:13:25 +0000 Subject: [PATCH 04/28] ch09-writing-your-own-methods/ --- .../roman_numerals.rb | 39 ++++++++++++++++++- 1 file changed, 37 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..d2d45e29b 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,38 @@ def roman_numeral num - # your code here -end \ No newline at end of file + thousands = (num / 1000) + hundreds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10 ) + + roman = 'M' * thousands + 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 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(1999)) From f6d0c6367847b65ae281bf943de0f71427433049 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 13:52:00 +0000 Subject: [PATCH 05/28] Recursion --- ch10-nothing-new/Recursion | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 ch10-nothing-new/Recursion diff --git a/ch10-nothing-new/Recursion b/ch10-nothing-new/Recursion new file mode 100644 index 000000000..e69de29bb From 16ffe51362b8721611b68a72d2df53162814cdd8 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 16:32:49 +0000 Subject: [PATCH 06/28] sort.txt --- ch10-nothing-new/sort.rb | 3 --- ch10-nothing-new/sort.txt | 28 ++++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) delete mode 100644 ch10-nothing-new/sort.rb create mode 100644 ch10-nothing-new/sort.txt diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb deleted file mode 100644 index 44c6deb58..000000000 --- a/ch10-nothing-new/sort.rb +++ /dev/null @@ -1,3 +0,0 @@ -def sort arr - # your code here -end \ No newline at end of file diff --git a/ch10-nothing-new/sort.txt b/ch10-nothing-new/sort.txt new file mode 100644 index 000000000..de1d0b1cd --- /dev/null +++ b/ch10-nothing-new/sort.txt @@ -0,0 +1,28 @@ +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 + +puts(sort(['I can', 'feel', 'singing', 'like', 'a', 'can'])) + From 3dca9365ee0debff94eb279dde7fe444623886c3 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 20:32:25 +0000 Subject: [PATCH 07/28] ch10-nothing-new/ --- ch10-nothing-new/shuffle.rb | 3 --- ch10-nothing-new/shuffle.txt | 25 +++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) delete mode 100644 ch10-nothing-new/shuffle.rb create mode 100644 ch10-nothing-new/shuffle.txt diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb deleted file mode 100644 index a486ad94c..000000000 --- a/ch10-nothing-new/shuffle.rb +++ /dev/null @@ -1,3 +0,0 @@ -def shuffle arr - # your code here -end \ No newline at end of file diff --git a/ch10-nothing-new/shuffle.txt b/ch10-nothing-new/shuffle.txt new file mode 100644 index 000000000..319d4e4a4 --- /dev/null +++ b/ch10-nothing-new/shuffle.txt @@ -0,0 +1,25 @@ +def shuffle arr + shuf = [] + while arr.length > 0 + + curr_index = 0 + new_arr = [] + + arr.each do |item| + if curr_index == rand_index + shuf.push item + else + new_arr.push item + end + + curr_index = curr_index + 1 + end + + arr = new_arr + end + + shuf + end + + puts(shuffle([1,2,3,4,5,6,7,8,9])) + \ No newline at end of file From 96c6dd2483f70c7bc7544ab3330f25a7513bfc79 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 20:35:17 +0000 Subject: [PATCH 08/28] shuffle.rb --- ch10-nothing-new/shuffle.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch10-nothing-new/shuffle.rb diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb new file mode 100644 index 000000000..319d4e4a4 --- /dev/null +++ b/ch10-nothing-new/shuffle.rb @@ -0,0 +1,25 @@ +def shuffle arr + shuf = [] + while arr.length > 0 + + curr_index = 0 + new_arr = [] + + arr.each do |item| + if curr_index == rand_index + shuf.push item + else + new_arr.push item + end + + curr_index = curr_index + 1 + end + + arr = new_arr + end + + shuf + end + + puts(shuffle([1,2,3,4,5,6,7,8,9])) + \ No newline at end of file From c2c90f6df68d3f1a250032db74484b0ec0b8c3d9 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 20:51:52 +0000 Subject: [PATCH 09/28] dictionary_sort.rb --- ch10-nothing-new/dictionary_sort.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..c8453168f 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,24 @@ 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 sortedend + + 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 + + puts(dictionary_sort(['can', 'feel', 'singing.', 'like', 'A', 'can'])) \ No newline at end of file From 36a2973a2caaf115b01efb0892bfdc932de4c53c Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 22:25:41 +0000 Subject: [PATCH 10/28] english_number.rb --- ch10-nothing-new/english_number.rb | 71 +++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..090c1a13d 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,72 @@ def english_number number - # your code here + if number < 0 + return 'Please enter a number that is not 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], ['nontillion', 30], ['decillion', 33], ['undecillion', 36], ['duodecillion', 39], ['tredecillion', 42], ['quattuordecillion', 45], ['quindecillion', 48], ['sexdecillion', 51], ['septendecillion', 54], ['octodecillion', 57], ['novemdecillion', 60], ['vigintillion', 63], ['googol', 100]] +left = number + +while zillions.length > 0 +zil_pair = zillions.pop +zil_name = zil_pair[0] +zil_base = 10 ** zil_pair[1] +write = left/zil_base +left = left - write*zil_base + +if write > 0 + prefix = english_number write + num_string = num_string + prefix + '' + zil_name + + if left > 0 + num_string = num_string + '' + end +end +end +write = left/10 +left = left - write*10 + +if write > 0 + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + + if left > 0 + num_string = num_string + '-' + end +end + +write = left +left = 0 + +if write > 0 + num_string = num_string + ones_place[write-1] +end +num_string +end + +puts english_number(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(62819273644748393029282) From 3ceb18d0de42dfafb063fc6c284f7c93f78c3e70 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 22:30:28 +0000 Subject: [PATCH 11/28] english_number.rb --- ch10-nothing-new/english_number.txt | 72 +++++++++++++++++++++++++++++ ch10-nothing-new/shuffle.txt | 25 ---------- 2 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 ch10-nothing-new/english_number.txt delete mode 100644 ch10-nothing-new/shuffle.txt diff --git a/ch10-nothing-new/english_number.txt b/ch10-nothing-new/english_number.txt new file mode 100644 index 000000000..090c1a13d --- /dev/null +++ b/ch10-nothing-new/english_number.txt @@ -0,0 +1,72 @@ +def english_number number + if number < 0 + return 'Please enter a number that is not 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], ['nontillion', 30], ['decillion', 33], ['undecillion', 36], ['duodecillion', 39], ['tredecillion', 42], ['quattuordecillion', 45], ['quindecillion', 48], ['sexdecillion', 51], ['septendecillion', 54], ['octodecillion', 57], ['novemdecillion', 60], ['vigintillion', 63], ['googol', 100]] +left = number + +while zillions.length > 0 +zil_pair = zillions.pop +zil_name = zil_pair[0] +zil_base = 10 ** zil_pair[1] +write = left/zil_base +left = left - write*zil_base + +if write > 0 + prefix = english_number write + num_string = num_string + prefix + '' + zil_name + + if left > 0 + num_string = num_string + '' + end +end +end +write = left/10 +left = left - write*10 + +if write > 0 + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + left = 0 + else + num_string = num_string + tens_place[write-1] + end + + if left > 0 + num_string = num_string + '-' + end +end + +write = left +left = 0 + +if write > 0 + num_string = num_string + ones_place[write-1] +end +num_string +end + +puts english_number(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(62819273644748393029282) diff --git a/ch10-nothing-new/shuffle.txt b/ch10-nothing-new/shuffle.txt deleted file mode 100644 index 319d4e4a4..000000000 --- a/ch10-nothing-new/shuffle.txt +++ /dev/null @@ -1,25 +0,0 @@ -def shuffle arr - shuf = [] - while arr.length > 0 - - curr_index = 0 - new_arr = [] - - arr.each do |item| - if curr_index == rand_index - shuf.push item - else - new_arr.push item - end - - curr_index = curr_index + 1 - end - - arr = new_arr - end - - shuf - end - - puts(shuffle([1,2,3,4,5,6,7,8,9])) - \ No newline at end of file From 7753d3a910ead4ca90f7748253811b03d6308c60 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sat, 10 Sep 2016 23:31:09 +0000 Subject: [PATCH 12/28] ninety_nine_bottles_of_beer.rb --- ch10-nothing-new/ninety_nine_bottles_of_beer.rb | 15 ++++++++++++++- 1 file changed, 14 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..4920fcc30 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,14 @@ -# your code here \ No newline at end of file +require_relative 'english_number.rb' + +def plurals(num, word) +num == "one" ? num + " " + word : num + " " + word + "s" +end +count = 999 +while count > 0 + number_bottles = english_number(count) + bottles = plurals(number_bottles, "bottle") + puts "#{bottles.capitalize} of beers on the wall, #{bottles} of beer." + count -= 1 + bottles = plurals(english_number(count), "bottle") + puts "Take one down and pass it around, #{bottles} of beer on the wall." +end \ No newline at end of file From b135cd9afade8ffe5bc5f160c0556932e97f784b Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 09:04:33 +0000 Subject: [PATCH 13/28] 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..cf130ed32 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 + +#Added features: 'abort' on line 21 + + + +Dir.chdir '/home/ubuntu/Projects/learn_to_program/ch11-reading-and-writing/dltst_write' + + + +pic_names = Dir['/home/ubuntu/Projects/learn_to_program/ch11-reading-and-writing/dltst_read'] + + + +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 '.' + + + +abort("File name already in use- aborting.") if File.exist? 'bouncer' + + + + + +new_name = if pic_number < 10 + +"#{batch_name}0#{pic_number}.jpg" + +else + +"#{batch_name}#{pic_number}.jpg" + +end + + + + + +File.rename name, new_name + + + +pic_number = pic_number + 1 + + + +end \ No newline at end of file From c3ba910a9aa605a761a12bbe921d51314ace3f0c Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 09:11:42 +0000 Subject: [PATCH 14/28] build_your_own_playlist.rb --- .../build_your_own_playlist.rb | 15 ++++++++++++++- 1 file changed, 14 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..1714fe205 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,14 @@ -# your code here \ No newline at end of file +# your code here +Dir.chdir '/meeshyep:~/workspace//Projects/learn_to_program/ch11-reading-and-writing/' +music_files= Dir['*.mp3'] +puts music_files + +playlist_name=File.new("michelle_playlist.m3u", "w") + +def write_files object, filename + File.open filename, 'w' do |f| + f.write object.join("\n") + end +end + +write_files(music_files, playlist_name) \ No newline at end of file From 980ec1580a6e2858c3fda11bb5371641f304eff6 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 09:25:13 +0000 Subject: [PATCH 15/28] build_a_better_playlist.rb --- .../build_a_better_playlist.rb | 36 ++++++++++++++++++- 1 file changed, 35 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..ce4c41dd8 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,37 @@ def music_shuffle filenames - # your code here + filenames - filenames.sort + len = filenames.length + else + + 2.times do + l_idx = 0 + r_idx = len/2 + shuf = [] + + while shuf.length < len + if shuf.length%2 == 0 + shuf.push(filenames[r_indx]) + 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 + songs = ['aa/bbb', 'aa/ccc', 'aa/ddd', 'AAA/xxxx', 'AAA/yyy', 'AAA/zzzz' , 'foo/bar'] + puts (music_shuffle(songs)) +end_of_input From f6deb3d899605648a8bd6d02a7d7293c76aaf2a8 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 11:05:29 +0000 Subject: [PATCH 16/28] happy_birthday.rb --- ch12-new-classes-of-objects/happy_birthday.rb | 22 ++++++++++++++++++- .../one_billion_seconds.rb | 8 ++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..778ae95cc 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,21 @@ -# your code here \ No newline at end of file + puts "What is your birth year? (YYYY)" + birth_yr=gets.chomp.to_i + puts "What is your birth month? (MM)" + birth_mon=gets.chomp.to_i + puts "What is your birth date? (DD)" + birth_dt=gets.chomp.to_i + + user_birthday=Time.local(birth_yr, birth_mon, birth_dt) + puts "You were born on: "+user_birthday.to_s + + years_since_birth = Time.new.year-user_birthday.year + + birthday_passed=(Time.local(Time.new.year, birth_mon, birth_dt))-(Time.new) + + if birthday_passed > 0 + years_since_birth=years_since_birth-1 + end + + print "SPANK! "*years_since_birth + puts "You just got spanked "+years_since_birth.to_s+" times. + \ No newline at end of file diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..312d7a432 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,7 @@ -# your code here \ No newline at end of file +michelle_birth_minute=Time.local(1991, 01, 30, 10, 19) +mbm_integer=michelle_birth_minute.to_i + + puts michelle_birth_minute + puts mbm_integer + puts michelle_birth_minute+1000000000 + \ No newline at end of file From 1c22530d8d5a0835aea4807aa712c5f320c5683a Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 11:11:21 +0000 Subject: [PATCH 17/28] one_billion_seconds. --- ch12-new-classes-of-objects/one_billion_seconds.txt | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 ch12-new-classes-of-objects/one_billion_seconds.txt diff --git a/ch12-new-classes-of-objects/one_billion_seconds.txt b/ch12-new-classes-of-objects/one_billion_seconds.txt new file mode 100644 index 000000000..312d7a432 --- /dev/null +++ b/ch12-new-classes-of-objects/one_billion_seconds.txt @@ -0,0 +1,7 @@ +michelle_birth_minute=Time.local(1991, 01, 30, 10, 19) +mbm_integer=michelle_birth_minute.to_i + + puts michelle_birth_minute + puts mbm_integer + puts michelle_birth_minute+1000000000 + \ No newline at end of file From b97df4576fdeade902086bffd9495c9316cb9b23 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 11:17:10 +0000 Subject: [PATCH 18/28] party_like_its_roman_to_integer_mcmxcix.rb --- ...party_like_its_roman_to_integer_mcmxcix.rb | 34 +++++++++++++++++-- 1 file changed, 32 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..f11d31dcc 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,33 @@ 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 + whie 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 = valend + total = total + val + end + + total +end + +puts (roman_to_integer('mcmxcix')) +puts (roman_to_integer('CCCLXV')) From 38ec3a8369b44df1dc28316114df9ed0355fdc07 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 13:03:51 +0000 Subject: [PATCH 19/28] birthday_helper.rb --- .../birthday_helper.rb | 25 ++++++++++++++++++- 1 file changed, 24 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..b7f622d33 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,24 @@ -# your code here \ No newline at end of file +birthday_dates = {} +File.read( 'birthday_dates.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 "Oh, I'm not sure.." +else + puts date[0..5] +end From 97c90d640b5dddd8ceee9d9967fc01a3df237bfa Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:00:25 +0000 Subject: [PATCH 20/28] extend_built_in_classes.rb --- .../extend_built_in_classes.rb | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..d6405e7cb 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,31 @@ class Integer - # your code here + + def factorial + raise 'Must not use negative integers' if self < 0 + (self <= 1) ? 1 : self * (self-1).factorial + end + + def to_roman + raise 'Must use positive integer' if self <= 0 + roman = '' + roman << 'M' * (self / 1000) + roman << 'D' * (self % 1000 / 500) + roman << 'C' * (self % 500 / 100) + roman << 'L' * (self % 100 / 50) + roman << 'X' * (self % 50 / 10) + roman << 'V' * (self % 10 / 5) + roman << 'I' * (self % 5 / 1) + + roman + + end +end + +class Array + def shuffle + sort_by{rand} + end +end + +p 7.factorial.to_roman.split(//).shuffle end \ No newline at end of file From ee25fd43e6fc27e176f6d6cb238b4b731c20e7b2 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:03:54 +0000 Subject: [PATCH 21/28] orange_tree.rb --- ch13-creating-new-classes/orange_tree.rb | 59 +++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..011dbf5aa 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,62 @@ 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.round(1) * 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 + From 9db18fa5614682acbc8202287c331512ea203a01 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:11:00 +0000 Subject: [PATCH 22/28] interactive_baby_dragon.rb --- .../interactive_baby_dragon.rb | 129 +++++++++++++++++- 1 file changed, 128 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..c74ba2280 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,128 @@ -# your code here \ No newline at end of file +# your code here +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 up 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 + + +puts 'Please enter name of new dragon: ' +name = gets.chomp.capitalize +pet = Dragon.new(name) + +while true + puts "What would you like to do with #{name}; there are 5 commands:" + + " feed, walk, play, rock, and bedtime; type 'exit' to quit: " + response = gets.chomp.downcase + + if response == 'feed' + puts pet.feed + elsif response == 'walk' + puts pet.walk + elsif response == 'rock' + puts pet.rock + elsif response == 'bedtime' + puts pet.put_to_bed + elsif response == 'play' + puts pet.toss + elsif + puts "Please enter correct command." + elsif response == 'exit' + exit + end +end From ba5ae6f97c9176de8525a2f36c7215463d6fd299 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:21:28 +0000 Subject: [PATCH 23/28] even_better_profiling.rb --- ch14-blocks-and-procs/even_better_profiling.rb | 16 ++++++++++++++-- 1 file changed, 14 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..94e4c0f34 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,15 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + +end + profiling_on = false + + if profiling_on == true + start_time = Time.new + block.call + + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end +end_of_input From e862e647866bad8e8011891e27c28624d81cd80c Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:22:27 +0000 Subject: [PATCH 24/28] grandfather_clock.rb --- ch14-blocks-and-procs/grandfather_clock.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..8752ac4e4 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,19 @@ def grandfather_clock &block - # your code here + +end + hour = Time.new.hour + + if hour == 0 + hour = 12 + end + + if hour >= 13 + hour = hour - 12 + end + + hour.times do block.call + end +end + +grandfather_clock do puts "DONG!" end \ No newline at end of file From 3b3558ca31ad24acdfb14287d9c82eaca3c63ebf Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:23:41 +0000 Subject: [PATCH 25/28] m --- ch14-blocks-and-procs/program_logger.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..76ae36e08 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,20 @@ def log desc, &block - # your code here + + +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + +program_log 'outer block' do + program_log 'some little block' do + 1**1 + 2**2 + end + + program_log 'yet another block' do + '!doof iahT ekil I'.reverse + end + + '0' == 0 end \ No newline at end of file From 7f95d94e9b1ed2cac786fb763eda9c5fe24cfd5a Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Sun, 11 Sep 2016 14:25:24 +0000 Subject: [PATCH 26/28] better_program_logger.rb --- .../better_program_logger.rb | 34 +++++++++++++++++-- 1 file changed, 32 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..d89193431 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,33 @@ def log desc, &block - # your code here -end \ No newline at end of file + #used the book solution here + +$logger_depth = 0 + +def better_log desc, &block + prefix = ' '*$logger_depth + + puts prefix + 'Beginning "' + desc + '"...' + + $logger_depth = $logger_depth + 1 + + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s +end + +better_log 'outer block' do + better_log 'some little block' do + better_log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase + end + + 7 * 3 * 2 + end + + better_log 'yet another block' do + '!doof naidnI evol I'.reverse + end + + '0' == "0" +end From 9dd19c370f860b3a99a54ffb007c687831bab629 Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Thu, 15 Sep 2016 15:50:12 +0000 Subject: [PATCH 27/28] shuffle.rb --- ch10-nothing-new/shuffle.rb | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index 319d4e4a4..94de15849 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,25 +1,8 @@ -def shuffle arr - shuf = [] - while arr.length > 0 - - curr_index = 0 - new_arr = [] - - arr.each do |item| - if curr_index == rand_index - shuf.push item - else - new_arr.push item - end - - curr_index = curr_index + 1 - end - - arr = new_arr - end - - shuf - end - - puts(shuffle([1,2,3,4,5,6,7,8,9])) - \ No newline at end of file +meeshyep:~/workspace/Projects/learn_to_program/ch10-nothing-new (master) $ irb +2.3.0 :001 > def shuffle arr +2.3.0 :002?> arr.sort_by{rand} +2.3.0 :003?> end + => :shuffle +2.3.0 :004 > p(shuffle([1,2,3,4,5,6,7,8,9,0])) +[9, 0, 6, 8, 4, 3, 2, 1, 7, 5] + => [9, 0, 6, 8, 4, 3, 2, 1, 7, 5] \ No newline at end of file From 31c9a1642b38adc9dcc2379216bbf06b9620342d Mon Sep 17 00:00:00 2001 From: Michelle Epstein Date: Thu, 15 Sep 2016 16:12:46 +0000 Subject: [PATCH 28/28] dictionary_sort.rb --- ch10-nothing-new/dictionary_sort.rb | 34 ++++++++++------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c8453168f..044b15701 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,24 +1,12 @@ -def dictionary_sort arr - rec_dict_sort arr, [] -end -def rec_dict_sort unsorted, sorted - if unsorted.length <= 0 - return sortedend - - 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 +def dictionary_sort arr + return arr if arr.length <= 1 + + middle = arr.pop + less = arr.select{|x| x.downcase < middle.downcase} + morr = arr.select{|x| x.downcase >= middle.downcase} - puts(dictionary_sort(['can', 'feel', 'singing.', 'like', 'A', 'can'])) \ No newline at end of file + dictionary_sort(less) + [middle] + dictionary_sort(more) +end + +words = ['can', 'feel', ' singing', 'lobster', 'banana'] +puts(dictionary_sort(words).join('')) \ No newline at end of file