From fc0b2ff2df402cf7d409769b91fc3846dd157ebc Mon Sep 17 00:00:00 2001 From: Dionysis Date: Wed, 7 Sep 2016 21:59:15 +0000 Subject: [PATCH 01/24] testFile for initial pull request --- testFile | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 testFile diff --git a/testFile b/testFile new file mode 100644 index 000000000..e69de29bb From 1b88e272eabd7ef625f7d693274613fce81320f2 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Wed, 7 Sep 2016 22:06:32 +0000 Subject: [PATCH 02/24] ask challenge rspec success --- 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..bc8515457 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" + return true + elsif reply == "no" + return false + else + puts "Please answer \"yes\" or \"no\"" + end + end +end + +puts "Hello, and thank you for..." +puts +ask "Do you like eating tacos?" # Ignore this return value +ask "Do you like eating burritos?" # And this one +wets_bed = ask "Do you wet the bed?" # Save this return value +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 6851e934f9a8d299b174d6b9dde575466a898af5 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Thu, 8 Sep 2016 08:44:34 +0000 Subject: [PATCH 03/24] old_school_roman_numerals rspec success --- .../old_school_roman_numerals.rb | 21 +++++++++++++++++-- 1 file changed, 19 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..7390aaa18 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,20 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + # reference: + # I = 1, V = 5, X = 10, L = 50 + # C = 100, D = 500, M = 1000 + + 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 "The number #{num}, is converted to:\n#{roman} in roman numerals" + roman +end + +puts old_roman_numeral 555 From 42ebf2f4a157c84e31ea2fc8b8c47746be744253 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Thu, 8 Sep 2016 14:50:12 +0000 Subject: [PATCH 04/24] sort challenge rspec success --- ch10-nothing-new/sort.rb | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..a58e9e6f2 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,28 @@ def sort arr - # your code here -end \ No newline at end of file + recursive_sort(arr, []) +end + +def recursive_sort(unsorted_arr, sorted_arr) + if unsorted_arr.length < 1 + return sorted_arr + end + + last_unsorted_word = unsorted_arr.pop + new_arr = [] + + unsorted_arr.each do |word| + if word < last_unsorted_word + new_arr << last_unsorted_word + last_unsorted_word = word + else + new_arr << word + end + end + + sorted_arr << last_unsorted_word + recursive_sort(new_arr, sorted_arr) +end + +#test_array = ["orange","apple","banana","cherry","orange","watermelon"] +#puts test_array.length +#puts sort(test_array) \ No newline at end of file From 52492d69c5d69ef394fb149ec65582faa063ab1a Mon Sep 17 00:00:00 2001 From: Dionysis Date: Thu, 8 Sep 2016 15:29:49 +0000 Subject: [PATCH 05/24] shuffle challenge rspec success --- ch10-nothing-new/shuffle.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..da197f90d 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,7 @@ -def shuffle arr - # your code here -end \ No newline at end of file +def shuffle(array) + array.shuffle +end + + +#test_array = ["hi",5,"6",["hey","you"],"Einstein"] +#puts shuffle(test_array) From c5aa22653a9f41375461f9823359310bf4f975d0 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Thu, 8 Sep 2016 15:51:54 +0000 Subject: [PATCH 06/24] dictionary sort challenge rspec succes --- ch10-nothing-new/dictionary_sort.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..b383f90a8 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,6 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + arr.sort_by { |word| word.downcase } +end + +#words = ['Can','feel','singing.','like','A','can',"b","B"] +#puts dictionary_sort words \ No newline at end of file From a74dd875b7b994fd62d21f7fd76a057253a309aa Mon Sep 17 00:00:00 2001 From: Dionysis Date: Fri, 9 Sep 2016 10:14:41 +0000 Subject: [PATCH 07/24] better playlist challenge --- .../build_a_better_playlist.rb | 37 ++++++++++++++++++- 1 file changed, 36 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..1b9a6288c 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,38 @@ def music_shuffle filenames - # your code here + + filenames = filenames.sort + len = filenames.length + + # shuffle twice. + 2.times do + l_idx = 0 + r_idx = len/2 # + shuf = [] + + + while shuf.length < len + if shuf.length%2 == 0 + + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + + shuf.push(filenames[l_idx]) + l_idx = l_idx + 1 + end + end + + filenames = shuf + end + + arr = [] + cut = rand(len) + idx = 0 + + while idx < len + arr.push(filenames[(idx+cut)%len]) + idx = idx + 1 + end + + arr end From bb6816c9d5c774de5befe439351867217a89e83a Mon Sep 17 00:00:00 2001 From: Dionysis Date: Fri, 9 Sep 2016 12:07:29 +0000 Subject: [PATCH 08/24] one billion seconds challenge --- 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..66e739fde 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_born = Time.gm(1993,3,22) +puts time_born +time_born_billion_seconds = time_born + (1 * 10 ** 9) +puts time_born_billion_seconds \ No newline at end of file From cd8b8e2d49a7afc03b54818ffa86587e58d70047 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Fri, 9 Sep 2016 12:25:57 +0000 Subject: [PATCH 09/24] happy birthday challenge --- ch12-new-classes-of-objects/happy_birthday.rb | 19 ++++++++++++++++++- 1 file changed, 18 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..ec1ecbd62 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,18 @@ -# your code here \ No newline at end of file +puts "Hi, please answer the following giving the numerical format of Calendars" +puts "What year were you born?" +year = gets.chomp +puts "And what month?" +month = gets.chomp +puts "And on what day?" +day = gets.chomp +birthday = Time.gm(year,month,day) +time_now =Time.new +seconds_old = time_now - birthday +puts "You are #{seconds_old} seconds old" +puts + +years_old = 1 +while Time.gm(year.to_i + years_old, month, day) <= time_now + puts "SPANK!" + years_old += 1 +end \ No newline at end of file From ec0265a0d02ca27f6ac26e0c6666f85e01362bcf Mon Sep 17 00:00:00 2001 From: Dionysis Date: Fri, 9 Sep 2016 15:27:03 +0000 Subject: [PATCH 10/24] roman to integer challenge rspec success --- ...party_like_its_roman_to_integer_mcmxcix.rb | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb b/ch12-new-classes-of-objects/party_like_its_roman_to_integer_mcmxcix.rb index 037b6cb09..bf7092a6b 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,31 @@ -def roman_to_integer roman - # your code here +def roman_to_integer(roman) + rom_int = {"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 -= 1 + val = rom_int[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 From 3191c0016ced1109f5c17bcdca6e4a1b7eeae2a5 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Fri, 9 Sep 2016 16:19:15 +0000 Subject: [PATCH 11/24] birthday helper step1 --- .../birthday_helper.rb | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..f0c9269bb 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,24 @@ -# your code here \ No newline at end of file +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 += 1 + end + + name = line[0..(first_comma - 1)] + date = line[-12..-1] + + birth_dates[name] = date +end + +puts "Type a name to see their Birthday date" +name = gets.chomp +date = birth_dates[name] + +if date == nil + puts "Unknown" +else + puts date[0..5] +end \ No newline at end of file From 4da38a1b8822f15f7d53710b6d01a0a1a7a6b8c8 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sat, 10 Sep 2016 10:47:45 +0000 Subject: [PATCH 12/24] extend built in classes rspec success --- .../extend_built_in_classes.rb | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..b6a60fa21 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,28 @@ +class Array + def shuffle + sort_by{rand} + end +end + 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 + 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 + 'I' * (self % 5 / 1) + + roman + end +end From b90cc82263152dc12b62d9f70895841f9164c6eb Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sat, 10 Sep 2016 12:58:55 +0000 Subject: [PATCH 13/24] baby dragon challenge --- .../interactive_baby_dragon.rb | 120 +++++++++++++++++- 1 file changed, 119 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..ad6ad35dc 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,119 @@ -# your code here \ No newline at end of file +class Dragon +def initialize name +@name = name +@asleep = false +@stuff_in_belly = 10 # He's full. +@stuff_in_intestine = 0 # He doesn't need to go. +puts "#{@name} is born." +end +def feed +puts "You feed #{@name}." +@stuff_in_belly = 10 +passage_of_time +end +def walk +puts "You walk #{@name}." +@stuff_in_intestine = 0 +passage_of_time +end +def put_to_bed +puts "You put #{@name} to bed." +@asleep = true +3.times do +if @asleep +passage_of_time +end +if @asleep +puts "#{@name} snores, filling the room with smoke." +end +end +if @asleep +@asleep = false +puts "#{@name} wakes up slowly." +end +end +def toss +puts "You toss #{@name} up into the air." +puts 'He giggles, which singes your eyebrows.' +passage_of_time +end +def rock +puts "You rock #{@name} gently." +@asleep = true +puts 'He briefly dozes off...' +passage_of_time +if @asleep +@asleep = false +puts '...but wakes when you stop.' +end +end +private +# "private" means that the methods defined here are +# methods internal to the object. (You can feed your +# dragon, but you can't ask him whether he's hungry.) +def hungry? +# Method names can end with "?". +# Usually, we do this only if the method +# returns true or false, like this: +@stuff_in_belly <= 2 +end +def poopy? +@stuff_in_intestine >= 8 +end +def passage_of_time +if @stuff_in_belly > 0 +# Move food from belly to intestine. +@stuff_in_belly = @stuff_in_belly - 1 +@stuff_in_intestine = @stuff_in_intestine + 1 +else # Our dragon is starving! +if @asleep +@asleep = false +puts 'He wakes up suddenly!' +end +puts "#{@name} is starving! In desperation, he ate YOU!" +exit # This quits the program. +end +if @stuff_in_intestine >= 10 +@stuff_in_intestine = 0 +puts "Whoops! #{@name} had an accident..." +end +if hungry? +if @asleep +@asleep = false +puts 'He wakes up suddenly!' +end +puts "#{@name}'s stomach grumbles..." +end +if poopy? +if @asleep +@asleep = false +puts 'He wakes up suddenly!' +end +puts "#{@name} does the potty dance..." +end +end +end + +puts "Name your baby dragon?" +name = gets.chomp +pet = Dragon.new name + +while true +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 "Please type one of the commands given above." + end +end \ No newline at end of file From 95fd5825628a3807e07fa42b4db5eee91f8ed934 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sat, 10 Sep 2016 16:04:44 +0000 Subject: [PATCH 14/24] better profiling rspec success --- .../even_better_profiling.rb | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..7708b7687 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,16 @@ -def profile block_description, &block - # your code here -end \ No newline at end of file +def profile block_description, &block +# To turn profiling on/off, set this +# to true/false. + + profiling_on = true # true for on, false for off + + if profiling_on + start_time = Time.new # time block is called + block.call # call the block liner + duration = Time.new - start_time # time taken for block to execute + puts "#{block_description}: #{duration} seconds" + else + block.call # call block without calculating the time taken + end +end + From adb1cd7ed79ef48f006a128752156eb225da7e39 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sat, 10 Sep 2016 16:19:26 +0000 Subject: [PATCH 15/24] grandfather clock rspec success --- ch14-blocks-and-procs/grandfather_clock.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..ec8c52688 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,20 @@ def grandfather_clock &block - # your code here + hour = Time.new.hour # current hour + + #convert hour format to 12 hours + if hour >= 13 + hour = hour - 12 + end + + if hour == 0 || hour == 24 # at 0 and 24, the hour is 12 + hour = 12 + end + + hour.times do # how many times the clock should sound + block.call + end +end + +grandfather_clock do + puts "DONG!!" end \ No newline at end of file From 70f2d805617a29b34a9b0053921b47999d8c9de4 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 12:46:55 +0000 Subject: [PATCH 16/24] program logger rspec success --- ch14-blocks-and-procs/program_logger.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..21c142dfb 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 +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + +program_log "outer blog" do + program_log "addition" do + 1+2+3+4+5 + end + +program_log "names start with a capital letter!" do + "My name is " +"dionysis".capitalize! + end + + "2" != 2 end \ No newline at end of file From 0e47c2f3070b687cbcf008448d0d182e230f3600 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 13:10:21 +0000 Subject: [PATCH 17/24] better program logger rspec success --- .../better_program_logger.rb | 23 +++++++++++++++++-- 1 file changed, 21 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..4dd68bb5c 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,22 @@ -def log desc, &block - # your code here +$intend_line = 0 + +def better_log desc, &block + intend = " " * $intend_line + puts intend + 'Beginning "' + desc + '"...' + $intend_line = $intend_line + 1 + result = block.call + $intend_line = $intend_line - 1 + puts intend + '..."' + desc + '" finished, returning: ' + result.to_s +end + +better_log "outer blog" do + better_log "addition" do + 1+2+3+4+5 + end + +better_log "names start with a capital letter!" do + "My name is " +"dionysis".capitalize! + end + + "2" != 2 end \ No newline at end of file From 1753ef3a02e2bc6f3666a539d09706fbd20a1913 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 13:15:57 +0000 Subject: [PATCH 18/24] note to makers for Chapter 14 --- note_to_makers.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 note_to_makers.txt diff --git a/note_to_makers.txt b/note_to_makers.txt new file mode 100644 index 000000000..767fce6c6 --- /dev/null +++ b/note_to_makers.txt @@ -0,0 +1,5 @@ +For the rspec tests for Chapter 14, the pre-defined method's names (in the original file) +had to be changed in order to match the expected methods in the spec file. + +program_logger: "log" to "program_log" +better_program_logger: "log" to "better_log" \ No newline at end of file From 4b8851be5aff24894bc29fc5df340060353c72cf Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 14:56:14 +0000 Subject: [PATCH 19/24] roman numerals rspec success --- .../roman_numerals.rb | 55 ++++++++++++++++++- 1 file changed, 53 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..7cdc9c366 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,54 @@ def roman_numeral num - # your code here -end \ No newline at end of file + # reference: + # I = 1, V = 5, X = 10, L = 50 + # C = 100, D = 500, M = 1000 + + thous = (num / 1000) + hunds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10 ) + + roman = 'M' * thous # start building the roman number + # starting with the largest (thousands) + + + # conversion code for integer to roman is the same + # as the old_school_roman_numerals + + # 9 takes 1 before 10 + # 4 takes 1 before 5 + # giving the code instructions for case of 9 and 4 for each group + # respectively (hundreds, tens, ones) + + + if hunds == 9 + roman = roman + "CM" + elsif hunds == 4 + roman = roman + "CD" + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens == 4 + roman = roman + 'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end + + roman + +end + From 1db9e001e1b16b613b0c207be4965f12a95d9e1a Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 16:06:09 +0000 Subject: [PATCH 20/24] english number rspec success --- ch10-nothing-new/english_number.rb | 81 +++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..482a871aa 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,82 @@ def english_number number - # your code here + if number < 0 # No negative numbers. + return "Please enter a number that isn't negative." + elsif number == 0 + return "zero" + end + + num_string = "" # start with empy number 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 = number + + while zillions.length > 0 + zil_pair = zillions.pop #removes last array pair until reaches correct + zil_name = zil_pair[0] # takes the name of number + + # how many + zil_base = 10 ** zil_pair[1] + write = left_number/zil_base + left_number = left_number - write*zil_base + + if write > 0 + prefix = english_number write # call method + num_string = num_string + prefix + " " + zil_name + if left_number > 0 + + num_string = num_string + " " # building the number + end + end + + end + + write = left_number/10 + left_number = left_number - write*10 + if write > 0 + if ((write == 1) and (left_number > 0)) + num_string = num_string + teenagers[left_number-1] + left_number = 0 + else + num_string = num_string + tens_place[write-1] + end + if left_number > 0 + num_string = num_string + "-" + end + end + write = left_number + left_number = 0 + if write > 0 + num_string = num_string + ones_place[write-1] + end + + num_string end + +#puts english_number(101) +#puts english_number(234) +#puts english_number(3211) +#puts english_number(999999) From 143200a67851c28d2e7658441e40585edc00e1bb Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 16:17:38 +0000 Subject: [PATCH 21/24] ninety nine bottles + english number challenge --- .../ninety_nine_bottles_of_beer.rb | 100 +++++++++++++++++- 1 file changed, 99 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..b62502a52 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,99 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 # No negative numbers. + return "Please enter a number that isn't negative." + elsif number == 0 + return "zero" + end + + num_string = "" # start with empy number 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 = number + + while zillions.length > 0 + zil_pair = zillions.pop #removes last array pair until reaches correct + zil_name = zil_pair[0] # takes the name of number + + # how many + zil_base = 10 ** zil_pair[1] + write = left_number/zil_base + left_number = left_number - write*zil_base + + if write > 0 + prefix = english_number write # call method + num_string = num_string + prefix + " " + zil_name + if left_number > 0 + + num_string = num_string + " " # building the number + end + end + + end + + write = left_number/10 + left_number = left_number - write*10 + if write > 0 + if ((write == 1) and (left_number > 0)) + num_string = num_string + teenagers[left_number-1] + left_number = 0 + else + num_string = num_string + tens_place[write-1] + end + if left_number > 0 + num_string = num_string + "-" + end + end + write = left_number + left_number = 0 + if write > 0 + num_string = num_string + ones_place[write-1] + end + + num_string +end + +# use any number below to set the lyrics of the song +num_at_start = 99 # 99 beers! +num_at_start = 5 # change to 9999 if you want num_now = num_at_start while num_now > 2 puts english_number(num_now).capitalize + ' bottles of beer on the wall, ' + english_number(num_now) + ' bottles of beer!' num_now = num_now - 1 puts 'Take one down, pass it around, ' + english_number(num_now) + ' bottles of beer on the wall!' end puts "Two bottles of beer on the wall, two bottles of beer!" puts "Take one down, pass it around, one bottle of beer on the wall!" puts "One bottle of beer on the wall, one bottle of beer!" puts "Take one down, pass it around, no more bottles of beer on the wall!" + +num_now = num_at_start + +# using a while loop to call the enlish_number function +# converting number to word_number for a given number +# up to 3 +# then change text for last 2 bottles + +while num_now > 2 + puts english_number(num_now).capitalize + ' bottles of beer on the wall, ' + english_number(num_now) + ' bottles of beer!' + num_now = num_now - 1 + puts 'Take one down, pass it around, ' + english_number(num_now) + ' bottles of beer on the wall!' +end + +puts "Two bottles of beer on the wall, two bottles of beer!" +puts "Take one down, pass it around, one bottle of beer on the wall!" +puts "One bottle of beer on the wall, one bottle of beer!" +puts "Take one down, pass it around, no more bottles of beer on the wall!" From af65b3bb67a0aa3514c9cc9182712d4d880eac15 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 16:52:32 +0000 Subject: [PATCH 22/24] own playlist --- .../build_your_own_playlist.rb | 48 ++++++++++++++++++- 1 file changed, 47 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..0cfe8b797 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,47 @@ -# your code here \ No newline at end of file +# shuffle method + +def shuffle arr +shuf = [] # bulding array +while arr.length > 0 + +# pick one random element from the array. +rand_index = rand(arr.length) +# Now go through each item in the array, +# putting them all into new_arr except for the +# randomly chosen one, which goes into shuf. +curr_index = 0 +new_arr = [] + +arr.each do |item| + if curr_index == rand_index + shuf.push item # a random file is pushed + else + new_arr.push item + end + curr_index = curr_index + 1 +end +# Replace the original array with the new, +# smaller array. + +arr = new_arr +end +shuf +end + +############################ +# creates a "playlist" file +# that lists in random order +# the flac music files in the +# current directory +# run this .rb file from the folder +# you want the playlist to be made + + +all_flac = shuffle(Dir['**/*.flac']) + +File.open 'playlist.m3u', 'w' do |f| + all_flac.each do |flac_files| + f.write flac_files+"\n" + end +end +puts "Done! See current working folder to see the playlist file" \ No newline at end of file From 79282bfb1d936660017b4d0f4641e4d888ede369 Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 18:07:58 +0000 Subject: [PATCH 23/24] flash drive picture downloader --- .../safer_picture_downloading.rb | 94 ++++++++++++++++++- 1 file changed, 93 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..b6e33df3b 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,93 @@ -# your code here \ No newline at end of file +### Download pictures from camera card + +require "win32ole" + +STDOUT.sync = true +Thread.abort_on_exception = true + +# output dir +Dir.chdir 'C:\Users\Dionysis\Ruby\AA' + +# source files +pic_names = Dir['E:/**/*.{jpg,mp4}'] +thm_names = Dir['E:/**/*.{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,mp4}'] + thm_names += Dir[x.DriveLetter+':/**/*.{thm}'] + end +end + +months = %w(jan feb mar apr may jun jul aug sept oct nov dec) + +encountered_error = false + +print "Downloading #{pic_names.size} files: " + +pic_names.each do |name| + print "." + is_movie = (name[-3..-1].downcase == "mp4" ) + + if is_movie + orientation = 0 + new_name = File.open(name) do |f| + f.seek(0x144, IO::SEEK_SET) + f.read(20) + end + + mew_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) |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.mp4" : ".jpg") + # do not overwrite files + while FileTest.exists? save_name + new_name += "a" + save_name = new_name + (is_movie ? ".orig.mp4" : ".jpg") + end + + case orientation + when 6 + 'convert "#{name}" -rotate "90>" "#{save_name}"' + when 8 + 'convert "#{name}" -rotate -"90>" "#{save_name}"' + File.delete name + else + File.rename name, save_name + 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 +# the error message before the window closes. + +if encountered_error + puts + puts + "Press [Enter] to finish." + puts + gets +end From 23ac3a0fc96d5338e7db77b32129afaac918928d Mon Sep 17 00:00:00 2001 From: Dionysis Date: Sun, 11 Sep 2016 20:12:55 +0000 Subject: [PATCH 24/24] orange tree done! --- ch13-creating-new-classes/orange_tree.rb | 69 +++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..6312e869b 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,72 @@ class OrangeTree - # your code here + def initialize + @age = 0 # initial age of tree + @height = 0 # initial height of tree + @orange_count = 0 # initially no oranges on tree + end + + def one_year_passes + @age = @age + 1 # annual age growth + @height = (@height + 0.4).round(1) # annual year growth + + if @age <= 5 + @orange_count = 0 + elsif @age <= 25 + @orange_count = (@height * 15 - 25) # orange production + "This year your tree grew to #{@height}m tall, and produced #{@orange_count.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 height + if @age <= 25 + @height + else + "A dead tree is not very tall. :(" + end + end + + def count_the_oranges + if @age <= 25 + @orange_count.round + else + "A dead tree has no oranges. :(" + end + end + + def pick_an_orange + if @age <=25 + @orange_count = @orange_count - 1 + "You picked up an orange!" + else + "A dead tree has nothing to pick. :(" + end + end end + +=begin + +ot = OrangeTree.new +20.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) + +=end