diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..4092c6765 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,31 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + if (reply == 'yes' || reply == 'no') + if reply == 'yes' + return true + else + return false + end + else + puts 'Please answer "yes" or "no".' + end + end +end + +puts 'Hello, and thank you for...' +puts +ask 'Do you like eating tacos?' +ask 'Do you like eating burritos?' +wets_bed = ask 'Do you wet the bed?' +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 \ 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..cabbb572b 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,51 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + if num <= 0 + return '>0 only please!' + end + + #this will be our roman numeral string: + roman_numeral = '' + + # deal with 1000s + thousands = num/1000 + if thousands > 0 + roman_numeral = 'M'*thousands + num = num - thousands*1000 + end + + #deal wih 100s + hundreds = num/100 + if hundreds >= 5 + roman_numeral = roman_numeral + 'D' + 'C'*(hundreds-5) + elsif hundreds > 0 + roman_numeral = roman_numeral + 'C'*hundreds + end + num = num - hundreds*100 + + #deal wih 10s + tens = num/10 + if tens >= 5 + roman_numeral = roman_numeral + 'L' + 'X'*(tens-5) + elsif tens > 0 + roman_numeral = roman_numeral + 'X'*tens + end + num = num - tens*10 + + #deal wih 1s + if num >= 5 + roman_numeral = roman_numeral + 'V' + 'I'*(num-5) + elsif num > 0 + roman_numeral = roman_numeral + 'I'*num + end + + return roman_numeral + +end + +puts 'Translation:' +puts '3541 is ' + old_roman_numeral(3541) +puts '0 is '+ old_roman_numeral(0) +puts '-123 is ' + old_roman_numeral(-123) +puts '2077 is ' + old_roman_numeral(2077) +puts '2005 is ' + old_roman_numeral(2005) \ No newline at end of file diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..a97b2163c 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,61 @@ def roman_numeral num - # your code here -end \ No newline at end of file + if num <= 0 + return '>0 only please!' + end + + #this will be our roman numeral string: + roman_numeral = '' + + # deal with 1000s + thousands = num/1000 + if thousands > 0 + roman_numeral = 'M'*thousands + num = num - thousands*1000 + end + + #deal wih 100s + hundreds = num/100 + if hundreds == 9 + roman_numeral = roman_numeral + 'CM' + elsif hundreds >= 5 + roman_numeral = roman_numeral + 'D' + 'C'*(hundreds-5) + elsif hundreds == 4 + roman_numeral = roman_numeral + 'CD' + elsif hundreds > 0 + roman_numeral = roman_numeral + 'C'*hundreds + end + num = num - hundreds*100 + + #deal wih 10s + tens = num/10 + if tens == 9 + roman_numeral = roman_numeral + 'XC' + elsif tens >= 5 + roman_numeral = roman_numeral + 'L' + 'X'*(tens-5) + elsif tens == 4 + roman_numeral = roman_numeral + 'XL' + elsif tens > 0 + roman_numeral = roman_numeral + 'X'*tens + end + num = num - tens*10 + + #deal wih 1s + if num == 9 + roman_numeral = roman_numeral + 'IX' + elsif num >= 5 + roman_numeral = roman_numeral + 'V' + 'I'*(num-5) + elsif num == 4 + roman_numeral = roman_numeral + 'IV' + elsif num > 0 + roman_numeral = roman_numeral + 'I'*num + end + + return roman_numeral +end + +puts 'Translation:' +puts '3541 is ' + roman_numeral(3541) +puts '0 is '+ roman_numeral(0) +puts '-123 is ' + roman_numeral(-123) +puts '2077 is ' + roman_numeral(2077) +puts '2005 is ' + roman_numeral(2005) \ No newline at end of file diff --git a/ch10-nothing-new/aga.jpg b/ch10-nothing-new/aga.jpg new file mode 100644 index 000000000..c6ca9968d Binary files /dev/null and b/ch10-nothing-new/aga.jpg differ diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..cf9825267 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,31 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + recursive_sort arr, [] +end + +#the method that actually does sorting +def recursive_sort unsorted_array, sorted_array + j = 1 + + smallest_index = 0 + + until unsorted_array.length == 0 + + # find smallest in unsorted_array + while j < unsorted_array.length + smallest_index = j if unsorted_array[j].to_s.downcase < unsorted_array[smallest_index].to_s.downcase + j += 1 + end + + # move it from the unsorted array to the end of already_sorted + sorted_array << unsorted_array[smallest_index] + unsorted_array.delete_at(smallest_index) + + # find next smallest with recursive call + recursive_sort unsorted_array, sorted_array + end + + return sorted_array +end + +puts dictionary_sort ["avada", "Kedavra", "black", "SirIUs", "harry"] +puts dictionary_sort ["A","feel", "can", "like", "can","singing"] \ No newline at end of file diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..ab80a51e3 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,109 @@ 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 = '' # 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'] + higher_powers = ['thousand','million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', + 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', + 'sedecillion', 'septendecillion', 'octodecillion', 'novendecillion', 'vigintillion'] + + # "left" is how much of the number we still have left to write out. + # "write" is the part we are writing out right now. + left = number + write = 0 + + #deal with thousands up to vigintillions + #start from vigintillion and go down to find the lowest power when write is not 0 + power = 63 + i = 20 + + until power == 0 + ten_power = 10**power + write = left/ten_power + left = left - write*ten_power + if write > 0 + illions = english_number write + num_string = num_string + illions + ' ' + higher_powers[i] + if left > 0 + # Add space between thousands and hundreds + num_string = num_string + ' ' + end + end + power -= 3 + i -= 1 + end + + #deal with hundreds + write = left/100 # How many hundreds left? + left = left - write*100 # Subtract off those hundreds. + if write > 0 + # Now here's the recursion: + hundreds = english_number write + num_string = num_string + hundreds + ' hundred' + if left > 0 + # So we don't write 'two hundredfifty-one'... + num_string = num_string + ' ' + end + end + + #deal with tens + write = left/10 + left = left - write*10 + if write > 0 + #teenagers + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + # The "-1" is because teenagers[3] is 'fourteen', not 'thirteen'. + 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 + # Add space between tens and ones + num_string = num_string + '-' + end + end + + #deal with ones + write = left + left = 0 + if write > 0 + num_string = num_string + ones_place[write-1] + # The "-1" is because ones_place[3] is 'four', not 'three'. + end + + # TA DA! + 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(10000) +puts english_number(999999) +puts english_number(1100458) +puts english_number(1000000000000) +puts english_number(10**63 + 10**60 + 10**24 + 10*6 + 1215) + diff --git a/ch10-nothing-new/jacqui.jpg b/ch10-nothing-new/jacqui.jpg new file mode 100644 index 000000000..62ed2dc64 Binary files /dev/null and b/ch10-nothing-new/jacqui.jpg differ diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..0cadc5658 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 + return 'Please enter a number that isn\'t negative.' + end + + if number == 0 + return 'zero' + end + + 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'] + higher_powers = ['thousand','million', 'billion', 'trillion', 'quadrillion', 'quintillion', 'sextillion', 'septillion', + 'octillion', 'nonillion', 'decillion', 'undecillion', 'duodecillion', 'tredecillion', 'quattuordecillion', 'quindecillion', + 'sedecillion', 'septendecillion', 'octodecillion', 'novendecillion', 'vigintillion'] + + # "left" is how much of the number we still have left to write out. + # "write" is the part we are writing out right now. + left = number + write = 0 + + #deal with thousands up to vigintillions + #start from vigintillion and go down to find the lowest power when write is not 0 + power = 63 + i = 20 + + until power == 0 + ten_power = 10**power + write = left/ten_power + left = left - write*ten_power + if write > 0 + illions = english_number write + num_string = num_string + illions + ' ' + higher_powers[i] + if left > 0 + # Add space between thousands and hundreds + num_string = num_string + ' ' + end + end + power -= 3 + i -= 1 + end + + #deal with hundreds + write = left/100 # How many hundreds left? + left = left - write*100 # Subtract off those hundreds. + if write > 0 + # Now here's the recursion: + hundreds = english_number write + num_string = num_string + hundreds + ' hundred' + if left > 0 + # So we don't write 'two hundredfifty-one'... + num_string = num_string + ' ' + end + end + + #deal with tens + write = left/10 + left = left - write*10 + if write > 0 + #teenagers + if ((write == 1) and (left > 0)) + num_string = num_string + teenagers[left-1] + # The "-1" is because teenagers[3] is 'fourteen', not 'thirteen'. + 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 + # Add space between tens and ones + num_string = num_string + '-' + end + end + + #deal with ones + write = left + left = 0 + if write > 0 + num_string = num_string + ones_place[write-1] + # The "-1" is because ones_place[3] is 'four', not 'three'. + end + + # TA DA! + num_string + +end + +def beer_song number + while number > 0 + puts english_number(number).capitalize + " bottles of beer on the wall, " + english_number(number) + " bottles of beer." + puts "Take one down and pass it around, " + english_number(number-1) + " bottles of beer on the wall." + number -= 1 + end +end + +beer_song 9999 \ No newline at end of file diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..615f357cc 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,29 @@ +=begin +Defining perfect shuffle as one where no card happens after the same card it did before. +Implementation here inspired by the "faro in-shuffle" +=end + def shuffle arr - # your code here -end \ No newline at end of file + #split the array in two + left, right = arr.each_slice((arr.size/2.0).round).to_a + + #generate new array where the two arrays intertwine + i = 0 + shuffled = [] + while i < right.length + shuffled << left[i] + shuffled << right[i] + i += 1 + end + shuffled << left[i] if i < left.length # for case when arr.length is odd + + #move the first element to the end + shuffled << shuffled[0] + shuffled.delete_at(0) + + return shuffled + +end + +puts shuffle([1,2,3,4,5]).to_s +puts shuffle([10,20,30,40]).to_s \ No newline at end of file diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..8d90719f3 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,32 @@ def sort arr - # your code here -end \ No newline at end of file +# wrapper method; pass given array for sorting, along with an empty array + recursive_sort arr, [] +end + +#the method that actually does sorting +def recursive_sort unsorted_array, sorted_array + j = 1 + + smallest_index = 0 + + until unsorted_array.length == 0 + # find smallest in unsorted_array + while j < unsorted_array.length + smallest_index = j if unsorted_array[j] < unsorted_array[smallest_index] + j += 1 + end + + # move it from the unsorted array to the end of already_sorted + sorted_array << unsorted_array[smallest_index] + unsorted_array.delete_at(smallest_index) + + # find next smallest with recursive call + recursive_sort unsorted_array, sorted_array + end + + return sorted_array +end + +puts sort ["avada", "kedavra", "black", "sirius", "harry"] +puts sort ["a","feel", "can", "like", "can","singing"] + diff --git a/ch10-nothing-new/tom.jpg b/ch10-nothing-new/tom.jpg new file mode 100644 index 000000000..e69de29bb diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..03c6aed01 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,20 @@ def music_shuffle filenames - # your code here + return filenames.shuffle! end + +# search for music files in current directory +songs = Dir['*.mp3'] + +#create the playlist +filename = 'playlist.m3u' + +songs = music_shuffle songs + +songs_string = '' +songs.each do |song| + songs_string << song << "\n" +end + +File.open filename, 'w' do |f| + f.write songs_string +end \ No newline at end of file diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..913b96ebc 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,16 @@ -# your code here \ No newline at end of file +# search for music files in current directory +songs = Dir['*.mp3'] + +#create the playlist +filename = 'playlist.m3u' + +songs.shuffle! + +songs_string = '' +songs.each do |song| + songs_string << song << "\n" +end + +File.open filename, 'w' do |f| + f.write songs_string +end diff --git a/ch11-reading-and-writing/playlist.m3u b/ch11-reading-and-writing/playlist.m3u new file mode 100644 index 000000000..7f281447e --- /dev/null +++ b/ch11-reading-and-writing/playlist.m3u @@ -0,0 +1,4 @@ +song2.mp3 +song.mp3 +song3.mp3 +song4.mp3 diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..12801272c 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,28 @@ -# your code here \ No newline at end of file +pic_names = Dir['**/*.{JPG,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 '.' # "progress bar". + + new_name = if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" + end + + if File.exist?(new_name) + puts "File already exists" + exit + else + File.rename name, new_name + end + + pic_number = pic_number + 1 +end + +puts +puts 'Done!' \ No newline at end of file diff --git a/ch11-reading-and-writing/song.mp3 b/ch11-reading-and-writing/song.mp3 new file mode 100644 index 000000000..e69de29bb diff --git a/ch11-reading-and-writing/song2.mp3 b/ch11-reading-and-writing/song2.mp3 new file mode 100644 index 000000000..e69de29bb diff --git a/ch11-reading-and-writing/song3.mp3 b/ch11-reading-and-writing/song3.mp3 new file mode 100644 index 000000000..e69de29bb diff --git a/ch11-reading-and-writing/song4.mp3 b/ch11-reading-and-writing/song4.mp3 new file mode 100644 index 000000000..e69de29bb diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..d61bb4f84 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,32 @@ -# your code here \ No newline at end of file +#read in names and birth dates from file +read_string = File.read 'dob' + +#split by newline to obtain seperate people -> though could have actually also just used each_line on the read_string maybe +list = read_string.split("\n") + +hash_list = Hash.new + +#convert to hashes +list.each do |person| + data = person.split(',',2) #returns array of 2 strings, split by first occurence of , + hash_list[data[0]] = data[1].lstrip! +end + +#Time for user interaction: +puts "Name please" +name = gets.chomp + +if hash_list[name] == nil + puts "sorry, not got them on me list" + exit +end + +birthday_month, birthday_day = hash_list[name].split(',')[0].split +birthday_year = hash_list[name].split(',')[1] + +year = '2016' +year = '2017' if Time.new > Time.gm(year, birthday_month, birthday_day) + +age = (Time.gm(year, birthday_month, birthday_day) - Time.gm(birthday_year, birthday_month, birthday_day)) / (60*60*24*365) + +puts "#{name}'s next birthday is #{birthday_day} #{birthday_month} #{year}. He will be #{age.round} years old." diff --git a/ch12-new-classes-of-objects/dob b/ch12-new-classes-of-objects/dob new file mode 100644 index 000000000..e5115be6a --- /dev/null +++ b/ch12-new-classes-of-objects/dob @@ -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/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..f91694852 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,16 @@ -# your code here \ No newline at end of file +=begin +Ask what year a person was born in, then the month, +and then the day. Figure out how old they are, and give them a big SPANK! +for each birthday they have had. +=end + +puts "what year were you born?" +year = gets.chomp +puts "what month?" +month = gets.chomp +puts "what day?" +day = gets.chomp + +age = Time.new - Time.local(year,month,day) #gives age in seconds... convert to years: +age = age/(60*60*24*365) +puts "SPANK!\n"*age.round diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..38c4dc695 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 +born = Time.local(1991, 7, 9) + +puts "was born on: " + born.to_s +puts "will be 1 billion seconds old on: #{born + 10**9}" \ 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..706b331ff 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,82 @@ def roman_to_integer roman - # your code here -end \ No newline at end of file + + # only strings with letters I, V, X, L, C, D, M allowed + if roman =~ /[^ivxlcdm]/i + puts "Not very Roman of you..." + exit + end + + #make my life easier, remove case + roman.downcase! + + #will store the final number + number = 0 + + while roman.length > 0 + #let's do it letter by letter + if roman[0] == 'm' + number = number + 1000 + roman.slice!(/m/) + elsif roman[0] == 'd' + number = number + 500 + roman.slice!(/d/) + elsif roman[0] == 'c' + if roman[1] == 'm' + number = number + 900 + roman.slice!(/cm/) + elsif roman[1] == 'd' + number = number + 400 + roman.slice!(/cd/) + else + number = number + 100 + roman.slice!(/c/) + end + elsif roman[0] == 'l' + number = number + 50 + roman.slice!(/l/) + elsif roman[0] == 'x' + if roman[1] == 'c' + number = number + 90 + roman.slice!(/xc/) + elsif roman[1] == 'l' + number = number + 40 + roman.slice!(/xl/) + else + number = number + 10 + roman.slice!(/x/) + end + elsif roman[0] == 'v' + number = number + 5 + roman.slice!(/v/) + elsif roman[0] == 'i' + if roman[1] == 'x' + number = number + 9 + roman.slice!(/ix/) + elsif roman[1] == 'v' + number = number + 4 + roman.slice!(/iv/) + else + number = number + 1 + roman.slice!(/i/) + end + end + + end + + return number + +end + +puts roman_to_integer 'i' #1 +puts roman_to_integer 'iiI' #3 +puts roman_to_integer 'Iv' #4 +puts roman_to_integer 'V' #5 +puts roman_to_integer 'VIII' #8 +puts roman_to_integer 'XXVI' #26 +puts roman_to_integer 'LIII' #53 +puts roman_to_integer 'CXCIX' #199 +puts roman_to_integer 'DCLXXXII' #682 +puts roman_to_integer 'MMCMXLI' #2941 +puts roman_to_integer 'MMMX' #3010 +puts roman_to_integer 'mcmxcix' #1999 +puts roman_to_integer 'CCCLXV' #365 \ 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..9116be46b 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,61 @@ class Integer - # your code here -end \ No newline at end of file + + def factorial + return 'You can\'t take the factorial of a negative number!' if self < 0 + return 1 if self <= 1 + self * (self-1).factorial + end + + def to_roman + + num = self + + if num <= 0 + return '>0 only please!' + end + + #this will be our roman numeral string: + roman_numeral = '' + + # deal with 1000s + thousands = num/1000 + if thousands > 0 + roman_numeral = 'M'*thousands + num = num - thousands*1000 + end + + #deal wih 100s + hundreds = num/100 + if hundreds >= 5 + roman_numeral = roman_numeral + 'D' + 'C'*(hundreds-5) + elsif hundreds > 0 + roman_numeral = roman_numeral + 'C'*hundreds + end + num = num - hundreds*100 + + #deal wih 10s + tens = num/10 + if tens >= 5 + roman_numeral = roman_numeral + 'L' + 'X'*(tens-5) + elsif tens > 0 + roman_numeral = roman_numeral + 'X'*tens + end + num = num - tens*10 + + #deal wih 1s + if num >= 5 + roman_numeral = roman_numeral + 'V' + 'I'*(num-5) + elsif num > 0 + roman_numeral = roman_numeral + 'I'*num + end + + return roman_numeral + + end + +end + +puts 3.factorial +puts 30.factorial +puts 4.to_roman +puts 3567.to_roman diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..7dfe9711a 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,126 @@ -# 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 + + def interact + puts "What will you do, mommy?" + + action = gets.chomp.downcase + + case action + when 'feed' + self.feed + when 'walk' + self.walk + when 'put to bed' + self.put_to_bed + when 'toss' + self.toss + when 'rock' + self.rock + else + puts "You can't do THAT!" + end + + end + + + private + + def hungry? + @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 + +pet = Dragon.new("Basia") +10.times do + pet.interact +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..a46e4f50b 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,73 @@ class OrangeTree - # your code here + def initialize + @h = 0 + @age = 0 + @orange_count = 0 + end + + def one_year_passes + @age +=1 + @orange_count = 0 #just to make sure all oranges fell off + if @age > 25 + death + else + grow + if @age > 5 + make_fruit + end + "This year your tree grew to #{@h.round(1)}m tall, and produced #{@orange_count.round} oranges." + end + end + + def count_the_oranges + if @age > 25 + 'A dead tree has no oranges. :(' + else + @orange_count.round + end + end + + def height + if @age > 25 + 'A dead tree is not very tall. :(' + else + @h.round(1) + end + end + + def pick_an_orange + return 'A dead tree has nothing to pick. :(' if @age > 25 + if @orange_count > 0 + @orange_count -= 1 + 'Mmmmmmmmm. Delicious.' + else + 'No oranges. Sadness. :(' + end + end + + private + + def grow + @h += 0.4 + end + + def death + if @age == 26 + 'Oh, no! The tree is too old, and has died. :(' + else + 'A year later, the tree is still dead. :(' + end + end + + def make_fruit + @orange_count = @h * 15 - 25 + end + +end + +tree = OrangeTree.new +28.times do + puts tree.one_year_passes end diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..56dba0336 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 +$level = 0 + +def better_log desc, &block + puts "#{' '*$level}Beginning \"#{desc}\"..." + $level += 1 + result = block.call + $level -= 1 + puts "#{' '*$level}...\"#{desc}\" finished, returning: #{result}" +end + +better_log 'outer block' do + better_log 'some little block' do + better_log 'tiny block' do + 10+11 + end + 3+2 + end + better_log 'yet another block' do + 'I like Thai food!' + end + 2>3 end \ No newline at end of file diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..e191b7051 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,28 @@ +$PROFILING_ON = true + def profile block_description, &block - # your code here + 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" +# That's the number of digits in this HUGE number. +end + +profile 'count to a million' do + number = 0 + 1000000.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..82ff6a1ee 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,11 @@ def grandfather_clock &block - # your code here + hours = Time.new.hour + hours -= 12 if hours > 12 + hours.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..5fd122e3a 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,14 @@ -def log desc, &block - # your code here +def program_log desc, &block + puts "Beginning \"#{desc}\"..." + puts "...\"#{desc}\" finished, returning: #{block.call}" +end + +program_log 'outer block' do + program_log 'some little block' do + 3+2 + end + program_log 'yet another block' do + 'I like Thai food!' + end + 2>3 end \ No newline at end of file