From da20fe71c795b1a00eeb6b811761fe3b39cf8191 Mon Sep 17 00:00:00 2001 From: iaink Date: Mon, 5 Sep 2016 21:35:25 +0000 Subject: [PATCH 1/6] emptyFile --- emptyFile | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 emptyFile diff --git a/emptyFile b/emptyFile new file mode 100644 index 000000000..e69de29bb From 7831b7525361fc773d2f4dca7fa69a7154a716d2 Mon Sep 17 00:00:00 2001 From: iaink Date: Tue, 6 Sep 2016 17:56:45 +0000 Subject: [PATCH 2/6] origin --- ch09-writing-your-own-methods/ask.rb | 31 ++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..3e28d823e 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,30 @@ 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 + +puts "Hello, and thank you for participating in this survey." +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?" + +puts +puts "DEBRIEFING:" +puts "Thank you for your time." +puts +puts wets_bed \ No newline at end of file From 7fa6983c5656ad9f29ef6b529131dd894d50e5c1 Mon Sep 17 00:00:00 2001 From: iaink Date: Tue, 6 Sep 2016 18:29:27 +0000 Subject: [PATCH 3/6] origin --- 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 3e28d823e..ad97fa2d6 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -3,7 +3,7 @@ def ask question puts question reply = gets.chomp.downcase - if (reply == "yes"} || reply == "no") + if (reply == "yes" || reply == "no") if reply == "yes" return true else @@ -13,8 +13,8 @@ def ask question else puts "Please answer \"yes\" or \"no\"." end + end end - puts "Hello, and thank you for participating in this survey." puts From cc0ac83b4986000c93b9223871adb862d54a5220 Mon Sep 17 00:00:00 2001 From: iaink Date: Mon, 12 Sep 2016 00:12:39 +0000 Subject: [PATCH 4/6] origin --- .../old_school_roman_numerals.rb | 15 ++++- .../roman_numerals.rb | 58 ++++++++++++++++++- 2 files changed, 67 insertions(+), 6 deletions(-) diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index ca6589f2d..aa7348cdb 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,12 @@ -def old_roman_numeral num - # your code here -end \ No newline at end of file +def old_roman number + roman = '' + roman += 'M' * (number/1000) + roman += 'D' * (number % 1000/500) + roman += 'C' * (number % 500/100) + roman += 'L' * (number % 100/50) + roman += 'X' * (number % 50/10) + roman += 'V' * (number % 10/ 5) + roman += 'I' * (number % 5) +end + +puts old_roman(1209) \ No newline at end of file diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..4d6a3128a 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,55 @@ -def roman_numeral num - # your code here -end \ No newline at end of file +def old_roman_numeral number + + i = 0 + v = 0 + x = 0 + l = 0 + c = 0 + d = 0 + m = 0 + + if number >= 1000 + m = number/1000 + number = number % 1000 + end + + if number >= 500 + d = number/500 + number = number % 500 + end + + if number >= 100 + c = number/100 + number = number % 100 + end + + if number >= 50 + l = number/50 + number = number % 50 + end + + if number >= 10 + x = number/10 + number = number % 10 + end + + if number >= 5 + v = number/5 + number = number % 5 + end + + if number < 5 + i = number/1 + number = number % 10 + end + +puts ("M" * m) + ("D" * d) + ("C" * c) + ("L" * l) + ("X" * x) + ("V" * v) + ("I" * i) + +end + +puts "Enter a number, and I'll convert it into an old-school Roman numeral" + +reply = gets.chomp + +puts "The number " + reply.to_s + " in old-school Roman numerals is:" +old_roman_numeral reply.to_i \ No newline at end of file From a72baa0ae0fbedebbf35454f832c290fd1232b28 Mon Sep 17 00:00:00 2001 From: iaink Date: Mon, 12 Sep 2016 04:19:35 +0000 Subject: [PATCH 5/6] origin --- ch10-nothing-new/dictionary_sort.rb | 19 ++++++++++++--- ch10-nothing-new/shuffle.rb | 6 +++-- ch10-nothing-new/sort.rb | 12 +++++++++- .../one_billion_seconds.rb | 2 +- .../extend_built_in_classes.rb | 24 +++++++++++++++++-- ch14-blocks-and-procs/grandfather_clock.rb | 15 ++++++++++++ 6 files changed, 69 insertions(+), 9 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..04b40a423 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,16 @@ -def dictionary_sort arr - # your code here -end \ No newline at end of file +def sort(some_array) + recursive_sort(some_array, []) +end + +def recursive_sort(unsorted_array, sorted_array) + smallest = unsorted_array.reduce { |memo, word| memo.downcase < word.downcase ? memo : word} + unsorted_array.delete_at(unsorted_array.index(smallest)) + sorted_array << smallest + if unsorted_array.length > 0 + recursive_sort(unsorted_array, sorted_array) + else + sorted_array + end +end + +puts sort(["Blue", "red", "pink", "Yellow", "Purple", "green"]) \ No newline at end of file diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..ffb9c3907 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,5 @@ def shuffle arr - # your code here -end \ No newline at end of file + arr.sort_by{rand} +end + +puts (shuffle ([4, 10, 9, 8, 400, 252])) \ No newline at end of file diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..441ee3d08 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,13 @@ def sort arr - # your code here + + return arr if arr.length <= 1 + + first = arr[0] + arr.delete_at(0) + + higher = arr.select {|a| a > first} + lower = arr.select {|a| a <= first} + + sort(lower) + [first] + sort(higher) + end \ 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..21ce25669 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1 @@ -# your code here \ No newline at end of file +puts(Time.local(1993, 7, 1) + 10**9) \ No newline at end of file diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..6a06bb329 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,23 @@ class Integer - # your code here -end \ No newline at end of file + def factorial + if self < 1 + 1 + else self * (self-1).factorial + end +end + + def to_roman + num = self + 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) + end +end + +puts 40.to_roman +puts 4.factorial \ No newline at end of file diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..d65bddb52 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,18 @@ def grandfather_clock &block # your code here + + if Time.new.hour < 12 + current_hr = Time.new.hour + else + current_hr = Time.new.hour - 12 + end + + current_hr.times do + block.call + end + +end + +grandfather_clock do + puts 'DONG!' end \ No newline at end of file From cb8ca893f41d800c8112a62459e25174ac9858f2 Mon Sep 17 00:00:00 2001 From: iaink Date: Wed, 14 Sep 2016 14:19:20 +0000 Subject: [PATCH 6/6] Completed the remainder of CP's exercises. --- ch10-nothing-new/english_number.rb | 99 +++++++++++++- .../ninety_nine_bottles_of_beer.rb | 109 ++++++++++++++- .../build_a_better_playlist.rb | 49 ++++++- .../build_your_own_playlist.rb | 11 +- .../safer_picture_downloading.rb | 99 +++++++++++++- .../birthday_helper.rb | 22 ++- ch12-new-classes-of-objects/happy_birthday.rb | 15 ++- .../one_billion_seconds.rb | 2 +- ...party_like_its_roman_to_integer_mcmxcix.rb | 29 +++- .../extend_built_in_classes.rb | 59 ++++++-- .../interactive_baby_dragon.rb | 126 +++++++++++++++++- ch13-creating-new-classes/orange_tree.rb | 86 ++++++++++-- .../better_program_logger.rb | 31 ++++- .../even_better_profiling.rb | 15 ++- ch14-blocks-and-procs/program_logger.rb | 18 ++- emptyFile | 0 16 files changed, 731 insertions(+), 39 deletions(-) delete mode 100644 emptyFile diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..729d85f7e 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,98 @@ def english_number number - # your code here -end + if number < 0 + return 'Please enter a numberwith a value higher than zero' + 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'] + + large_n = [['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 large_n.length > 0 + zil_pair = large_n.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 diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..64ee2fcc3 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,108 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 + return 'Please enter a numberwith a value higher than zero' + 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'] + + large_n = [['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 large_n.length > 0 + zil_pair = large_n.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 + +num_at_start = 40 +num_now = num_at_start +while num_now > 1 + 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 \ No newline at end of file diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..7eca10fe5 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,50 @@ + # Used code from solution. def music_shuffle filenames - # your code here + # We don't want a perfectly random shuffle, so let's + # instead do a shuffle like card-shuffling. Let's + # shuffle the "deck" twice, then cut it once. That's + # not enough times to make a perfect shuffle, but it + # does mix things up a bit. + # Before we do anything, let's actually *sort* the + # input, since we don't know how shuffled it might + # already be, and we don't want it to be *too* random. + filenames = filenames.sort + len = filenames.length + + # Now we shuffle twice. + 2.times do + l_idx = 0 # index of next card in left pile + r_idx = len/2 # index of next card in right pile + shuf = [] + # NOTE: If we have an odd number of "cards", + # then the right pile will be larger. + + while shuf.length < len + if shuf.length%2 == 0 + # take card from right pile + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + # take card from left pile + shuf.push(filenames[l_idx]) + l_idx = l_idx + 1 + end + end + + filenames = shuf + end + # And cut the deck. + arr = [] + cut = rand(len) # index of card to cut at + idx = 0 + + while idx < len + arr.push(filenames[(idx+cut)%len]) + idx = idx + 1 + end + + arr end +# songs = ['aa/bbb', 'aa/ccc', 'aa/ddd', +# 'AAA/xxxx', 'AAA/yyyy', 'AAA/zzzz', 'foo/bar'] +# puts(music_shuffle(songs)) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..a60dd9cb7 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,10 @@ -# your code here \ No newline at end of file + # Used code from solution. +all_oggs = shuffle(Dir['**/*.ogg']) + +File.open 'playlist.m3u', 'w' do |f| + all_oggs.each do |ogg| + f.write ogg+"\n" + end +end + +puts 'Done!' diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..ddebd5379 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,98 @@ -# your code here \ No newline at end of file +# For Katy, with love. + +### Download pictures from camera card. + +require 'win32ole' + +STDOUT.sync = true +Thread.abort_on_exception = true + +Dir.chdir 'C:\Documents and Settings\Chris\Desktop\pictureinbox' + +# Always look here for pics. + +pic_names = Dir['!undated/**/*.{jpg,avi}'] +thm_names = Dir['!undated/**/*.{thm}' ] + +# Scan for memory cards in the card reader. +WIN32OLE.new("Scripting.FileSystemObject").Drives.each() do |x| + #driveType 1 is removable disk + if x.DriveType == 1 && x.IsReady + pic_names += Dir[x.DriveLetter+':/**/*.{jpg,avi}'] + thm_names += Dir[x.DriveLetter+':/**/*.{thm}' ] + end +end + +months = %w(jan feb mar apr may jun jul aug sep oct nov dec) + +encountered_error = false + +print "Downloading #{pic_names.size} files: " + +pic_names.each do |name| + print '.' + is_movie = (name[-3..-1].downcase == 'avi') + + if is_movie + orientation = 0 + new_name = File.open(name) do |f| + f.seek(0x144,IO::SEEK_SET) + f.read(20) + end + + new_name[0...3] = '%.2d' % (1 + months.index(new_name[0...3].downcase)) + new_name = new_name[-4..-1] + ' ' + new_name[0...-5] + else + new_name, orientation = File.open(name) do |f| + f.seek(0x36, IO::SEEK_SET) + orientation_ = f.read(1)[0] + f.seek(0xbc, IO::SEEK_SET) + new_name_ = f.read(19) + [new_name_, orientation_] + end + end + + [4,7,10,13,16].each {|n| new_name[n] = '.'} + if new_name[0] != '2'[0] + encountered_error = true + puts "\n"+'ERROR: Could not process "'+name+ + '" because it\'s not in the proper format!' + next + end + + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') + # Make sure we don't save over another file!! + while FileTest.exist? save_name + new_name += 'a' + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') + end + + + case orientation + when 6 + `convert "#{name}" -rotate "90>" "#{save_name}"` + File.delete name + when 8 + `convert "#{name}" -rotate "-90>" "#{save_name}"` + File.delete name + else + File.rename name, save_name + end +end + +print "\nDeleting #{thm_names.size} THM files: " +thm_names.each do |name| + print '.' + File.delete name +end + +# If something bad happened, make sure she + +# sees the error message before the window closes. + +if encountered_error + puts + puts "Press [Enter] to finish." + puts + gets +end diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..0803d8341 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,21 @@ -# your code here \ No newline at end of file +birth_dates = {} +File.read('birthdays.txt').each_line do |line| + line = line.chomp + first_comma = 0 + while line[first comma] != ',' && + first_comma = first_comma + 1 + end + + name = line[0..(first_comma -1)] + date = line[-12..-1] + end + + puts 'Whose birthday would you like to know?' + name = gets.chomp + date = birth_dates[name] + + if date == nil + puts 'No matches were found' + else + puts date [0..5] + end \ No newline at end of file diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..3fc6500a5 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,14 @@ -# your code here \ No newline at end of file +puts "What year was your born in? Please use the following format: YYYY" +year = gets.chomp.to_i +puts "What month was you born in? Please select a number between 1 and 12." +month = gets.chomp.to_i +puts "What day was you born on? Please select a number between 1 and 31." +day = gets.chomp.to_i + +time = Time.new +age = 1 + +while Time.local(year + age, month, day) <= time + puts 'SPANK!' + age += 1 +end \ 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 21ce25669..e23bb5cee 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1 @@ -puts(Time.local(1993, 7, 1) + 10**9) \ No newline at end of file +puts Time.local(1993, 7, 1) + 10**9 \ No newline at end of file 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..acddb91c6 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 + digit_vals = {'i' => 1, + 'v' => 5, + 'x' => 10, + 'l' => 50, + 'c' => 100, + 'd' => 500, + 'm' => 1000} + total = 0 + prev = 0 + index = roman.length - 1 + while index >= 0 + c = roman[index].downcase + index = index - 1 + val = digit_vals[c] + if !val + puts 'This is not a valid roman numeral!' + return + end + + if val < prev + val = val * -1 + else + prev = val + end + total = total + val + end + + total end \ No newline at end of file diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 6a06bb329..01487f9c2 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,23 +1,54 @@ +class Array + def shuffle + arr = self + + shuf = [] + + while arr.length > 0 + + rand_index = rand(arr.length) + + 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 + end + class Integer def factorial - if self < 1 + if self <= 1 1 - else self * (self-1).factorial + else + self * (self-1).factorial + end end -end - def to_roman - num = self + 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 = roman + 'M' * (self / 1000) + roman = roman + 'D' * (self % 1000 / 500) + roman = roman + 'C' * (self % 500 / 100) + roman = roman + 'L' * (self % 100 / 50) + roman = roman + 'X' * (self % 50 / 10) + roman = roman + 'V' * (self % 10 / 5) + roman = roman + 'I' * (self % 5 / 1) + + roman end end -puts 40.to_roman -puts 4.factorial \ No newline at end of file +puts [1,2,3,4,5].shuffle +puts 7.factorial +puts 73.to_roman diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..80dad2bf3 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,125 @@ -# 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_belly = 10 + 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 -= 1 + @stuff_in_intestine += 2 + 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 stomch 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 name your baby dragon' +name = gets.chomp +pet = Dragon.new name +while true + puts + puts 'commands: feed, toss, walk, rock, put to bed, exit' + command = gets.chomp + if command == 'exit' + exit + elsif command == 'feed' + pet.feed + elsif command == 'toss' + pet.toss + elsif command == 'walk' + pet.walk + elsif command == 'rock' + pet.rock + elsif command == 'put to bed' + pet.put_to_bed + else + puts 'Huh? Please type one of the commands.' + end +end \ No newline at end of file diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..f411b86a4 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -1,11 +1,81 @@ -# in order to pass the rspec please follow the below rates of growth, orange production and age of death. -# have your OrangeTree grow by 0.4 per year. -# have it produce no oranges in its first 5 years -# starting in its sixth year have it produce oranges at a rate of (height * 15 - 25) per year. -# 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. - +=begin +in order to pass the rspec please follow the below rates of growth, orange production and age of death. +have your OrangeTree grow by 0.4 per year. +have it produce no oranges in its first 5 years +starting in its sixth year have it produce oranges at a rate of (height * 15 - 25) per year. +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. +=end 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 += 0.4 + @orange_count = 0 + if @height > 10 && rand(2) > 0 # 0.4 * 25 = 10. Thus, this is the point at which our tree should die. + @alive = false + 'Oh, no! The tree is too old, and has died. :(' + elsif + @height > 2 # 0.4 * 5 = 2. Thus, any @height value greater than 2 should produce oranges. + @orange_count = (@height * 15 - 25).to_i + "This year, your tree grew to #{@height.round(1)}M tall and produced #{@orange_count} oranges." + else + "This year, your tree grew to #{@height.round(1)}M tall, but is still to young to bear fruit." + end + else + 'A year later, tree is still dead.' + end + end + + def pick_an_orange + if @alive + if orange_count > 0 + @orange_count -= 1 + 'You pick a juicy, delicious orange!' + else + 'You search every branch, but find no oranges.' + end + else + 'A dead tree has nothing to pick. :(' + end + end +end + +ot = OrangeTree.new +23.times do +ot.one_year_passes end + +puts(ot.one_year_passes) +puts(ot.count_the_oranges) +puts(ot.height) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.one_year_passes) +puts(ot.height) +puts(ot.count_the_oranges) +puts(ot.pick_an_orange) \ No newline at end of file diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..cc8c1c07d 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,30 @@ +$logger_depth = 0 + def log desc, &block - # your code here -end \ No newline at end of file + 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 + +log 'outer block' do + log 'some little block' do + log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase + end + + 7 * 3 * 2 + end + + log 'yet another block' do + '!doof naidnI evol I'.reverse + end + + '0' == "0" +end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..5e205bd90 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,14 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + # To turn profiling on/off, set this + # to true/false. + profiling_on = false + if profiling_on + start_time = Time.new + block.call + + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end +end diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..559789486 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,17 @@ def log desc, &block - # your code here -end \ No newline at end of file + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + +log 'outer block' do + log 'some little block' do + 1**1 + 2**2 + end + + log 'yet another block' do + '!doof iahT ekil I'.reverse + end + + '0' == 0 +end diff --git a/emptyFile b/emptyFile deleted file mode 100644 index e69de29bb..000000000