From 46c0077d912a06949413f19448c13e28f5fa7d96 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sat, 10 Sep 2016 13:09:13 +0100 Subject: [PATCH 01/15] Chapter 09 --- ch09-writing-your-own-methods/ask.rb | 12 +++++- .../old_school_roman_numerals.rb | 17 ++++++++- .../roman_numerals.rb | 38 ++++++++++++++++++- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..60fd86d00 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,13 @@ def ask question - # your code here + while true + puts question + reply = gets.chomp.downcase + if reply == "yes" + return true + elsif reply == "no" + return false + else + puts "Please answer 'yes' or 'no.'" + end + end end \ No newline at end of file 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..5f8b2c2f3 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,16 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + # your code here + 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(2498)) \ 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..a5c1a0fe8 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,37 @@ def roman_numeral num - # your code here -end \ No newline at end of file + roman = "" + roman = roman + "M" * (num / 1000) + if (900..999) === (num % 1000) + roman = roman + "CM" + elsif (400..499) === (num % 500) + roman = roman + "CD" + else + roman = roman + "D" * ((num % 1000) / 500) + roman = roman + "C" * ((num % 500) / 100) + end + if (90..99) === (num % 100) + roman = roman + "XC" + elsif + (40..49) === (num % 50) + roman = roman + "XL" + else + roman = roman + "L" * ((num % 100) / 50) + roman = roman + "X" * ((num % 50) / 10) + end + if (num % 10) == 9 + roman = roman + "IX" + elsif (num % 5) == 4 + roman = roman + "IV" + else + roman = roman + "V" * ((num % 10) / 5) + roman = roman + "I" * ((num % 5) / 1) + end + puts roman +end + +puts(roman_numeral(1999)) +puts(roman_numeral(2469)) +puts(roman_numeral(1378)) +puts(roman_numeral(5)) +puts(roman_numeral(497)) +puts(roman_numeral(949)) \ No newline at end of file From 314d4db3f7b163df1853509e486e31a4f923ee44 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sat, 10 Sep 2016 13:21:17 +0100 Subject: [PATCH 02/15] Chapter 10.a --- ch10-nothing-new/Rite_passage.rb | 24 ++++++++++++++++++++++++ ch10-nothing-new/shuffle.rb | 25 +++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 ch10-nothing-new/Rite_passage.rb diff --git a/ch10-nothing-new/Rite_passage.rb b/ch10-nothing-new/Rite_passage.rb new file mode 100644 index 000000000..18ce2af29 --- /dev/null +++ b/ch10-nothing-new/Rite_passage.rb @@ -0,0 +1,24 @@ +def some_array + recursive_sort some_array, [] +end + +def recursive_sort unsorted_array, sorted_array + if unsorted.length <= 0 + return "Sorted" + end + + smallest = unsorted_array.pop + still_unsorted =[] + + unsorted_array each do |tested| + if tested < smallest + still_unsorted.push smallest + smallest = tested + else + still_unsorted.push tested + end + end + + sorted_array.push smallest + +end diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..b21b159ba 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,24 @@ def shuffle arr - # your code here -end \ No newline at end of file + shuffled = [] + while arr.length > 0 + rand_index = rand(arr.length) + + curr_index = 0 + new_array =[] + + arr.each do |item| + if curr_index == rand_index + shuffled.push item + else + new_array.push item + end + + curr_index = curr_index + 1 + end + + arr = new_array + end + shuffled +end + +puts shuffle [1, 2, 3, 4, 5, 6, 7] \ No newline at end of file From 130fa0e73a372b47fe5be9d23c96f2e59fca3372 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sat, 10 Sep 2016 13:30:17 +0100 Subject: [PATCH 03/15] Chapter 11.a --- .../build_your_own_playlist.rb | 8 ++++++- .../safer_picture_downloading.rb | 24 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..35e13bd10 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,7 @@ -# your code here \ No newline at end of file +# your code here +songs = shuffle(Dir["**/*.mp3"]) +File.open 'playlist.m3u', 'w' do |f| + songs.each do |mp3| + f.write mp3 + "/n" + end +end \ No newline at end of file diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..697db3fb8 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +pic_names = Dir["**/*.{JPG,jpg}"] +puts "How would you want to call this batch?" +batch_name = gets.chomp +puts +puts "Downloading #{pic_names.length} files" +pic_number = 1 +pic_names.each do |name| + print "." + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" +end +if File.exist?(new_name) == true + puts "Sorry, but you would be overwriting a picture!" + exit + else + File.rename name, new_name + pic_number = pic_number + 1 +end +end + +puts "Your batch have been saved with the following name: #{batch_name}!" \ No newline at end of file From 98eed07f9b40fac5dd0f9777536b6e04f9847ef5 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sat, 10 Sep 2016 13:33:25 +0100 Subject: [PATCH 04/15] Chapter 12.a --- ch12-new-classes-of-objects/happy_birthday.rb | 17 +++++++++++++++++ .../one_billion_seconds.rb | 5 ++++- 2 files changed, 21 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..29b2212f4 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,18 @@ +puts "Which year did you get born?" +year = gets.chomp.to_i +puts "Which month did you get born? Please give it in a numerical way (1-12)" +month = gets.chomp.to_i +puts "Which day did you get born?" +day = gets.chomp.to_i + + +birthday = Time.local(year, month, day) +older = 1 +now = Time.new + + +while Time.local(year + older, month, day) < now + puts "PRANK!!" + older = older + 1 +end # your code here \ 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..8391fff35 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 +time = Time.gm(1989, 5, 18) +time2 = time + 1000000000 +puts time +puts time2 From 4abe3bd531b18c34b7c87ffb18c4eac93bf65b0d Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sat, 10 Sep 2016 19:30:04 +0100 Subject: [PATCH 05/15] Roman numerals corrected --- ch09-writing-your-own-methods/roman_numerals.rb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index a5c1a0fe8..5cc8ed29d 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -26,12 +26,5 @@ def roman_numeral num roman = roman + "V" * ((num % 10) / 5) roman = roman + "I" * ((num % 5) / 1) end - puts roman + roman end - -puts(roman_numeral(1999)) -puts(roman_numeral(2469)) -puts(roman_numeral(1378)) -puts(roman_numeral(5)) -puts(roman_numeral(497)) -puts(roman_numeral(949)) \ No newline at end of file From 45971e0d988c19eb12bec18638b443dd8a63f90a Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sun, 11 Sep 2016 00:25:15 +0100 Subject: [PATCH 06/15] Chapter 13 --- .../extend_built_in_classes.rb | 46 ++++++- .../interactive_baby_dragon.rb | 122 +++++++++++++++++- ch13-creating-new-classes/orange_tree.rb | 52 +++++++- 3 files changed, 216 insertions(+), 4 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..1be1097c1 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,45 @@ 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 roman + roman = "" + 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 + "D" * (self % 5 / 1) + end +end + +class Array + def shuffle + arr = self + shuffled = [] + while arr.length > 0 + rand_index = rand(arr.length) + + curr_index = 0 + new_array =[] + + arr.each do |item| + if curr_index == rand_index + shuffled.push item + else + new_array.push item + end + + curr_index = curr_index + 1 + end + + arr = new_array + end + shuffled +end diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..d94fb64da 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,121 @@ -# your code here \ No newline at end of file +class dragon # creates class dragon + def initialize name # initializes an instance of dragon called by its name + @name = name.capitalize # instance variable name is equal name of the dragon + @stuff_in_belly = 10 + @stuff_in_intestine = 0 + + puts "#{@name} is born." + end + + def feed + puts "#{@name} has been fed, and is so happy now" + @stuff_in_belly = 10 + passage_of_time + end + + def walk + puts "#{@name} has enjoyed his walk!" + @stuff_in_intestine = 0 + passage_of_time + end + + def put_to_bed + puts "#{@name} seemed tired and you put him to sleep" + @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 hungy? + @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 hates YOU!" + exit # This quit the program + end + if @stuff_in_intestine >= 10 + @stuff_in_intestine = 0 + puts "Whooops! #{@name} had an accident..." + end + if hungry? + if @asleep + @asleep = false + puts "He wakes up suddenly!" + end + puts "#{@name}'s stomach grimbles..." + end + if poppy? + if @asleep + @asleep = false + puts "#{@name} wakes up suddenly!" + end + puts "#{@name} does the potty dance... someone needs a walk!" + end + end +end + +puts "Hello sir, what's the name of your new little dragon?" +name = gets.chomp.capitalize +pet = Dragon.new name + + +while true + puts + puts "Possible commands for your baby dragon: feed, toss, walk" + + puts " rock, put to bed, exit." + command = gets.chomp.downcase + if command == "exit" + exit + elsif == "feed" + pet.feed + elsif == "toss" + pet.toss + elsif == walk + pet.walk + elsif == rock + pet.rock + elsif == put to bed + pet.put_to_bed + else + puts "Please, choose one of the dragon commands." + end +end diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..d04f05a1a 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,55 @@ class OrangeTree - # your code here + def initialize + @height = 0 + @orange_count = 0 + @alive = true + end + def height + if @alive + @height + else + "A dead tree is not very tall. :(" + end + end + def count_the_oranges + if @alive + @orange_count + else + "A dead tree has no oranges. :(" + end + end + + def one_year_passes + if @alive + @height = @height + 0.4 + @orange_count = 0 + if @height > 10 && rand(2) > 0 + @alive = false + "Oh no! The tree is too old, and has died! :(" + elsif @height > 2 + @orange_count = (@height * 15 - 25).to_i + "This year your tree grew to #{@height} m tall," + + " and produced #{@orange_count} oranges." + else + "This year your tree grew to #{@height} 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 17cbfedc2914e671f0b9abbf14ed4794dea0e564 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sun, 11 Sep 2016 19:05:42 +0100 Subject: [PATCH 07/15] Chapter 10.b --- ch10-nothing-new/dictionary_sort.rb | 22 ++++++- ch10-nothing-new/english_number.rb | 57 ++++++++++++++++++- .../ninety_nine_bottles_of_beer.rb | 15 ++++- ch10-nothing-new/sort.rb | 25 +++++++- 4 files changed, 113 insertions(+), 6 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..5b85ffecd 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,21 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + rec_dict_sort arr, [] +end +def rec_dict_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + smallest = unsorted.pop + still_unsorted = [] + + unsorted.each do |tested| + if tested.downcase < smallest.downcase + still_unsorted.push smallest + smallest = tested + else + still_unsorted.push tested + end + end + sorted.push smallest + rec_dict_sort still_unsorted, sorted +end diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..1fef793e6 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,58 @@ def english_number number - # your code here + if number < 0 + return "Please enter a number that isn't negative." + end + if number == 0 + return "zero" + end + num_string = "" + 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 diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..a63d2d3e6 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 +num_at_start = 5 +num_now = num_at_start +while num_now > 2 + puts english_number(num_now).capitalize + "bottles of beer on the wall, " + + english_number(num_now) + "bottles of beer!" + num_now = num_now - 1 + puts "Take one down, pass it around, " + + english_number(num_now) + "bottles of beer on the wall!" +end + +puts "Two bottles of beer on the wall, two bottles of beer!" +puts "Take one down, pass it around, one bottle of beer on the wall!" +puts "One bottle of beer on the wall, one bottle of beer!" +puts "Take one down, pass it around, no more bottles of beer on the wall!" diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..ed4810003 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,24 @@ 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 <= 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 From 244214d0ce07779922d5656dba99fcc59772b758 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sun, 11 Sep 2016 19:38:05 +0100 Subject: [PATCH 08/15] Chapter 11.b --- .../build_a_better_playlist.rb | 47 ++++++++++++++++++- 1 file changed, 46 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..07d1156a1 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,48 @@ def music_shuffle filenames - # your code here + filenames = filenames.sort + len = filenames.length + + 2.times do + l_idx = 0 + r_idx = len / 2 + shuf = [] + + while shuf.length < len + if shuf.length % == 0 + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + shuf.push(filenames[r_idx]) + r_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 + + + + + + + + + + + + + + end end From 34e012b0cdce4c181b915b813dab4c6f611b9b1f Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sun, 11 Sep 2016 19:40:01 +0100 Subject: [PATCH 09/15] Chapter 12.b --- .../birthday_helper.rb | 24 ++++++++++++++- ...party_like_its_roman_to_integer_mcmxcix.rb | 30 +++++++++++++++++-- 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..35ef5c6c6 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +birth_dates = {} +File.read("birthdates.txt").each_line do |line| + line = line.chomp + + first_comma = 0 + while line[first_comma] != "," && first_comma < line.length + first_comma = first_comma + 1 + end + + name = line[0..(first_comma - 1)] + date = line[-12..-1] + birth_dates[name] = name +end + +puts "Whose birthday would you like to know?" +name = gets.chomp +date = birth_dates[name] + +if date == nil + puts "Oooh, I don't know that one..." +else + puts date [0..5] +end 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..090c56dc9 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,29 @@ def roman_to_integer roman - # your code here -end \ No newline at end of file + digit_vals = {'i' => 1, + 'v' => 5, + 'x' => 10, + 'l' => 50, + 'c' => 100, + 'd' => 500, + 'm' => 1000} + total = 0 + prev = 0 + index = roman.length - 1 + while index >= 0 + c = roman[index].downcase + index = index - 1 + val = digit_vals[c] + if !val + puts "This is not a valid roman numeral!" + return + end + + if val < prev + val = val * -1 + else + prev = val + end + total = total + val + end + total +end From a402466aec2adcba140ca402ac7309426b0ae569 Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sun, 11 Sep 2016 20:00:29 +0100 Subject: [PATCH 10/15] Orange Tree corrected --- ch13-creating-new-classes/orange_tree.rb | 63 +++++++++++++----------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index d04f05a1a..7b79267c9 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -10,52 +10,55 @@ class OrangeTree def initialize @height = 0 @orange_count = 0 - @alive = true + @year = 0 + @dead = 0 end - def height - if @alive - @height + + def one_year_passes + @height += 0.4 + @year += 1 + if @year == 26 + @dead = 1 + return "Oh, no! The tree is too old, and has died. :(" + elsif @dead == 1 + return "A year later, the tree is still dead. :(" else - "A dead tree is not very tall. :(" + unless @year < 6 + oranges = @height * 15 - 25 + @orange_count = oranges + else + oranges = 0 + end + return "This year your tree grew to #{@height.round(1)}m tall, and produced #{oranges.to_i} oranges." end end - def count_the_oranges - if @alive - @orange_count + + def height + if @dead == 0 + return @height.round(1) else - "A dead tree has no oranges. :(" + return "A dead tree is not very tall. :(" end end - def one_year_passes - if @alive - @height = @height + 0.4 - @orange_count = 0 - if @height > 10 && rand(2) > 0 - @alive = false - "Oh no! The tree is too old, and has died! :(" - elsif @height > 2 - @orange_count = (@height * 15 - 25).to_i - "This year your tree grew to #{@height} m tall," + - " and produced #{@orange_count} oranges." - else - "This year your tree grew to #{@height} m tall," + - " but is still too young to bear fruit." - end + def count_the_oranges + if @dead == 0 + return @orange_count.to_i else - "A year later, the tree is still dead. :(" + return "A dead tree has no oranges. :(" end end + def pick_an_orange - if @alive + if @dead == 0 if @orange_count > 0 - @orange_count = @orange_count - 1 - "You pick a juicy, delicious orange!" + return "Mmmmm, delicious juicy freshness." + @orange_count -= 1 else - "You search every branch, but find no oranges." + return "Oh dear, there are no oranges left. Try next year!" end else - "A dead tree has nothing to pick. :(" + return "A dead tree has nothing to pick. :(" end end end From 7c574571fc5f5adfce70cf9da16a36ba865fe76c Mon Sep 17 00:00:00 2001 From: JulioGT Date: Sun, 11 Sep 2016 21:19:05 +0100 Subject: [PATCH 11/15] Chapter 14 --- .../better_program_logger.rb | 13 ++++++++++--- .../even_better_profiling.rb | 12 ++++++++++-- ch14-blocks-and-procs/grandfather_clock.rb | 19 +++++++++++++++++-- ch14-blocks-and-procs/program_logger.rb | 17 ++++++++++++++--- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..b9ba9b9e7 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,10 @@ -def log desc, &block - # your code here -end \ No newline at end of file +$nesting_depth = 0 + +def better_log desc, &block + indent = " " * $nesting_depth + puts "#{indent}Beginning \"#{desc}\"..." + $nesting_depth += 1 + result = block.call + puts "#{indent}...\"#{desc}\" finished, returning: #{result}" + $nesting_depth -= 1 +end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..0bb3ff44c 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,11 @@ +$OPT_PROFILING_ON = false def profile block_description, &block - # your code here -end \ No newline at end of file + if $OPT_PROFILING_ON + start_time = Time.new + block[] + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block[] + end +end diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..edb7f9b8b 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 -end \ No newline at end of file + hour = Time.new.hour + if hour >= 13 + hour = hour - 12 + end + + if hour == 0 + hour = 12 + end + + hour.times do + block.call + end +end + +grandfather_clock do + puts "DONG!" +end diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..62f6f7b2f 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,14 @@ -def log desc, &block - # your code here -end \ No newline at end of file +def program_log desc, &block + puts "Beginning #{desc.inspect}..." + result = block[] + puts "...#{desc.inspect} finished, returning: #{result}" +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 From 6b2b5c6cfd1b1f417b7e8d304b6166f129a08152 Mon Sep 17 00:00:00 2001 From: Julio Date: Sun, 11 Sep 2016 21:33:56 +0100 Subject: [PATCH 12/15] Update roman_numerals.rb --- ch09-writing-your-own-methods/roman_numerals.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5cc8ed29d..0ee765a04 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,6 +1,7 @@ def roman_numeral num roman = "" roman = roman + "M" * (num / 1000) + if (900..999) === (num % 1000) roman = roman + "CM" elsif (400..499) === (num % 500) From cb51c289b9a9b7b0f633ef10751651123cece165 Mon Sep 17 00:00:00 2001 From: Julio Date: Sun, 11 Sep 2016 21:55:02 +0100 Subject: [PATCH 13/15] Update roman_numerals.rb --- ch09-writing-your-own-methods/roman_numerals.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 0ee765a04..5cc8ed29d 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,7 +1,6 @@ def roman_numeral num roman = "" roman = roman + "M" * (num / 1000) - if (900..999) === (num % 1000) roman = roman + "CM" elsif (400..499) === (num % 500) From e9bb2b53229cb7eec36641ea0176ef76b4b740e6 Mon Sep 17 00:00:00 2001 From: Julio Date: Sun, 11 Sep 2016 21:57:19 +0100 Subject: [PATCH 14/15] Update old_school_roman_numerals.rb --- .../old_school_roman_numerals.rb | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 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 5f8b2c2f3..362380272 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,16 +1,13 @@ def old_roman_numeral num - # your code here - 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 "Oops! Try using a positive integer!" if num <= 0 + roman = '' + roman << 'M' * (num / 1000) + roman << 'D' * (num % 1000 / 500) + roman << 'C' * (num % 500 / 100) + roman << 'L' * (num % 100 / 50) + roman << 'X' * (num % 50 / 10) + roman << 'V' * (num % 10 / 5) + roman << 'I' * (num % 5 / 1) -puts(old_roman_numeral(2498)) \ No newline at end of file + return roman +end From 012ee2d656ed263f38c544a98d072a2c661cbe06 Mon Sep 17 00:00:00 2001 From: Julio Date: Sun, 11 Sep 2016 21:58:23 +0100 Subject: [PATCH 15/15] Update extend_built_in_classes.rb --- .../extend_built_in_classes.rb | 62 +++++++------------ 1 file changed, 22 insertions(+), 40 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 1be1097c1..293f0de2a 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,45 +1,27 @@ class Integer - def factorial - if self <= 1 - 1 - else - self * (self - 1).factorial - end - end + +def factorial + + raise 'Must not use negative integer' if self < 0 - def roman - roman = "" - 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 + "D" * (self % 5 / 1) + (self <= 1) ? 1 : self * (self-1).factorial end -end -class Array - def shuffle - arr = self - shuffled = [] - while arr.length > 0 - rand_index = rand(arr.length) - - curr_index = 0 - new_array =[] - - arr.each do |item| - if curr_index == rand_index - shuffled.push item - else - new_array.push item - end - - curr_index = curr_index + 1 - end - - arr = new_array - end - shuffled + + 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