From 96c4814192514b4fb0b5f741f1a61cea6f5f715f Mon Sep 17 00:00:00 2001 From: Nicole Shasha Date: Mon, 12 Sep 2016 04:45:58 +0100 Subject: [PATCH 1/2] Learn to program exercises --- ask.rb | 13 +++ ch10-nothing-new/dictionary_sort.rb | 22 +++- ch10-nothing-new/english_number.rb | 81 ++++++++++++- .../ninety_nine_bottles_of_beer.rb | 108 +++++++++++++++++- ch10-nothing-new/shuffle.rb | 20 +++- ch10-nothing-new/sort.rb | 25 +++- .../build_a_better_playlist.rb | 33 +++++- .../build_your_own_playlist.rb | 26 ++++- .../safer_picture_downloading.rb | 93 ++++++++++++++- ch12-new-classes-of-objects/birthdates.txt | 6 + .../birthday_helper.rb | 21 +++- ch12-new-classes-of-objects/happy_birthday.rb | 14 ++- .../one_billion_seconds.rb | 6 +- ...party_like_its_roman_to_integer_mcmxcix.rb | 16 ++- .../extend_built_in_classes.rb | 25 +++- .../interactive_baby_dragon.rb | 24 +++- ch13-creating-new-classes/orange_tree.rb | 74 ++++++++++-- .../better_program_logger.rb | 31 ++++- .../even_better_profiling.rb | 15 ++- ch14-blocks-and-procs/grandfather_clock.rb | 6 +- ch14-blocks-and-procs/program_logger.rb | 20 +++- old_school_roman_numerals.rb | 12 ++ roman_numerals.rb | 33 ++++++ 23 files changed, 684 insertions(+), 40 deletions(-) create mode 100644 ask.rb create mode 100644 ch12-new-classes-of-objects/birthdates.txt create mode 100644 old_school_roman_numerals.rb create mode 100644 roman_numerals.rb diff --git a/ask.rb b/ask.rb new file mode 100644 index 000000000..7fa4f93d1 --- /dev/null +++ b/ask.rb @@ -0,0 +1,13 @@ +def ask question + while true + puts question + reply = gets.chomp.downcase + if reply == 'yes' + return true + end + if reply == 'no' + return false + end +end +likes_it = ask 'Do you like eating tacos?' +puts likes_it diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..7e6314390 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_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 diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..28f08a75d 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 +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..0de884de5 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,107 @@ -# your code here \ No newline at end of file +def english_number number +if number < 0 +return 'Please enter a number that isn\'t negative.' +end +if number == 0 +return 'zero' +end +num_string = '' +ones_place = ['one', 'two', 'three', +'four', 'five', 'six', +'seven', 'eight', 'nine'] +tens_place = ['ten', 'twenty', 'thirty', +'forty', 'fifty', 'sixty', +'seventy', 'eighty', 'ninety'] +teenagers = ['eleven', 'twelve', 'thirteen', +'fourteen', 'fifteen', 'sixteen', +'seventeen', 'eighteen', 'nineteen'] + +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 + +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 + +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/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..03fbce347 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,19 @@ def shuffle arr - # your code here -end \ No newline at end of file + 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 +puts(shuffle([1, 2, 2, 323, 53, 54, 65, 7, 47, 54, 2, 8765, 44, 23, 48, 6, 2, 32, 6])) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..3b5471935 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 +puts(sort(['hello','this','should','be','sorted','into','the','right', 'order'])) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..0e69b6cb4 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,32 @@ def music_shuffle filenames - # your code here -end + filenames = filenames.sort + + len = filenames.length + 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 + 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..244ebb069 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,25 @@ -# your code here \ No newline at end of file +def shuffle arr + 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 +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..8c0bfca08 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,92 @@ -# your code here \ No newline at end of file +require 'win32ole' +STDOUT.sync = true +Thread.abort_on_exception = true +Dir.chdir 'C:\Documents and Settings\Chris\Desktop\pictureinbox' +pic_names = Dir['!undated/**/*.{jpg,avi}'] +thm_names = Dir['!undated/**/*.{thm}' ] +WIN32OLE.new("Scripting.FileSystemObject").Drives.each() do |x| +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') +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 encountered_error + +puts + +puts "Press [Enter] to finish." + +puts + +gets + +end diff --git a/ch12-new-classes-of-objects/birthdates.txt b/ch12-new-classes-of-objects/birthdates.txt new file mode 100644 index 000000000..068d3564a --- /dev/null +++ b/ch12-new-classes-of-objects/birthdates.txt @@ -0,0 +1,6 @@ +birth_dates['The King of Spain'] = 'Jan 5, 1938' +birth_dates['Francois Figgins'] = 'Feb 9, 1921' +birth_dates['Jebediah Gremble'] = 'Dec 30, 1999' +birth_dates['Sally Brule'] = 'May 13, 1979' +birth_dates['Barley Mussel'] = 'Oct 28, 1962' +birth_dates['Anne Boleyn'] = 'July 25, 1500' diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..d5b1b2790 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,20 @@ -# 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] = date +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/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..60d4dac6a 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,13 @@ -# your code here \ No newline at end of file +puts 'What year were you born?' +b_year = gets.chomp.to_i +puts 'What month were you born? (1-12)' +b_month = gets.chomp.to_i +puts 'What day of the month were you born?' +b_day = gets.chomp.to_i +b = Time.local(b_year, b_month, b_day) +t = Time.new +age = 1 +while Time.local(b_year + age, b_month, b_day) <= t +puts 'SPANK!' +age = age + 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..e5d70c2a3 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,5 @@ -# your code here \ No newline at end of file +time = Time.new +bday = Time.gm(1993, 02, 22, 02, 20, 22) +seconds_old = time - bday +time_left = 1000000000 - seconds_old +puts time+time_left 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..60d4dac6a 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,13 @@ -def roman_to_integer roman - # your code here -end \ No newline at end of file +puts 'What year were you born?' +b_year = gets.chomp.to_i +puts 'What month were you born? (1-12)' +b_month = gets.chomp.to_i +puts 'What day of the month were you born?' +b_day = gets.chomp.to_i +b = Time.local(b_year, b_month, b_day) +t = Time.new +age = 1 +while Time.local(b_year + age, b_month, b_day) <= t +puts 'SPANK!' +age = age + 1 +end diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..2570f9705 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,24 @@ +class Array +def shuffle +sort_by{rand} +end +end class Integer - # your code here -end \ No newline at end of file +def factorial +raise 'Must not use negative integer' if self < 0 +(self <= 1) ? 1 : self * (self-1).factorial +end +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 +p 7.factorial.to_roman.split(//).shuffle diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..898b2ffa4 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 '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 diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..58b762511 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -1,11 +1,67 @@ -# 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. - - 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 +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) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..fb2545360 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,28 @@ -def log desc, &block - # your code here -end \ No newline at end of file +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..3c8b429c0 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/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..5b4da4812 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,5 @@ def grandfather_clock &block - # your code here -end \ No newline at end of file + hour = (Time.new.hour + 11)%12 + 1 + hour.times(&block) +end +grandfather_clock { puts 'DONG!' } 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 diff --git a/old_school_roman_numerals.rb b/old_school_roman_numerals.rb new file mode 100644 index 000000000..58e46a9dd --- /dev/null +++ b/old_school_roman_numerals.rb @@ -0,0 +1,12 @@ +def old_roman_numeral num + 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(1999)) diff --git a/roman_numerals.rb b/roman_numerals.rb new file mode 100644 index 000000000..c0814c40a --- /dev/null +++ b/roman_numerals.rb @@ -0,0 +1,33 @@ +def roman_numeral num +hunds = (num % 1000 / 100) +tens = (num % 100 / 10) +ones = (num % 10 ) +roman = 'M' * thouscode here +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 + +puts(roman_numeral(1999)) From 961a0d3b5977d31d1447b5bd5f6bc8e2f71a603c Mon Sep 17 00:00:00 2001 From: Nicole Shasha Date: Mon, 12 Sep 2016 04:46:35 +0100 Subject: [PATCH 2/2] Ch09 exercises --- ch09-writing-your-own-methods/ask.rb | 14 ++++++-- .../old_school_roman_numerals.rb | 13 +++++-- .../roman_numerals.rb | 34 +++++++++++++++++-- 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..7fa4f93d1 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 -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + if reply == 'yes' + return true + end + if reply == 'no' + return false + end +end +likes_it = ask 'Do you like eating tacos?' +puts likes_it 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..58e46a9dd 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 + 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(1999)) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..c0814c40a 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,33 @@ def roman_numeral num - # your code here -end \ No newline at end of file +hunds = (num % 1000 / 100) +tens = (num % 100 / 10) +ones = (num % 10 ) +roman = 'M' * thouscode here +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 + +puts(roman_numeral(1999))