diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..d8102b452 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,12 @@ -def ask question - # your code here -end \ No newline at end of file +def ask question + while true + puts question + reply = gets.chomp.downcase + + return true if reply == 'yes' + return false if reply == 'no' + + puts 'Please answer "yes" or "no".' + end + answer + 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..3cee50bdf 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,3 +1,13 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + +thousand = 'M' * (num / 1000) +fivehundreds = thousand + 'D' * (num % 1000 / 500) +hundreds = fivehundreds + 'C' * (num % 500 / 100) +fifties = hundreds + 'L' * (num % 100 / 50) +tenth = fifties + 'X' * (num % 50 / 10) +fifth = tenth + 'V' * (num % 10 / 5) +units = fifth + 'I' * (num % 5 / 1) + +units + +end diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..f3ce82083 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,36 @@ def roman_numeral num - # your code here -end \ No newline at end of file +thousand = num / 1000 +hundreds = num % 1000 / 100 +tenth = num % 100 / 10 +units = num % 10 + +roman = 'M' * thousand + + if hundreds == 9 + roman = roman +'CM' + elsif hundreds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tenth == 9 + roman = roman + 'XC' + elsif tenth == 4 + roman = roman +'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if units == 9 + roman = roman + 'IX' + elsif units == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end +end + diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..07132da6e 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 +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_object| + if tested_object.downcase < smallest.downcase + still_unsorted.push smallest + smallest = tested_object + else + still_unsorted.push tested_object + end +end +sorted.push smallest +rec_dict_sort still_unsorted, sorted end \ No newline at end of file diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..970f5ab46 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,64 @@ 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..632d0f31d 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,10 @@ -# your code here \ No newline at end of file +num_at_start = 9999 +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 \ No newline at end of file diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..009f0881f 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,20 @@ def shuffle arr - # your code here + final_array = [] + while arr.length > 0 + rand_index = rand(arr.length) + intial_index = 0 + array = [] + arr.each do |w| + if intial_index == rand_index + final_array.push(w) + else + array.push(w) + end + + intial_index = intial_index + 1 + end + arr = array + end + +return final_array end \ No newline at end of file diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..121cf75e8 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,24 @@ -def sort arr - # your code here +def sort arr +rec_sort arr, [] +end + +def rec_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + +smallest = unsorted.pop +still_unsorted = [] + +unsorted.each do |tested_object| + if tested_object < smallest + still_unsorted.push smallest + smallest = tested_object + else + still_unsorted.push tested_object + end +end + +sorted.push smallest +rec_sort still_unsorted, sorted end \ 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..4923c8608 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 + + 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 + + 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 + diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..cd38d0be3 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,6 @@ -# your code here \ No newline at end of file +musics = shuffle(Dir['**/*.mp3']) +File.open 'playlist.m3u', 'w' do |f| +musics.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..cf5a68e92 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,24 @@ -# your code here \ No newline at end of file +Dir.chdir 'C:\Users\Laurent\Downloads' +pic_names = Dir['*.jpg'] + +puts 'What would you like to call this batch?' +batch_name = gets.chomp +puts +print "Downloading #{pic_names.length} files: " + +pic_number = 1 +pic_names.each do |name| +print '.' +if pic_number < 10 + new_name = "#{batch_name}0#{pic_number}.jpg" +else + new_name = "#{batch_name}#{pic_number}.jpg" +end + +if File.exist?(new_name) == false +File.rename name, new_name +elsif File.exist?(new_name) == true + exit +end +pic_number = pic_number + 1 +end \ No newline at end of file diff --git a/ch12-new-classes-of-objects/birthdates.txt b/ch12-new-classes-of-objects/birthdates.txt new file mode 100644 index 000000000..e5115be6a --- /dev/null +++ b/ch12-new-classes-of-objects/birthdates.txt @@ -0,0 +1,8 @@ +Christopher Alexander, Oct 4, 1936 +Christopher Lambert, Mar 29, 1957 +Christopher Lee, May 27, 1922 +Christopher Lloyd, Oct 22, 1938 +Christopher Pine, Aug 3, 1976 +Christopher Plummer, Dec 13, 1927 +Christopher Walken, Mar 31, 1943 +The King of Spain, Jan 5, 1938 \ No newline at end of file diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..4f8f61305 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,14 @@ -# your code here \ No newline at end of file +birth_dates = {} +File.read('birthdates.txt').each_line do |line| + line = line.chomp + comma = line.index(',') +$name = line[0..(comma - 1)] +$dob = line[-12..-1] + birth_dates[$name] = $dob +end + puts "Enter a name" + name = gets.chomp + puts (birth_dates[name])[0..5] + + + diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..a2cc48514 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file +puts "Enter your birth year in the following format: YYYY" +year = gets.chomp.to_i +puts "Enter your birth month in the following format: MM" +month = gets.chomp.to_i +puts "Enter your birth day in the following format: DD" +day = gets.chomp.to_i + +y = Time.new.year - year +m = Time.new.month - month +d = Time.new.day - day + if m - month > 0 +puts "You are #{y}years old" +puts "SPANK!" * y +elsif m -month == 0 and d - day >= 0 +puts "You are #{y}years old" +puts "SPANK!" * y +elsif m -month == 0 and d - day < 0 +puts "You are #{y-1}years old" +puts "SPANK!" * (y-1) +elsif m-month < 0 +puts "You are #{y-1}years old" +puts "SPANK!" * (y-1) +end diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..c0c016c9b 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,2 @@ -# your code here \ No newline at end of file +billion_seconds = (Time.local(1976, 8, 15, 21) + 3600) + 1000000000 +puts billion_seconds \ 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 c3e793933..fdeb70a10 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 + 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 + diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..4f9d26ef3 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,23 @@ -# your code here \ No newline at end of file + +puts "How do you want to call your dragon?" +name = gets.chomp +dragon_name = Dragon.new name + +while true +puts "What do you want to do to your dragon? feed? walk? put to bed? toss? rock?" +answer = gets.chomp.downcase + +if answer == "feed" + feed +elsif answer == "walk" + walk +elsif answer == "put to bed" + put_to_bed +elsif answer == "toss" + toss +elsif answer == "rock" + rock +else + puts "This is not a valid answer. Please enter feed or walk or put to bed or toss or rock" +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..3dba71319 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -5,7 +5,49 @@ # have the tree die after 25 years. # check out the rspec spec/ch13/orange_tree_spec.rb to see what strings we're looking for in the responses. - class OrangeTree - # your code here -end + def initialize + @age = 0 + @height = 0 + @oranges = 0 + end + + def height + if @age <= 25 + @height + else + "A dead tree is not very tall. :(" + end + end + + def one_year_passes + @age += 1 + @height = (@age * 0.4).round(1) + if @age <= 5 + @oranges = 0 + elsif @age <= 25 + @oranges = (@height * 15 - 25) + "This year your tree grew to #{@height}m tall, and produced #{@oranges.round} oranges." + elsif @age == 26 + "Oh, no! The tree is too old, and has died. :(" + else + "A year later, the tree is still dead. :(" + end + end + + def count_the_oranges + if @age <= 25 + @oranges.round + else + "A dead tree has no oranges. :(" + end + end + + def pick_an_orange + if @age <=25 + @oranges -= 1 + else + "A dead tree has nothing to pick. :(" + end + end +end \ 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..5cd52fd55 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,30 @@ -def log desc, &block - # your code here -end \ No newline at end of file +$logger_depth = 0 + +def better_log desc, &block + prefix = ' '*$logger_depth + + puts prefix + 'Beginning "' + desc + '"...' + + $logger_depth = $logger_depth + 1 + + result = block.call + + $logger_depth = $logger_depth - 1 + puts prefix + '..."' + desc + '" finished, returning: ' + result.to_s +end + +better_log 'outer block' do + better_log 'some little block' do + better_log 'teeny-tiny block' do + 'lOtS oF lOVe'.downcase + end + + 7 * 3 * 2 + end + + better_log 'yet another block' do + '!doof naidnI evol I'.reverse + end + + '0' == 0 +end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..23d8d31fd 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,28 @@ def profile block_description, &block - # your code here + + profiling_on = true + + if profiling_on + start_time = Time.new + block.call + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end +end + +profile '25000 doublings' do + number = 1 + 25000.times do + number = number + number + end + puts "#{number.to_s.length} digits" +end + +profile 'count to a million' do + number = 0 + 100000.times do + number = number + 1 + end end \ 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..26e29ed4a 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,17 @@ def grandfather_clock &block - # your code here + 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 \ No newline at end of file diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..3438199fb 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 +def program_log desc, &block + puts 'Beginning "' + desc + '"...' + result = block.call + puts '..."' + desc + '" finished, returning: ' + result.to_s +end + +program_log 'outer block' do + program_log 'some little block' do + 1**1 + 2**2 + end + + program_log 'yet another block' do + '!doof iahT ekil I'.reverse + end + + '0' == 0 +end