diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..6f2ef9e8d 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,34 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + input = gets.chomp.downcase + if input == 'yes' + return true + elsif input == 'no' + return false + else + puts "please answer yes or no" + end + end +end + +puts "Hello and thank you for participating" +puts + +ask "Do you like tacos?" +ask "Do you like burritos?" +wets_bed = ask "Do you wet the bed" +ask "Do you like chimichangas?" +ask "Do you like sopapillas?" +puts "just a couple more..." +ask "Do you like horchata?" +ask "Do you like flautas?" + +puts +puts "Debriefing:" +puts "Thank you for participating" +puts +puts +puts + +puts wets_bed 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..396bf12c2 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,14 @@ 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..72a358479 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,35 @@ def roman_numeral num - # your code here -end \ No newline at end of file + raise 'Must use positive integer' if num <= 0 + + digit_vals = [['I', 5, 1], + ['V', 10, 5], + ['X', 50, 10], + ['L', 100, 50], + ['C', 500, 100], + ['D', 1000, 500], + ['M', nil, 1000]] + + roman = '' + remaining = nil + + # Build string "roman" in reverse. + build_rev = proc do |l,m,n| + num_l = m ? (num % m / n) : (num / n) + full = m && (num_l == (m/n - 1)) + + if full && (num_l>1 || remaining) + # must carry + remaining ||= l # carry l if not already carrying + else + if remaining + roman << l + remaining + remaining = nil end + roman << l * num_l + end + end + digit_vals.each {|l,m,n| build_rev[l,m,n]} + + roman.reverse +end + +# puts(old_roman_numeral(1999)) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..8d5618b01 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,56 @@ -def dictionary_sort arr - # your code here -end \ No newline at end of file + +=begin +def sort some_array + recursive_sort some_array, [] +end + + +def recursive_sort unsorted_array, sorted_array + sorted_array = unsorted_array.sort { |x, y| x <=> y } +end + +puts sort ["meems", "meems", "freems", "frrops", "zeeps", "haha"] +=end + + + + +# The well-known quicksort algorithm. +=begin +def sort arr + return arr if arr.length <= 1 + + if arr.pop.capitalize != arr.pop + middle = arr.pop + less = arr.select{|x| x < middle} + more = arr.select{|x| x >= middle} + + elsif arr.pop.capitalize == arr.pop + middle = arr.pop + less = arr.select{|x| x < middle} + more = arr.select{|x| x >= middle} + + end +sort(less) + [middle] + sort(more) +end +p(sort(['blablabla', 'car', 'Alpha', 'zebra'])) +=end + +def super_sort unsorted_array + capitalized = unsorted_array.select {|x| x.capitalize == x} + uncapitalized = unsorted_array.select {|x| x.capitalize != x} + capitalized.sort! {|x, y| x <=> y } + uncapitalized.sort! {|x, y| x <=> y } + puts capitalized + puts + puts + puts uncapitalized +=begin + if x == x.capitalize + capitals << x + puts capitals +=end + +end + +super_sort ["baaa", "Baa", 'car', 'Car'] diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..e01167245 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,130 @@ def english_number number - # your code here + if number < 0 # No negative numbers. + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end + + # No more special cases! No more returns! + + num_string = '' # This is the string we will return. + + 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" is how much of the number + # we still have left to write out. + # "write" is the part we are + # writing out right now. + # write and left...get it? :) + 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 # How many zillions left? + left = left - write*zil_base # Subtract off those zillions. + + if write > 0 + # Now here's the recursion: + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end + end + + write = left/10 # How many tens left? + left = left - write*10 # Subtract off those tens. + + if write > 0 + if ((write == 1) and (left > 0)) + # Since we can't write "tenty-two" instead of + # "twelve", we have to make a special exception + # for these. + num_string = num_string + teenagers[left-1] + # The "-1" is because teenagers[3] is + # 'fourteen', not 'thirteen'. + + # Since we took care of the digit in the + # ones place already, we have nothing left to write. + left = 0 + else + num_string = num_string + tens_place[write-1] + # The "-1" is because tens_place[3] is + # 'forty', not 'thirty'. + end + + if left > 0 + # So we don't write 'sixtyfour'... + num_string = num_string + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + num_string = num_string + ones_place[write-1] + # The "-1" is because ones_place[3] is + # 'four', not 'three'. + end + + # Now we just return "num_string"... + num_string end + +# puts english_number( 0) +# puts english_number( 9) +# puts english_number( 10) +# puts english_number( 11) +# puts english_number( 17) +# puts english_number( 32) +# puts english_number( 88) +# puts english_number( 99) +# puts english_number(100) +# puts english_number(101) +# puts english_number(234) +# puts english_number(3211) +# puts english_number(999999) +# puts english_number(1000000000000) +# puts english_number(109238745102938560129834709285360238475982374561034) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..42ac44f40 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,15 @@ -# your code here \ No newline at end of file +require_relative 'english_number' + +@number = 100 + +while @number > 0 + if @number > 1 + puts ((english_number @number) + ' bottles of beer on the wall, ' + + (english_number @number) + ' Bottles of beer!') + puts ('Take one off the wall and pass it around or smth, ' + + (english_number (@number - 1)) + ' Bottles of beer on the wall!') + elsif @number == 1 + puts "One bottle of beer on the wall." + end + @number = @number - 1 +end diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..45cd45994 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,16 @@ def shuffle arr - # your code here -end \ No newline at end of file + random = Random.new.rand(1..3) + operand = arr.pop + c = 100 + while c > 0 + if random > 2 + operand.prepend arr + else + operand << arr + end + c = c - 1 + end + return arr +end + +shuffle(['hi', 'bye', 'yes', 'no']) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb deleted file mode 100644 index 44c6deb58..000000000 --- a/ch10-nothing-new/sort.rb +++ /dev/null @@ -1,3 +0,0 @@ -def sort arr - # your code here -end \ No newline at end of file diff --git a/ch10-nothing-new/test.rb b/ch10-nothing-new/test.rb new file mode 100644 index 000000000..e48180ce2 --- /dev/null +++ b/ch10-nothing-new/test.rb @@ -0,0 +1,7 @@ +arr = ['Cayman'] + +if arr.pop.capitalize == arr.pop + puts "It's capitalized" +else + puts "it isn't" +end diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..587526c9c 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,29 @@ -# your code here \ No newline at end of file +Dir.chdir '/users/michaelcalvey/Desktop/Music/' + +pic_names = Dir['F:/**/*.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 File.exists?("#{batch_name}#{pic_number}.jpg") == true + puts "File already exists, skipping..." + exit + else + new_name = if pic_number < 10 + "#{batch_name}#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" + end + end + File.rename name, new_name + pic_number = pic_number + 1 +end +puts diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..cb803fc93 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,39 @@ -# your code here \ No newline at end of file +puts "What year were you born?" +year = gets.chomp.to_i +puts "What month were you born" +month_str = gets.chomp.downcase +if month_str == "january" + month = 1 +elsif month_str == "february" + month = 2 +elsif month_str == "march" + month = 3 +elsif month_str == "april" + month = 4 +elsif month_str == "may" + month = 5 +elsif month_str == "june" + month = 6 +elsif month_str == "july" + month = 7 +elsif month_str == "august" + month = 8 +elsif month_str == "september" + month = 9 +elsif month_str == "october" + month = 10 +elsif month_str == "november" + month = 11 +elsif month_str == "december" + month = 12 +elsif month_str.to_1 < 13 +else + puts "you did not enter a valid month, application failed." +end + +puts "What day were you born?" +day = gets.chomp.to_i + +birthday = Time.local(year, month, day) + +puts ("Spank!" * ((Time.now - birthday) / 31536000)) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..7bc4dd250 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 +birthday = Time.local(1998, 05, 15, 12) +puts (birthday + 1000000000) 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..bc0107107 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,32 @@ 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 + roman.reverse.each_char do |c_or_C| + c = c_or_C.downcase + val = digit_vals[c] + if !val + puts 'This is not a valid roman numeral!' + return + end + + if val < prev + val *= -1 + else + prev = val + end + + total += val + end + + total +end + +puts(roman_to_integer('mcmxcix')) +puts(roman_to_integer('CCCLXV')) diff --git a/ch13-creating-new-classes/dice_roll.rb b/ch13-creating-new-classes/dice_roll.rb new file mode 100644 index 000000000..aa59a42a4 --- /dev/null +++ b/ch13-creating-new-classes/dice_roll.rb @@ -0,0 +1,24 @@ +class Die + + def initialize + roll + cheat + showing + end + + def roll + @number_showing = 1 + rand(6) + end + + def cheat + puts "enter number:" + @number_showing = gets.chomp.to_i + end + + def showing + puts @number_showing + end + +end + +die = Die.new diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..0b5d70cd6 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,77 @@ -# your code here \ No newline at end of file +class Dragon + + def initialize + puts "Please select a name" + @name = gets.chomp + @sleep = false + self.menu + end + + def feed + puts "Feeding #{@name}..." + sleep 2 + puts "MMMM #{@name} is fed!" + end + + def put_to_bed + puts "You put #{@name} to bed" + @sleep = true + end + + def asleep? + if @sleep + puts "The dragon is sleeping" + end + end + + def wake + if @sleep + @sleep = false + puts "The dragon is awake!" + else + puts "The dragon is already awake!" + end + end + + def menu + + puts "What would you like to do with #{@name}" + puts "Press 1 for feed, 2 for put to bed and three 3 for wake" + @input = gets.chomp.to_i + + if @input == 1 + self.feed + elsif @input == 2 + self.put_to_bed + elsif @input == 3 + self.wake + end +=begin + @@commands.each do |number, command| + if @input == number + self.(command) + else + puts "input not recognized" + end + end +=end + + puts "Would you like to leave the dragon? [yes/no]" + leave = gets.chomp.downcase + + if leave == 'yes' + exit + elsif leave == 'no' + self.menu + else + puts "input not recognised, please try again" + self.menu + end + + + end +end + + + +dragon = Dragon.new diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..f6bd20feb 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,34 @@ class OrangeTree - # your code here + + def initialize + @@age = 0 + @@height = 0 + while @@age < 25 + self.grow + if @@age > 5 + self.oranges + end + @@age = @@age + 1 + end + if @@age == 25 + self.dies + end + end + + def grow + puts "the tree grows. It is now 0.4 taller." + @@height = @@height + 0.4 + end + + def oranges + puts "The orange tree produces #{(@@age * 5)} oranges this year." + end + + def dies + puts "The tree grows old and dies" + end end + +OrangeTree.new diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..97b33ee90 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,12 @@ def log desc, &block - # your code here -end \ No newline at end of file + puts "Beginning #{desc}..." + block.call + puts "Finishing #{desc}..." +end + +log "Outer block" do + number = 5 + log "Inner block" do + puts " THIS IS THE INNER BLOCK!" + end +end diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..d06c20dd2 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,27 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + works = 'yes' + if works == 'yes' + start_time = Time.new + block.call + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + end +end + +profile '25000 doublings' do + number = 1 + + 25000.times do + number = number + number + end + + puts "#{number.to_s.length} digits" + puts "#{number}" +end + +profile 'count to a million' do + number = 0 + 1000000.times do + number = number + 1 + end +end diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..0cb802b6c 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,12 @@ def log desc, &block - # your code here -end \ No newline at end of file + puts "Beginning #{desc}..." + block.call + puts "Finishing #{desc}..." +end + +log "Outer block" do + number = 5 + log "Inner block" do + puts "THIS IS THE INNER BLOCK!" + end +end