From a84a02772d0ee89a3daddbd91e95df51eb906971 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 11:25:06 +0000 Subject: [PATCH 01/23] Chapter 9: Ask --- ch09-writing-your-own-methods/ask.rb | 35 ++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..8d6a5b2f5 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,34 @@ 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" + return true + else + return false + end + break + else + puts 'Please answer "yes" or "no".' + end + end +end + +puts "Hello, and thank you for..." +puts +ask "Do you like eating tacos?" +ask "Do you like eating burritos?" +wets_bed = ask "Do you wet the bed?" +ask "Do you like eating chimichangas?" +ask "Do you like eating sopapillas?" +puts "Just a few more questions..." +ask "Do you like drinking horchata?" +ask "Do you like eating flautas?" + +puts +puts "DEBRIEFING:" +puts "Thank you for..." +puts +puts wets_bed From b77aacf115f8db1088070619c787340689663fb9 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 12:53:31 +0000 Subject: [PATCH 02/23] Chapter 9: Old Sch Roman Numerals --- .../old_school_roman_numerals.rb | 16 ++++++++++++++-- 1 file changed, 14 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..ea1d1cf41 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,15 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + old_roman = "" + + old_roman = old_roman + "M" * (num / 1000) + old_roman = old_roman + "D" * (num % 1000 / 500) + old_roman = old_roman + "C" * (num % 500 / 100) + old_roman = old_roman + "L" * (num % 100 / 50) + old_roman = old_roman + "X" * (num % 50 / 10) + old_roman = old_roman + "V" * (num % 10 / 5) + old_roman = old_roman + "I" * (num % 5 / 1) + + old_roman + +end From 486d5f8ee414b0b7149bc92025b7a21ce1968273 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 12:54:07 +0000 Subject: [PATCH 03/23] Chapter 9: Roman Numerals --- .../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..39900573d 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 + new_roman = "" + + thousands = num / 1000 + hundreds = num % 1000 / 100 + tens = num % 100 / 10 + ones = num % 10 + + new_roman = "M" * thousands + + if hundreds == 9 + new_roman = new_roman + "CM" + elsif hundreds == 4 + new_roman = new_roman + "CD" + else + new_roman = new_roman + "D" * (num % 1000 / 500) + new_roman = new_roman + "C" * (num % 500 / 100) + end + + if tens == 9 + new_roman = new_roman + "XC" + elsif tens == 4 + new_roman = new_roman + "XL" + else + new_roman = new_roman + "L" * (num % 100 / 50) + new_roman = new_roman + "X" * (num % 50 / 10) + end + + if ones == 9 + new_roman = new_roman + "IX" + elsif ones == 4 + new_roman = new_roman + "IV" + else + new_roman = new_roman + "V" * (num % 10 / 5) + new_roman = new_roman + "I" * (num % 5) + end + new_roman +end From 7c1c9d6b85a80fac7269ef7e630712c065a471e9 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 16:34:24 +0000 Subject: [PATCH 04/23] Chapter 10: Sort --- ch10-nothing-new/sort.rb | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..92e5a3c8b 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,29 @@ def sort arr - # your code here -end \ No newline at end of file + rec_sort arr, [] +end + +def rec_sort unsorted, sorted + + if unsorted.length < 1 + return sorted + end + + still_unsorted = [] + smallest = unsorted.pop # smallest placeholder item + + unsorted.each do |i| + if i < smallest + still_unsorted << smallest + smallest = i + else + still_unsorted << i + end + end + + # this returns smallest item,store it in sorted and use recursion to find the next smallest item. + + sorted << smallest + rec_sort still_unsorted, sorted + +end + From 8b11da0d98a91f7199019025b1e599d83764fad5 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 16:55:09 +0000 Subject: [PATCH 05/23] Chapter 10: Shuffle --- ch10-nothing-new/shuffle.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..e86627e2c 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,13 @@ -def shuffle arr - # your code here +def shuffle (array) + shuffled = [] + if array.length == 0 + return array + else + while array.length > 0 + n = rand(array.length - 1) + shuffled << array[n] + array.delete_at(n) + end + end + shuffled end \ No newline at end of file From 1afb8b327a4898748a9911ae3f783e55a53d0bc3 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 19:26:33 +0000 Subject: [PATCH 06/23] Chapter 10: Dictionary Sort --- 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..e93260aa3 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 + rec_dic_sort arr, [] +end + +def rec_dic_sort unsorted, sorted + + if unsorted.length < 1 + return sorted + end + + still_unsorted = [] + smallest = unsorted.pop # smallest placeholder item + + unsorted.each do |i| + if i.downcase < smallest.downcase + still_unsorted << smallest + smallest = i + else + still_unsorted << i + end + end + + sorted << smallest + rec_dic_sort still_unsorted, sorted + +end + From 810fc4efdbfa190d542a5ae9119d69b00974c25d Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 20:22:30 +0000 Subject: [PATCH 07/23] Chapter 10: English number --- ch10-nothing-new/english_number.rb | 63 +++++++++++++++++++++++++++++- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..9297a30e8 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,62 @@ def english_number number - # your code here -end + if number < 0 + return "Please eneter 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 \ No newline at end of file From 85133fcc1fa22449bb82a62bbbc608e845d11511 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Wed, 7 Sep 2016 21:00:05 +0000 Subject: [PATCH 08/23] Chapter 10: 99 bottles --- .../ninety_nine_bottles_of_beer.rb | 113 +++++++++++++++++- 1 file changed, 112 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..e4c9ff539 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,112 @@ -# your code here \ No newline at end of file +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"] + + left = number + write = left / 1000000000000 # how many trllions? + left = left - write * 1000000000000 # substract the millions + + if write > 0 + trillions = english_number write # recursion! + num_string = num_string + trillions + " trillion" + if left > 0 + num_string = num_string + " " + end + end + + write = left / 1000000000 # how many billions? + left = left - write * 1000000000 # substract the billions + + if write > 0 + billions = english_number write # recursion! + num_string = num_string + billions + " billion" + if left > 0 + num_string = num_string + " " + end + end + + + write = left / 1000000 # how many millions? + left = left - write * 1000000 # substract the millions + + if write > 0 + millions = english_number write # recursion! + num_string = num_string + millions + " million" + if left > 0 + num_string = num_string + " " + end + end + + write = left / 1000 # how many thousands? + left = left - write * 1000 # substract the thousands + + if write > 0 + thousands = english_number write # recursion! + num_string = num_string + thousands + " thousand" + if left > 0 + num_string = num_string + " " + end + end + + write = left / 100 # how many hundreds? + left = left - write * 100 # substract the hundreds + + if write > 0 + hundreds = english_number write # recursion! + num_string = num_string + hundreds + " hundred" + if left > 0 + num_string = num_string + " " + 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 + +bbottles = 99 + +while bbottles >1 + bb_eng = english_number bbottles + bb_next = english_number (bbottles-1) + puts (bb_eng + " bottles of beer on the wall, " + bb_eng + " bottles of beer!").capitalize + puts "Take one down and pass it around, " + bb_next + " bottles of beer on the wall!" + puts + bbottles -=1 +end + 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!" + + + \ No newline at end of file From 32b728e24fa5ad6603e36398a10918b77ae1cdfb Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 09:09:32 +0000 Subject: [PATCH 09/23] Chapter 11: Playlist --- .../build_your_own_playlist.rb | 26 ++++++++++++++++++- 1 file changed, 25 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..0e23367b0 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,25 @@ -# your code here \ No newline at end of file +def shuffle (array) + shuffled = [] + if array.length == 0 + return array + else + while array.length > 0 + n = rand(array.length - 1) + shuffled << array[n] + array.delete_at(n) + end + end + shuffled +end + +# check: puts Dir["C:/Users/Li/Music/Various/**/*.{mp3,wma}"] +# check: puts Dir["C:/Users/Li/Music/Various/**/*.{mp3,wma}"].length +songs = shuffle(Dir["C:/Users/Li/Music/Various/**/*.{mp3,wma}"]) + +File.open "playlist_v.m3u", "w" do |f| + songs.each do |song| # can't write the playlist with an array so each method is used + f.write song + "\n" + end +end + +puts "Checkout the new playlist!" From 3db29fbd00a7e20cc633602c2e5d5f09bd5cbb5a Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 10:37:36 +0000 Subject: [PATCH 10/23] Chapter 11: Safer Picture Download --- .../safer_picture_downloading.rb | 43 ++++++++++++++++++- 1 file changed, 42 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..958dc4ec3 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,42 @@ -# your code here \ No newline at end of file +# change into the directory you want to work in +Dir.chdir "C:/Users/Li/Desktop/Dir/Dir2" + +# find the original files +pic_names = Dir["C:/Users/Li/Desktop/Dir/Dir1/*.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_name0}#{pic_number}.jpg" + end + + if File.exist?(new_name) + puts "File #{new_name} already existed. Overwrite - Y / N ?" + overwrite = gets.chomp + + if overwrite == "Y" || overwrite == "y" + File.rename name, new_name + pic_number += 1 + else + pic_number += 1 + end + + else + File.rename name, new_name + pic_number += 1 + end +end + + +puts +puts "All done!" From b04bea4d51f62272585bddcea51609f0cba83bda Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 12:15:58 +0000 Subject: [PATCH 11/23] Chapter 11: Better Playlist --- .../build_a_better_playlist.rb | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..5c3692816 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,35 @@ +# Chris Pine's solutions with additional comments + def music_shuffle filenames - # your code here -end + filenames = filenames.sort + len = filenames.length + + 2.times do + l_idx = 0 # index of next card in left pile + r_idx = len / 2 # index of next card in right pile + shuf = [] + + while shuf.length < len + if shuf.length % 2 == 0 # if number of shuf items is 0 or even + shuf << filenames[r_idx] # add card from right pile + r_idx += 1 + else # if number of shuf items is odd + shuf << filenames[l_idx] # add card from left pile + l_idx += 1 + end + end + filenames = shuf # replaces current playlist with shuffled playlist + end + + #cutting the deck + arr = [] + cut = rand(len) # random cut + idx = 0 + + while idx < len + arr << filenames[(idx + cut) % len] # (% len) is to ensure the index will not be out of bounds. if idx = 3 and rand returns 10, filenames[13] would return nil unless % is used. + idx += 1 + end + + arr +end \ No newline at end of file From 1c1534bb50349391fbba59c5c25b6a91035bf095 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 15:15:40 +0000 Subject: [PATCH 12/23] Chapter 12: One billion secs --- ch12-new-classes-of-objects/one_billion_seconds.rb | 5 ++++- 1 file changed, 4 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..6a00459d7 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,4 @@ -# your code here \ No newline at end of file +birth = Time.local(1990, 1, 1, 13, 31, 50) # Not real +a_billion_secs_later = birth + 1_000_000_000 + +puts a_billion_secs_later \ No newline at end of file From 2e46f53cbeb1d142402d0b8bf6ac21e550774913 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 17:18:28 +0000 Subject: [PATCH 13/23] Chapter 12: Happy Birthday! --- ch12-new-classes-of-objects/happy_birthday.rb | 20 ++++++++++++++++++- 1 file changed, 19 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..b7bf6dd82 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,19 @@ -# your code here \ No newline at end of file +# happy birthday! + +puts "What year were you born in?" +year = gets.chomp.to_i +puts "What month were you born in? Please provide an integer, January = 1, February = 2..." +month = gets.chomp.to_i +puts "Which day were you born on?" +day = gets.chomp.to_i + + +birth = Time.local(year, month, day) + +year = 60 * 60 * 24 * 365 # seconds in a year +day = 60 * 60 * 24 # seconds in a day + +age = ((Time.now - birth) / year).ceil +age -= 1 # ensure you don't get spank for the year ahead + +puts "SPANK \n" * age \ No newline at end of file From 674fa2a2eae8cb5a81662a0a15992c00e7d2d51e Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 20:08:23 +0000 Subject: [PATCH 14/23] Chapter 12: Party --- ...party_like_its_roman_to_integer_mcmxcix.rb | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) 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..4bb6c50cd 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,28 @@ def roman_to_integer roman - # your code here + # check if argument is valid roman numerals + if roman =~ /[^MDCLXVImdclxvi]/ + puts "Not valid Roman numerals" + else + idx = 0 + integer = 0 + all_roman = roman.upcase + + while idx < roman.length + integer = integer + 1000 if all_roman[idx] == "M" + integer = integer + 500 if all_roman[idx] == "D" + integer = integer + 100 if all_roman[idx] == "C" + integer = integer + 50 if all_roman[idx] == "L" + integer = integer + 10 if all_roman[idx] == "X" + integer = integer + 5 if all_roman[idx] == "V" + integer = integer + 1 if all_roman[idx] == "I" + idx += 1 + end + + integer = integer - 200 if all_roman.include?("CM") || all_roman.include?("CD") + integer = integer - 20 if all_roman.include?("XC") || all_roman.include?("XL") + integer = integer - 2 if all_roman.include?("IX") || all_roman.include?("IV") + + integer + end + end \ No newline at end of file From 87fddc02d7296786ea15c2edc27e325b0095d2b3 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Thu, 8 Sep 2016 20:08:53 +0000 Subject: [PATCH 15/23] Chapter 12: Birthday helper --- .../birthday_helper.rb | 33 ++++++++++++++++++- 1 file changed, 32 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..fdf351249 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,32 @@ -# your code here \ No newline at end of file +data_str = File.read ("Data.txt").each_line {|line| line.chomp} +data_arr = data_str.split("\n") + +birth_dates = {} +name = [] +date = [] +year = [] + +data_arr.each do |string| + a = string.split(",") + name << a[0] + date << a[1] + year << a[2] +end + +idx = 0 +while idx < name.length + birth_dates[name[idx]] = date[idx] + year[idx] + idx += 1 +end + +puts "Whose birthday would you like to find out?" +person = gets.chomp + +if birth_dates.has_key?(person) + dates = birth_dates[person] + puts dates[0..5] +else + puts "Sorry, we don't have information about this person." +end + +end# your code here \ No newline at end of file From 6a7969b4c34da5f364e6c93c1f43ffec7197a25d Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Fri, 9 Sep 2016 12:57:33 +0000 Subject: [PATCH 16/23] Chapter 13: Extend Built-In Classes --- .../extend_built_in_classes.rb | 67 ++++++++++++++++++- 1 file changed, 66 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..5207e5d3f 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,68 @@ class Integer - # your code here + def factorial + if self < 0 + return "You can't take the factorial of a negative number!" + end + if self <= 1 + 1 + else + self * (self-1).factorial + end + end + + def to_roman + new_roman = "" + + thousands = self / 1000 + hundreds = self % 1000 / 100 + tens = self % 100 / 10 + ones = self % 10 + + new_roman = "M" * thousands + + if hundreds == 9 + new_roman = new_roman + "CM" + elsif hundreds == 4 + new_roman = new_roman + "CD" + else + new_roman = new_roman + "D" * (self % 1000 / 500) + new_roman = new_roman + "C" * (self % 500 / 100) + end + + if tens == 9 + new_roman = new_roman + "XC" + elsif tens == 4 + new_roman = new_roman + "XL" + else + new_roman = new_roman + "L" * (self % 100 / 50) + new_roman = new_roman + "X" * (self % 50 / 10) + end + + if ones == 9 + new_roman = new_roman + "IX" + elsif ones == 4 + new_roman = new_roman + "IV" + else + new_roman = new_roman + "V" * (self % 10 / 5) + new_roman = new_roman + "I" * (self % 5) + end + new_roman + end +end + + +class Array + def shuffle + shuffled = [] + if self.length == 0 + return self + else + while self.length > 0 + n = rand(self.length) + shuffled << self[n] + self.delete_at(n) + end + end + shuffled + end end \ No newline at end of file From 7ca293d4731b81d615d507b80daa33c45e51731d Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Fri, 9 Sep 2016 14:21:26 +0000 Subject: [PATCH 17/23] Chapter 13: Orange Tree --- ch13-creating-new-classes/orange_tree.rb | 47 +++++++++++++++++++++++- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..ed1c2ecad 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,7 +5,50 @@ # 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 + @age = 0 + @height = 0 + @oranges = 0 + end + + def height + if @age <= 25 + @height + else + "A dead tree is not very tall. :(" + end + end + + def one_year_passes + @age += 1 + @height = (@age * 0.4).round(1) + if @age <= 5 + @oranges = 0 + elsif @age <= 25 + @oranges = (@height * 15 - 25) + "This year your tree grew to #{@height}m tall, and produced #{@oranges.round} oranges." + elsif @age == 26 + "Oh, no! The tree is too old, and has died. :(" + else + "A year later, the tree is still dead. :(" + end + end + + def count_the_oranges + if @age <= 25 + @oranges.round + else + "A dead tree has no oranges. :(" + end + end + + def pick_an_orange + if @age <=25 + @oranges -= 1 + else + "A dead tree has nothing to pick. :(" + end + end end + From 4397be779881ea23765c43e7e3c25b1866cd7379 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Fri, 9 Sep 2016 16:36:23 +0000 Subject: [PATCH 18/23] Chapter 13: Interactive Baby Dragon --- .../interactive_baby_dragon.rb | 51 ++++++++++++++++++- 1 file changed, 50 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..8a8fcc06d 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,50 @@ -# your code here \ No newline at end of file +class Dragon + def initialize name + @name = name + end + + def feed + puts "#{@name} is sharing your burrito. Munch, munch, munch. Nothing is left for you..." + end + + def walk + puts "#{@name} is enjoying the walk in the park." + end + + def put_to_bed + puts "You tuck #{@name} into bed." + 3.times do + puts "#{@name} snores, filling the room with smoke." + end + end + + def rock + puts "You rock #{@name} gently." + puts 'He briefly dozes off...' + puts '...but wakes when you stop.' + end +end + +puts "Hello there! You get to take care of a dragon today. Which dragon (enter name) would you like to spend some time with?" +name = gets.chomp +pet = Dragon.new (name) + +puts "Since #{name} is a baby dragon, make sure you feed, walk, rock and put it to bed at appropriate time." + +5.times do + puts "What would you like to do with #{name}?" + action = gets.chomp + + if action =~ /feed/ + pet.feed + elsif action =~ /walk/ + pet.walk + elsif action =~ /rock/ + pet.rock + elsif action =~ /bed/ + pet.put_to_bed + exit + else + puts "#{name} doesn't get what you're trying to do." + end +end From 61d2364ff6935d1cc89b479de7907d2b4ec8774b Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Fri, 9 Sep 2016 17:18:55 +0000 Subject: [PATCH 19/23] Chapter 14: Even Better Profiling --- .../even_better_profiling.rb | 29 +++++++++++++++++-- 1 file changed, 27 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..71e1023c9 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,28 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + + profiling_on = true + + if profiling_on + 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 + 100000.times do + number = number + 1 + end +end From 6ef4c9d53822de97efaad31d200393c2a4075611 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Fri, 9 Sep 2016 19:15:18 +0000 Subject: [PATCH 20/23] Chapter 14: Grandfather Clock --- ch14-blocks-and-procs/grandfather_clock.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..7fb0befb5 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,8 @@ def grandfather_clock &block - # your code here + hour = (Time.new.hour + 11) % 12 + 1 # if Time.new.hour % 12 is coded only, hour at 12 and 24 will == 0 + hour.times {block.call} +end + +grandfather_clock do + puts "DONG!" end \ No newline at end of file From f6e6bb35c81de573a2a42398eb94329991361786 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Sat, 10 Sep 2016 19:00:54 +0000 Subject: [PATCH 21/23] Chapter 14: Program Logger --- ch14-blocks-and-procs/program_logger.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..846d091a7 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 +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + +program_log "outer block" do # no string required because it's defined in log , only write code to return false + program_log "some little block" do + 1 + 4 + end + +program_log "yet another block" do + "I like Thai food!" + end + 1 != 1 end \ No newline at end of file From ce6896288b23dd501302f1b9e7b89417d02fc49f Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Sat, 10 Sep 2016 19:01:35 +0000 Subject: [PATCH 22/23] Chapter 14: Better Program Logger --- .../better_program_logger.rb | 28 +++++++++++++++++-- 1 file changed, 26 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..e4cb2f693 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,27 @@ -def log desc, &block - # your code here +$nesting_depth = 0 # outside of program so it doesn't reset depth to 0 + +def better_log desc, &block + + prefix = " " * $nesting_depth + puts prefix + 'Beginning "' + desc + '"...' + $nesting_depth += 1 + result = block.call + $nesting_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 * 6 + end + + better_log 'yet another block' do + "I love Indian food!" + end + + 1 == 1 end \ No newline at end of file From e096b3619ed17ad771abab33eec7585dc6f311b2 Mon Sep 17 00:00:00 2001 From: Li Chien Beh Date: Sat, 10 Sep 2016 19:08:37 +0000 Subject: [PATCH 23/23] Chapter 13: Extend Built In Classes - revise roman to old roman --- .../extend_built_in_classes.rb | 53 ++++++------------- 1 file changed, 15 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 5207e5d3f..cbf40408a 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -9,48 +9,25 @@ def factorial self * (self-1).factorial end end + def to_roman - new_roman = "" - - thousands = self / 1000 - hundreds = self % 1000 / 100 - tens = self % 100 / 10 - ones = self % 10 - - new_roman = "M" * thousands - - if hundreds == 9 - new_roman = new_roman + "CM" - elsif hundreds == 4 - new_roman = new_roman + "CD" - else - new_roman = new_roman + "D" * (self % 1000 / 500) - new_roman = new_roman + "C" * (self % 500 / 100) - end - - if tens == 9 - new_roman = new_roman + "XC" - elsif tens == 4 - new_roman = new_roman + "XL" - else - new_roman = new_roman + "L" * (self % 100 / 50) - new_roman = new_roman + "X" * (self % 50 / 10) - end - - if ones == 9 - new_roman = new_roman + "IX" - elsif ones == 4 - new_roman = new_roman + "IV" - else - new_roman = new_roman + "V" * (self % 10 / 5) - new_roman = new_roman + "I" * (self % 5) - end - new_roman + + old_roman = "" + + old_roman = old_roman + "M" * (self / 1000) + old_roman = old_roman + "D" * (self % 1000 / 500) + old_roman = old_roman + "C" * (self % 500 / 100) + old_roman = old_roman + "L" * (self % 100 / 50) + old_roman = old_roman + "X" * (self % 50 / 10) + old_roman = old_roman + "V" * (self % 10 / 5) + old_roman = old_roman + "I" * (self % 5 / 1) + + old_roman + end end - class Array def shuffle shuffled = [] @@ -65,4 +42,4 @@ def shuffle end shuffled end -end \ No newline at end of file +end \ No newline at end of file