diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..56b116edd 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 + elsif reply == 'no' + return false + else + puts 'Please answer "yes" or "no".' + end +end +end 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..9cee784bf 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 + +oldrom = "" + +oldrom = oldrom + 'M' * (num / 1000) +oldrom = oldrom + 'D' * (num % 1000 / 500) +oldrom = oldrom + 'C' * (num % 500 / 100) +oldrom = oldrom + 'L' * (num % 100 / 50) +oldrom = oldrom +'X' * (num % 50 / 10) +oldrom = oldrom + 'V' * (num % 10 / 5) +oldrom = oldrom + 'I' * (num % 5 / 1) +oldrom +end diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..a297cf836 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,42 @@ def roman_numeral num - # your code here -end \ No newline at end of file + #changes only to 4 and 9 in each tier + + newrom = "" + + thou = (num /1000) + hund = (num %1000 / 100) + ten = (num % 100 /10) + one = (num % 10) + + newrom = 'M' * thou + + if hund == 9 + newrom = newrom + 'CM' + elsif hund == 4 + newrom = newrom + 'CD' + else + newrom = newrom + 'D' * (num % 1000 / 500) + newrom = newrom + 'C' * (num % 500/ 100) + end + + if ten == 9 + newrom = newrom + 'XC' + elsif ten == 4 + newrom = newrom + 'XL' + else + newrom = newrom + 'L' * (num % 100 / 50) + newrom = newrom + 'X' * (num % 50 / 10) + end + + if one == 9 + newrom = newrom + 'IX' + elsif one == 4 + newrom = newrom + 'IV' + else + newrom = newrom + 'V' * (num % 10 / 5) + newrom = newrom + 'I' * (num % 5 / 1) + end + newrom +end + +puts roman_numeral 1992 diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..d5b2f4920 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,11 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + return arr if arr.length <=1 + + middle = arr.pop + less = arr.select{|x| x.downcase < middle.downcase} + more = arr.select{|x| x.downcase >= middle.downcase} + + dictionary_sort(less) + [middle] + dictionary_sort(more) +end + +puts dictionary_sort ['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog'] diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..6b52579c1 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,87 @@ 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 + +puts english_number 25 +puts english_number 0 +puts english_number 1000000000 +puts english_number 55986745241657 diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..98905ccce 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,96 @@ -# 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 = 10 +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..076d0f212 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,25 @@ 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 [10,11,12,13,14,15,16,17,18,19,20] diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..14af7b5f1 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,11 @@ def sort arr - # your code here -end \ No newline at end of file + return arr if arr.length <= 1 + + middle = arr.pop + less = arr.select{|x| x < middle} + more = arr.select{|x| x >= middle} + + sort(less) + [middle] + sort(more) +end + +puts(sort(['i', 'feel', 'like', 'dancing']).join(' ')) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..f52485078 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,36 @@ def music_shuffle filenames - # your code here -end + filenames = filenames.sort + len = filenames.length + + 2.times do + l_index = 0 + r_index = len/2 + shuf = [] + + while shuf.length < len + if shuf.length%2 == 0 + shuf.push(filenames[r_index]) + r_index = r_index + 1 + else + shuf.push(filenames[l_index]) + l_index = l_index + 1 + end + end + + filenames = shuf + end + + arr = [] + cut = rand(len) + index = 0 + + while index < len + arr.push(filenames[(index+cut)%len]) + index = index + 1 + end + + arr + end + songs = ['RayCharles/HitTheRoadJack', 'RayCharles/GeorgiaOnMyMind', 'RayCharles/IGotAWoman', 'ArethaFranklin/Respect', 'ArethaFranklin/NaturalWoman', 'ArethaFranklin/ISayALittlePrayer'] + + 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..ac0fbae6c 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,13 @@ -# your code here \ No newline at end of file +def shuffle arr + arr.shuffle +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..ddebd5379 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,98 @@ -# your code here \ No newline at end of file +# For Katy, with love. + +### Download pictures from camera card. + +require 'win32ole' + +STDOUT.sync = true +Thread.abort_on_exception = true + +Dir.chdir 'C:\Documents and Settings\Chris\Desktop\pictureinbox' + +# Always look here for pics. + +pic_names = Dir['!undated/**/*.{jpg,avi}'] +thm_names = Dir['!undated/**/*.{thm}' ] + +# Scan for memory cards in the card reader. +WIN32OLE.new("Scripting.FileSystemObject").Drives.each() do |x| + #driveType 1 is removable disk + if x.DriveType == 1 && x.IsReady + pic_names += Dir[x.DriveLetter+':/**/*.{jpg,avi}'] + thm_names += Dir[x.DriveLetter+':/**/*.{thm}' ] + end +end + +months = %w(jan feb mar apr may jun jul aug sep oct nov dec) + +encountered_error = false + +print "Downloading #{pic_names.size} files: " + +pic_names.each do |name| + print '.' + is_movie = (name[-3..-1].downcase == 'avi') + + if is_movie + orientation = 0 + new_name = File.open(name) do |f| + f.seek(0x144,IO::SEEK_SET) + f.read(20) + end + + new_name[0...3] = '%.2d' % (1 + months.index(new_name[0...3].downcase)) + new_name = new_name[-4..-1] + ' ' + new_name[0...-5] + else + new_name, orientation = File.open(name) do |f| + f.seek(0x36, IO::SEEK_SET) + orientation_ = f.read(1)[0] + f.seek(0xbc, IO::SEEK_SET) + new_name_ = f.read(19) + [new_name_, orientation_] + end + end + + [4,7,10,13,16].each {|n| new_name[n] = '.'} + if new_name[0] != '2'[0] + encountered_error = true + puts "\n"+'ERROR: Could not process "'+name+ + '" because it\'s not in the proper format!' + next + end + + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') + # Make sure we don't save over another file!! + while FileTest.exist? save_name + new_name += 'a' + save_name = new_name + (is_movie ? '.orig.avi' : '.jpg') + end + + + case orientation + when 6 + `convert "#{name}" -rotate "90>" "#{save_name}"` + File.delete name + when 8 + `convert "#{name}" -rotate "-90>" "#{save_name}"` + File.delete name + else + File.rename name, save_name + end +end + +print "\nDeleting #{thm_names.size} THM files: " +thm_names.each do |name| + print '.' + File.delete name +end + +# If something bad happened, make sure she + +# sees the error message before the window closes. + +if encountered_error + puts + puts "Press [Enter] to finish." + puts + gets +end diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..8ff9d2525 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,25 @@ -# 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 "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..7c784891e 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 "What year were you born in?" +year = gets.chomp.to_i + +puts "What month were you born in? (Jan = 1, Feb = 2, etc.)" +month = gets.chomp.to_i + +puts "On what day were you born?" +day = gets.chomp.to_i + +bdaytime = Time.local(year, month, day) +now = Time.new + +age = 1 + +while Time.local(year + age, month, day) <= now + 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..784780eeb 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(1992, 8, 11, 11, 32, 36) +time2 = time + 1000000000 + +puts time2 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..8f113d512 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,33 @@ 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 + +puts(roman_to_integer('mcmlxxxv')) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..57029f1b7 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,22 @@ 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..2e7cfcc64 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,17 @@ -# 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 +obj = Object.new + +while true + puts + puts 'commands: feed, toss, walk, rock, put_to_bed, exit' + command = gets.chomp + if command == 'exit' + exit + elsif pet.respond_to?(command) && !obj.respond_to?(command) + pet.send command + 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..6d449d484 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,58 @@ class OrangeTree - # your code here + def initialize + @height = 0 + @orange_count = 0 + @alive = true + end + + def height + if @alive + @height.round(1) + else + 'A dead tree is not very tall. :(' + end + end + + def count_the_oranges + if @alive + @orange_count + else + 'A dead tree has no oranges. :(' + end + end + + def one_year_passes + if @alive + @height = @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.round(1)}m tall," + + " and produced #{@orange_count} oranges." + else + "This year your tree grew to #{@height.round(1)}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 diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..ed0b74364 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,25 @@ -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.inspect}..." + $logger_depth += 1 + result = block[] + $logger_depth -= 1 + puts prefix+"...#{desc.inspect} finished, returning: #{result}" +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..133aa67d5 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,11 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + 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 diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..705584a4b 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,19 @@ 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..b9003e363 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.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 diff --git a/push b/push new file mode 100644 index 000000000..e69de29bb