From ce7eac8cceacdb581c5c72054e641bce5dd9373d Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 21:06:55 +0000 Subject: [PATCH 01/54] ch09 ask.rb complete --- ch09-writing-your-own-methods/ask.rb | 31 +++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..76f067b90 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,32 @@ 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 + end + end +end + +puts 'Hello, and thank you for...' + +puts + +ask 'Do you like eating tacos?' # Ignore this return value +ask 'Do you like eating burritos?' # And this one +wets_bed = ask 'Do you wet the bed?' # Save this return value +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 From 0ac22c67c291ca32c94fa0f1ff509959e8829b3a Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 21:51:22 +0000 Subject: [PATCH 02/54] ch09 roman_numerals.rb --- .../roman_numerals.rb | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..35edc7f81 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,20 @@ def roman_numeral num # your code here -end \ No newline at end of file + num = num.to_i + (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) + + numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"1" } + roman = "" + + numerals.keys.each do |k| + n = num/k + if n > 0 + roman += numerals[k]*n + num = num % k + end + end + + puts roman +end + +roman_numeral (gets.chomp) \ No newline at end of file From 3544e789bf40c09dd0a7ab7432765aa039ff7858 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:03:06 +0000 Subject: [PATCH 03/54] ch9 roman numerals programs --- .../old_school_roman_numerals.rb | 19 ++++++++++++++++++- .../roman_numerals.rb | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) 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..d3c70b21a 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,20 @@ def old_roman_numeral num # your code here -end \ No newline at end of file + num = num.to_i + (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) + + numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"1" } + roman = "" + + numerals.keys.each do |k| + n = num/k + if n > 0 + roman += numerals[k]*n + num = num % k + end + end + + puts roman +end + +old_roman_numeral (gets.chomp) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 35edc7f81..001e4f4b0 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -3,7 +3,7 @@ def roman_numeral num num = num.to_i (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) - numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"1" } + numerals = {1000=>"M", 500=>"D", 400=>"CD,", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 5=>"V", 4=>"IV", 1=>"1" } roman = "" numerals.keys.each do |k| From 6e3b3a37d41ea888c6b006b9459fbf32ff072678 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:12:09 +0000 Subject: [PATCH 04/54] ch9 roman numerals programs --- ch09-writing-your-own-methods/roman_numerals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 001e4f4b0..cfb3ccdba 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -17,4 +17,4 @@ def roman_numeral num puts roman end -roman_numeral (gets.chomp) \ No newline at end of file +#roman_numeral (gets.chomp) \ No newline at end of file From a0108cf5913c7c8810b65b198ec4b4724e907bd9 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:13:49 +0000 Subject: [PATCH 05/54] ch9 roman numerals programs --- ch09-writing-your-own-methods/old_school_roman_numerals.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 d3c70b21a..c0c493178 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -17,4 +17,4 @@ def old_roman_numeral num puts roman end -old_roman_numeral (gets.chomp) +#old_roman_numeral (gets.chomp) From 720836631d278fc9b0638503fd4006404f5fed0a Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:16:24 +0000 Subject: [PATCH 06/54] ch9 programs edited --- ch09-writing-your-own-methods/ask.rb | 21 +------------------ .../old_school_roman_numerals.rb | 6 +----- .../roman_numerals.rb | 5 +---- 3 files changed, 3 insertions(+), 29 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 76f067b90..36e9d8188 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,5 +1,4 @@ def ask question - # your code here while true puts question reply = gets.chomp.downcase @@ -11,22 +10,4 @@ def ask question end end end -end - -puts 'Hello, and thank you for...' - -puts - -ask 'Do you like eating tacos?' # Ignore this return value -ask 'Do you like eating burritos?' # And this one -wets_bed = ask 'Do you wet the bed?' # Save this return value -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 +end \ No newline at end of file diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index c0c493178..bea2fe5ca 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,6 +1,5 @@ def old_roman_numeral num - # your code here - num = num.to_i + num = num.to_i (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"1" } @@ -13,8 +12,5 @@ def old_roman_numeral num num = num % k end end - puts roman end - -#old_roman_numeral (gets.chomp) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index cfb3ccdba..81329002f 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,5 +1,4 @@ def roman_numeral num - # your code here num = num.to_i (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) @@ -15,6 +14,4 @@ def roman_numeral num end puts roman -end - -#roman_numeral (gets.chomp) \ No newline at end of file +end \ No newline at end of file From 69ef343dd6b238fa1434cba4687bb958f2b0741a Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:19:14 +0000 Subject: [PATCH 07/54] ch10 english number --- ch10-nothing-new/english_number.rb | 168 ++++++++++++++++++++++++++++- 1 file changed, 166 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..256fb5278 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,167 @@ -def english_number number - # your code here +def englishNumber number + 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! + + numString = '' # This is the string we will return. + + onesPlace = ['one', 'two', 'three', 'four', 'five', + 'six', 'seven', 'eight', 'nine'] + tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty', + 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', + 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + + # "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 + +#### ------------> TRILLIONS + +write = left/1000000000000 # How many millions left to write out? + left = left - write*1000000000000 # Subtract off those millions. + +if write > 0 + # trillions + trillions = englishNumber write + numString = numString + trillions + ' trillion' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end + +#### ------------> BILLIONS + +write = left/1000000000 # How many millions left to write out? + left = left - write*1000000000 # Subtract off those millions. + +if write > 0 + # billions + billions = englishNumber write + numString = numString + billions + ' billion' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end + +#### ------------> MILLIONS + write = left/1000000 # How many millions left to write out? + left = left - write*1000000 # Subtract off those millions. + +if write > 0 + # millions + millions = englishNumber write + numString = numString + millions + ' million' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end + +#### ------------> THOUSANDS + + write = left/1000 # How many thousands left to write out? + left = left - write*1000 # Subtract off those thousands. + +if write > 0 + # for thousands + hundreds = englishNumber write + numString = numString + hundreds + ' thousand' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end + +#### ------------> HUNDREDS + + write = left/100 # How many hundreds left to write out? + left = left - write*100 # Subtract off those hundreds. + + if write > 0 + # Now here's a really sly trick: + hundreds = englishNumber write + numString = numString + hundreds + ' hundred' + # That's called "recursion". So what did I just do? + # I told this method to call itself, but with "write" instead of + # "number". Remember that "write" is (at the moment) the number of + # hundreds we have to write out. After we add "hundreds" to + # "numString", we add the string ' hundred' after it. + # So, for example, if we originally called englishNumber with + # 1999 (so "number" = 1999), then at this point "write" would + # be 19, and "left" would be 99. The laziest thing to do at this + # point is to have englishNumber write out the 'nineteen' for us, + # then we write out ' hundred', and then the rest of + # englishNumber writes out 'ninety-nine'. + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end + +#### ------------> TENS + + write = left/10 # How many tens left to write out? + 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. + numString = numString + 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 + numString = numString + tensPlace[write-1] + # The "-1" is because tensPlace[3] is 'forty', not 'thirty'. + end + + if left > 0 + # So we don't write 'sixtyfour'... + numString = numString + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + numString = numString + onesPlace[write-1] + # The "-1" is because onesPlace[3] is 'four', not 'three'. + end + + # Now we just return "numString"... + numString end +=begin +puts englishNumber( 0) +puts englishNumber( 9) +puts englishNumber( 10) +puts englishNumber( 11) +puts englishNumber( 17) +puts englishNumber( 32) +puts englishNumber( 88) +puts englishNumber( 99) +puts englishNumber(100) +puts englishNumber(101) +puts englishNumber(234) +puts englishNumber(3211) +puts englishNumber(999999) +puts englishNumber(1000000000000) +=end \ No newline at end of file From 505e8c0ee49bc2e296fc91fae3b0b9529f9f1e5f Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:30:51 +0000 Subject: [PATCH 08/54] ch13 orange_tree --- ch13-creating-new-classes/orange_tree.rb | 48 +++++++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..d14174041 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,49 @@ class OrangeTree - # your code here -end + def initialize + @height = 0 + @age = 0 + @orangeCount = 0 + @live = true + puts "You orange tree is planted..." + end + + def age + puts "Your tree is #{@age} years old..." + end + + def height + puts "Your tree is #{@height}" + end + + def oneYearPasses + #method, which, when called, ages the tree one year. + if @age < 5 + @height += 0.4) + @orangeCount = 0 + @age = @age + 1 + end + puts "The orange tree is #{@age} years old..." + if @age > 5 + @orangeCount += (@height * 15 - 25) + elsif @age ==25 + @live = false + return "Your tree is dead..." + end + end + + def countTheOranges + puts "You have #{@orangeCount} oranges on the tree..." + end + + def pickAnOrange + if @orangeCount == 0 + return "There are no more oranges to pick this year!" + else + @orangeCount -= 1 + puts "You picked an orange. It is delicious!" + puts "There are now #{@orangeCount} oranges left on the tree." + end + end +end \ No newline at end of file From 0b47e77aacdd313f45540479ac434b4a637efd0f Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:32:42 +0000 Subject: [PATCH 09/54] updated roman numerals to return string --- ch09-writing-your-own-methods/old_school_roman_numerals.rb | 2 +- ch09-writing-your-own-methods/roman_numerals.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 bea2fe5ca..c88fdc541 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -12,5 +12,5 @@ def old_roman_numeral num num = num % k end end - puts roman + return roman end diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 81329002f..22b2a5e85 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -13,5 +13,5 @@ def roman_numeral num end end - puts roman + return roman end \ No newline at end of file From 2cefdb81391bcc55712c44d9404fb5cb5aa6c38e Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:40:41 +0000 Subject: [PATCH 10/54] updated roman numerals to return string --- ch09-writing-your-own-methods/old_school_roman_numerals.rb | 7 ++++--- ch09-writing-your-own-methods/roman_numerals.rb | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) 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 c88fdc541..8f4d0714a 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,16 +1,17 @@ def old_roman_numeral num + # your code here num = num.to_i (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) - numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"1" } + numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"I" } roman = "" numerals.keys.each do |k| n = num/k if n > 0 - roman += numerals[k]*n + roman += (numerals[k]*n) num = num % k end end return roman -end +end \ 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 22b2a5e85..31372e5a3 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,8 +1,9 @@ def roman_numeral num + # your code here num = num.to_i (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) - numerals = {1000=>"M", 500=>"D", 400=>"CD,", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 5=>"V", 4=>"IV", 1=>"1" } + numerals = {1000=>"M", 500=>"D", 400=>"CD,", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 5=>"V", 4=>"IV", 1=>"I" } roman = "" numerals.keys.each do |k| From ba1f0072b1af3e48da2a503bde1422f69c08a329 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 22:52:25 +0000 Subject: [PATCH 11/54] ch9 programs debugged --- ch09-writing-your-own-methods/ask.rb | 1 + ch09-writing-your-own-methods/roman_numerals.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 36e9d8188..9e9760e37 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,4 +1,5 @@ def ask question + # your code here while true puts question reply = gets.chomp.downcase diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 31372e5a3..95542d46a 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -3,7 +3,7 @@ def roman_numeral num num = num.to_i (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) - numerals = {1000=>"M", 500=>"D", 400=>"CD,", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 5=>"V", 4=>"IV", 1=>"I" } + numerals = {1000=>"M", 500=>"D", 400=>"CD", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 9=>"IX", 5=>"V", 4=>"IV", 1=>"I" } roman = "" numerals.keys.each do |k| From 094b66690268483e9b42baa34e6ec015ce752905 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 23:35:37 +0000 Subject: [PATCH 12/54] ch10 dictionary_sort --- ch10-nothing-new/dictionary_sort.rb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..ce91989ff 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,19 @@ def dictionary_sort arr # your code here + return recursive_sort(arr, []) +end + +def recursive_sort(unsorted_array, sorted_array) + return sorted_array if unsorted_array.empty? + index = 0 + (0..unsorted_array.length-1).each do |i| + if i == unsorted_array.length-1 + index = unsorted_array.length-1 + elsif unsorted_array[i+1].downcase > unsorted_array[i].downcase + index = i+1 + end + end + sorted_array << unsorted_array[index] + unsorted_array.delete_at(index) + recursive_sort(unsorted_array, sorted_array) end \ No newline at end of file From 8c14c848e3032d9cb8c8da3d7ce7e71499069c63 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 23:37:19 +0000 Subject: [PATCH 13/54] ch10 dictionary_sort --- ch10-nothing-new/dictionary_sort.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index ce91989ff..6d0ecde63 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -16,4 +16,4 @@ def recursive_sort(unsorted_array, sorted_array) sorted_array << unsorted_array[index] unsorted_array.delete_at(index) recursive_sort(unsorted_array, sorted_array) -end \ No newline at end of file +end From cb1215bfd72a9f28427786dd05e3bb639c0e7c03 Mon Sep 17 00:00:00 2001 From: daviryan Date: Fri, 9 Jun 2017 23:39:03 +0000 Subject: [PATCH 14/54] updated ch9 with lines at end of files --- ch09-writing-your-own-methods/ask.rb | 2 +- ch09-writing-your-own-methods/old_school_roman_numerals.rb | 2 +- ch09-writing-your-own-methods/roman_numerals.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 9e9760e37..c83613d4a 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -11,4 +11,4 @@ def ask question end end end -end \ No newline at end of file +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 8f4d0714a..5b69eb229 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -14,4 +14,4 @@ def old_roman_numeral num end end return roman -end \ No newline at end of file +end diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 95542d46a..3468ef2a2 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -15,4 +15,4 @@ def roman_numeral num end return roman -end \ No newline at end of file +end From f7ab62287d37cbeaa136f3c27d666276a607d238 Mon Sep 17 00:00:00 2001 From: daviryan Date: Sat, 10 Jun 2017 00:12:43 +0000 Subject: [PATCH 15/54] debugging dictionary, added shuffle --- ch09-writing-your-own-methods/ask.rb | 2 +- .../old_school_roman_numerals.rb | 2 +- ch09-writing-your-own-methods/roman_numerals.rb | 2 +- ch10-nothing-new/dictionary_sort.rb | 17 ++++++++++------- ch10-nothing-new/ninety_nine_bottles_of_beer.rb | 1 - ch10-nothing-new/shuffle.rb | 6 ++++++ 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index c83613d4a..9e9760e37 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -11,4 +11,4 @@ def ask question end end end -end +end \ No newline at end of file diff --git a/ch09-writing-your-own-methods/old_school_roman_numerals.rb b/ch09-writing-your-own-methods/old_school_roman_numerals.rb index 5b69eb229..8f4d0714a 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -14,4 +14,4 @@ def old_roman_numeral num end end return roman -end +end \ 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 3468ef2a2..95542d46a 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -15,4 +15,4 @@ def roman_numeral num end return roman -end +end \ No newline at end of file diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index 6d0ecde63..ac8081047 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -6,14 +6,17 @@ def dictionary_sort arr def recursive_sort(unsorted_array, sorted_array) return sorted_array if unsorted_array.empty? index = 0 - (0..unsorted_array.length-1).each do |i| - if i == unsorted_array.length-1 - index = unsorted_array.length-1 - elsif unsorted_array[i+1].downcase > unsorted_array[i].downcase + l = unsorted_array.length-1 + print unsorted_array + (0..l).each do |i| + break if unsorted_array[i+1].nil? + if unsorted_array[i+1].downcase > unsorted_array[i].downcase index = i+1 end end - sorted_array << unsorted_array[index] - unsorted_array.delete_at(index) + print index + print unsorted_array[index] + sorted_array.unshift( unsorted_array[index]) + unsorted_array.delete_at(index) # could swap values rather than delete? recursive_sort(unsorted_array, sorted_array) -end +end \ No newline at end of file diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..e69de29bb 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +0,0 @@ -# your code here \ No newline at end of file diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..05c8ecba1 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,9 @@ def shuffle arr # your code here + arr.size.downto(1) do |n| + index=rand(n) + # swap elements at index and the end + arr[index], arr[size-1] = arr[size-1],arr[index] + end + arr end \ No newline at end of file From 55c5394f0991f71756d7274e84349443db1fc7c9 Mon Sep 17 00:00:00 2001 From: daviryan Date: Sun, 11 Jun 2017 18:19:23 +0000 Subject: [PATCH 16/54] shuffle --- ch10-nothing-new/shuffle.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index 05c8ecba1..8268281ab 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,6 +1,7 @@ def shuffle arr # your code here - arr.size.downto(1) do |n| + size = arr.length + (size).downto(1) do |n| index=rand(n) # swap elements at index and the end arr[index], arr[size-1] = arr[size-1],arr[index] From b7d966e73263adcc24ea6c3bec324fd5a411c989 Mon Sep 17 00:00:00 2001 From: daviryan Date: Sun, 11 Jun 2017 19:40:59 +0000 Subject: [PATCH 17/54] english number bottles of beer --- ch10-nothing-new/english_number.rb | 4 +- .../ninety_nine_bottles_of_beer.rb | 191 ++++++++++++++++++ 2 files changed, 193 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index 256fb5278..eba17e76a 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,4 +1,4 @@ -def englishNumber number +def english_number number if number < 0 # No negative numbers. return 'Please enter a number that isn\'t negative.' end @@ -147,7 +147,7 @@ def englishNumber number end # Now we just return "numString"... - numString + return numString end =begin puts englishNumber( 0) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index e69de29bb..840182dcb 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -0,0 +1,191 @@ +=begin +https://pine.fm/LearnToProgram/chap_08.html + +Expand upon englishNumber. First, put in thousands. +So it should return 'one thousand' instead of 'ten hundred' +and 'ten thousand' instead of 'one hundred hundred'. +Expand upon englishNumber some more. +Now put in millions, so you get 'one million' instead of +'one thousand thousand'. Then try adding billions and trillions. +How high can you go? + +=end + + +def weddingNumber number + 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! + + numString = '' # This is the string we will return. + + onesPlace = ['one', 'two', 'three', 'four', 'five', + 'six', 'seven', 'eight', 'nine'] + tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty', + 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', + 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + + # "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 + +#### ------------> TRILLIONS + +write = left/1000000000000 # How many millions left to write out? + left = left - write*1000000000000 # Subtract off those millions. + +if write > 0 + # trillions + trillions = weddingNumber write + numString = numString + trillions + ' trillion' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' and ' + end + end + +#### ------------> BILLIONS + +write = left/1000000000 # How many millions left to write out? + left = left - write*1000000000 # Subtract off those millions. + +if write > 0 + # billions + billions = weddingNumber write + numString = numString + billions + ' billion' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' and ' + end + end + +#### ------------> MILLIONS + write = left/1000000 # How many millions left to write out? + left = left - write*1000000 # Subtract off those millions. + +if write > 0 + # millions + millions = weddingNumber write + numString = numString + millions + ' million' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' and ' + end + end + +#### ------------> THOUSANDS + + write = left/1000 # How many thousands left to write out? + left = left - write*1000 # Subtract off those thousands. + +if write > 0 + # for thousands + hundreds = weddingNumber write + numString = numString + hundreds + ' thousand' + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ', ' + end + end + +#### ------------> HUNDREDS + + write = left/100 # How many hundreds left to write out? + left = left - write*100 # Subtract off those hundreds. + + if write > 0 + # Now here's a really sly trick: + hundreds = weddingNumber write + numString = numString + hundreds + ' hundred' + # That's called "recursion". So what did I just do? + # I told this method to call itself, but with "write" instead of + # "number". Remember that "write" is (at the moment) the number of + # hundreds we have to write out. After we add "hundreds" to + # "numString", we add the string ' hundred' after it. + # So, for example, if we originally called englishNumber with + # 1999 (so "number" = 1999), then at this point "write" would + # be 19, and "left" would be 99. The laziest thing to do at this + # point is to have englishNumber write out the 'nineteen' for us, + # then we write out ' hundred', and then the rest of + # englishNumber writes out 'ninety-nine'. + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' and ' + end + end + +#### ------------> TENS + + write = left/10 # How many tens left to write out? + 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. + numString = numString + 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 + numString = numString + tensPlace[write-1] + # The "-1" is because tensPlace[3] is 'forty', not 'thirty'. + end + + if left > 0 + # So we don't write 'sixtyfour'... + numString = numString + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + numString = numString + onesPlace[write-1] + # The "-1" is because onesPlace[3] is 'four', not 'three'. + end + + # Now we just return "numString"... + numString +end + +#num = 99 +def ninety_nine_bottles_of_beer num +while (num > 0) + puts weddingNumber(num) + " bottles of beer on the wall, " + weddingNumber(num) + " bottles of beer." + puts "Take one down and pass it around, " + weddingNumber(num-1) + " bottles of beer on the wall." + num -= 1 +end +end + +=begin +puts weddingNumber( 0) +puts weddingNumber( 9) +puts weddingNumber( 10) +puts weddingNumber( 11) +puts weddingNumber( 17) +puts weddingNumber( 32) +puts weddingNumber( 88) +puts weddingNumber( 99) +puts weddingNumber(100) +puts weddingNumber(101) +puts weddingNumber(234) +puts weddingNumber(3211) +puts weddingNumber(999999) +puts weddingNumber(1000000000000) +=end \ No newline at end of file From bda4937f892d03641a60dad339eead9357182e29 Mon Sep 17 00:00:00 2001 From: daviryan Date: Sun, 11 Jun 2017 21:40:38 +0000 Subject: [PATCH 18/54] ch10 --- ch10-nothing-new/dictionary_sort.rb | 24 +++++++-------- ch10-nothing-new/english_number.rb | 47 ++++++++++++++--------------- ch10-nothing-new/sort.rb | 11 ++++++- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index ac8081047..2b4fb62ec 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -5,18 +5,18 @@ def dictionary_sort arr def recursive_sort(unsorted_array, sorted_array) return sorted_array if unsorted_array.empty? - index = 0 - l = unsorted_array.length-1 - print unsorted_array - (0..l).each do |i| - break if unsorted_array[i+1].nil? - if unsorted_array[i+1].downcase > unsorted_array[i].downcase - index = i+1 + #l = unsorted_array.length-1 + smallest = unsorted_array.pop + unsorted2 = [] + unsorted_array.each do |check| + if check.downcase < smallest.downcase + unsorted2 << smallest + smallest = check + else + unsorted2 << check end end - print index - print unsorted_array[index] - sorted_array.unshift( unsorted_array[index]) - unsorted_array.delete_at(index) # could swap values rather than delete? - recursive_sort(unsorted_array, sorted_array) + sorted_array << smallest + + recursive_sort(unsorted2, sorted_array) end \ No newline at end of file diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index eba17e76a..a7055a0d1 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -29,7 +29,7 @@ def english_number number if write > 0 # trillions - trillions = englishNumber write + trillions = english_number write numString = numString + trillions + ' trillion' if left > 0 @@ -45,7 +45,7 @@ def english_number number if write > 0 # billions - billions = englishNumber write + billions = english_number write numString = numString + billions + ' billion' if left > 0 @@ -60,7 +60,7 @@ def english_number number if write > 0 # millions - millions = englishNumber write + millions = english_number write numString = numString + millions + ' million' if left > 0 @@ -76,7 +76,7 @@ def english_number number if write > 0 # for thousands - hundreds = englishNumber write + hundreds = english_number write numString = numString + hundreds + ' thousand' if left > 0 @@ -92,19 +92,19 @@ def english_number number if write > 0 # Now here's a really sly trick: - hundreds = englishNumber write + hundreds = english_number write numString = numString + hundreds + ' hundred' # That's called "recursion". So what did I just do? # I told this method to call itself, but with "write" instead of # "number". Remember that "write" is (at the moment) the number of # hundreds we have to write out. After we add "hundreds" to # "numString", we add the string ' hundred' after it. - # So, for example, if we originally called englishNumber with + # So, for example, if we originally called english_number with # 1999 (so "number" = 1999), then at this point "write" would # be 19, and "left" would be 99. The laziest thing to do at this - # point is to have englishNumber write out the 'nineteen' for us, + # point is to have english_number write out the 'nineteen' for us, # then we write out ' hundred', and then the rest of - # englishNumber writes out 'ninety-nine'. + # english_number writes out 'ninety-nine'. if left > 0 # So we don't write 'two hundredfifty-one'... @@ -149,19 +149,18 @@ def english_number number # Now we just return "numString"... return numString end -=begin -puts englishNumber( 0) -puts englishNumber( 9) -puts englishNumber( 10) -puts englishNumber( 11) -puts englishNumber( 17) -puts englishNumber( 32) -puts englishNumber( 88) -puts englishNumber( 99) -puts englishNumber(100) -puts englishNumber(101) -puts englishNumber(234) -puts englishNumber(3211) -puts englishNumber(999999) -puts englishNumber(1000000000000) -=end \ No newline at end of file + +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) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..0b8fc4be5 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,12 @@ -def sort arr +def sort (arr) # your code here + print arr + puts "" + return arr if arr.length <= 1 + m = arr.pop + + left = arr.select {|x| x <= m} + right = arr.select{|x| x > m} + + sort(left) + [m] + sort(right) end \ No newline at end of file From 0aa6998969f0a876b8326ad42da774f8f4974795 Mon Sep 17 00:00:00 2001 From: daviryan Date: Sun, 11 Jun 2017 22:20:21 +0000 Subject: [PATCH 19/54] english number --- ch10-nothing-new/english_number.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index a7055a0d1..3b2a35ce4 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -16,14 +16,35 @@ def english_number number 'sixty', 'seventy', 'eighty', 'ninety'] teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + nums = {'quindecillion'=>10**48, 'quattuordecillion'=>10**45, 'tredecillion'=>10**42, 'duodecillion'=>10**39, 'undecillion'=>10**36, + 'decillion'=>10**33, 'nonillion'=>10**30, 'octillion'=>10**27, 'septillion'=>10**24, 'sextillion'=>10**21, + 'quintillion'=> 10**18, 'quadrillion'=> 10**15, + 'trillion'=> 10**12, 'billion'=> 10**9, 'million'=> 10**6, 'thousand'=> 10**3, 'hundred'=> 10**2 } # "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 -#### ------------> TRILLIONS +nums.each do |name,value| + #print name + write = left/value # How many millions left to write out? + left = left - write*value # Subtract off those millions. + +if write > 0 + # trillions + trillions = english_number write + numString = numString + (english_number write) + " " + name.to_s + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end +end +#### ------------> TRILLIONS +=begin write = left/1000000000000 # How many millions left to write out? left = left - write*1000000000000 # Subtract off those millions. @@ -111,7 +132,7 @@ def english_number number numString = numString + ' ' end end - +=end #### ------------> TENS write = left/10 # How many tens left to write out? From 6376141ee1fbbff2d9f199bf25646e9fbf1331d6 Mon Sep 17 00:00:00 2001 From: daviryan Date: Sun, 11 Jun 2017 22:31:28 +0000 Subject: [PATCH 20/54] english number --- ch10-nothing-new/english_number.rb | 79 ------------------------------ 1 file changed, 79 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index 3b2a35ce4..21d69671a 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -43,74 +43,10 @@ def english_number number end end -#### ------------> TRILLIONS =begin write = left/1000000000000 # How many millions left to write out? left = left - write*1000000000000 # Subtract off those millions. -if write > 0 - # trillions - trillions = english_number write - numString = numString + trillions + ' trillion' - - if left > 0 - # So we don't write 'two hundredfifty-one'... - numString = numString + ' ' - end - end - -#### ------------> BILLIONS - -write = left/1000000000 # How many millions left to write out? - left = left - write*1000000000 # Subtract off those millions. - -if write > 0 - # billions - billions = english_number write - numString = numString + billions + ' billion' - - if left > 0 - # So we don't write 'two hundredfifty-one'... - numString = numString + ' ' - end - end - -#### ------------> MILLIONS - write = left/1000000 # How many millions left to write out? - left = left - write*1000000 # Subtract off those millions. - -if write > 0 - # millions - millions = english_number write - numString = numString + millions + ' million' - - if left > 0 - # So we don't write 'two hundredfifty-one'... - numString = numString + ' ' - end - end - -#### ------------> THOUSANDS - - write = left/1000 # How many thousands left to write out? - left = left - write*1000 # Subtract off those thousands. - -if write > 0 - # for thousands - hundreds = english_number write - numString = numString + hundreds + ' thousand' - - if left > 0 - # So we don't write 'two hundredfifty-one'... - numString = numString + ' ' - end - end - -#### ------------> HUNDREDS - - write = left/100 # How many hundreds left to write out? - left = left - write*100 # Subtract off those hundreds. - if write > 0 # Now here's a really sly trick: hundreds = english_number write @@ -170,18 +106,3 @@ def english_number number # Now we just return "numString"... return numString 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) From 70746f4a9d0be0afc4090619118815727c061dbf Mon Sep 17 00:00:00 2001 From: daviryan Date: Sun, 11 Jun 2017 22:41:03 +0000 Subject: [PATCH 21/54] shuffle and english number --- ch10-nothing-new/english_number.rb | 15 +++++++++++++++ ch10-nothing-new/shuffle.rb | 22 ++++++++++++++++------ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index 21d69671a..aa007ad21 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -106,3 +106,18 @@ def english_number number # Now we just return "numString"... return numString 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) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index 8268281ab..86a73d6a1 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,10 +1,20 @@ def shuffle arr # your code here - size = arr.length - (size).downto(1) do |n| - index=rand(n) - # swap elements at index and the end - arr[index], arr[size-1] = arr[size-1],arr[index] + shuff = [] + + while !arr.empty? + rand_index = rand(arr.length) + #curr_index = 0 + new_arr = [] + arr.length.times do |i| + if i == rand_index + shuff << arr[rand_index] + else + new_arr << arr[i] + end end - arr + arr = new_arr + end + + shuff end \ No newline at end of file From 986fd1902284a0ee9833a7f2b6a944551dad2f89 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 16:40:08 +0000 Subject: [PATCH 22/54] playlist --- .../build_your_own_playlist.rb | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index 801de24bd..41b9a1746 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,28 @@ -# your code here \ No newline at end of file +def shuffle arr + # your code here + shuff = [] + + while !arr.empty? + rand_index = rand(arr.length) + #curr_index = 0 + new_arr = [] + arr.length.times do |i| + if i == rand_index + shuff << arr[rand_index] + else + new_arr << arr[i] + end + end + arr = new_arr + end + shuff +end + +def build_your_own_playlist +tracks = shuffle(Dir['**/*.{ogg,mp3,m4a,wma}']) +File.open 'playlist.m3u', 'w' do |f| + tracks.each do |track| + f.write track+"\n" + end +end +end \ No newline at end of file From cd1da98574ec3745fec3a9bf3049591e78cc575e Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 17:30:50 +0000 Subject: [PATCH 23/54] roman to int --- ...party_like_its_roman_to_integer_mcmxcix.rb | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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..ee3f538e2 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,23 @@ def roman_to_integer roman # your code here + numerals = {"M"=>1000, "D"=>500, "CD"=>400, "C"=>100, "XC"=>90, "L"=>50, "XL"=>40, "X"=>10, "IX"=>9, "V"=>5, "IV"=>4, "I"=>1 } + integer = 0 + i=0 + l = roman.length + until i == (l) do + if i < l-1 + r = roman[i]+roman[i+1] + if numerals.keys.include?(r) + integer += numerals[r] + i+=2 + else + integer += numerals[roman[i]] + i+=1 + end + else + integer += numerals[roman[i]] + i+=1 + end + end + return integer end \ No newline at end of file From 18d864efeab47c2c51c72b374195fb913d96efa9 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 17:34:00 +0000 Subject: [PATCH 24/54] roman to int --- .../party_like_its_roman_to_integer_mcmxcix.rb | 1 + 1 file changed, 1 insertion(+) 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 ee3f538e2..397afe5a2 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,5 +1,6 @@ def roman_to_integer roman # your code here + roman.upcase! numerals = {"M"=>1000, "D"=>500, "CD"=>400, "C"=>100, "XC"=>90, "L"=>50, "XL"=>40, "X"=>10, "IX"=>9, "V"=>5, "IV"=>4, "I"=>1 } integer = 0 i=0 From 162226b9bb3fb7cb2da380b690231b342ac13fa3 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 17:37:45 +0000 Subject: [PATCH 25/54] roman to int --- .../party_like_its_roman_to_integer_mcmxcix.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 397afe5a2..9e6118f2a 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,7 +1,7 @@ def roman_to_integer roman # your code here roman.upcase! - numerals = {"M"=>1000, "D"=>500, "CD"=>400, "C"=>100, "XC"=>90, "L"=>50, "XL"=>40, "X"=>10, "IX"=>9, "V"=>5, "IV"=>4, "I"=>1 } + numerals = {"M"=>1000, "CM"=> 900, "D"=>500, "CD"=>400, "C"=>100, "XC"=>90, "L"=>50, "XL"=>40, "X"=>10, "IX"=>9, "V"=>5, "IV"=>4, "I"=>1 } integer = 0 i=0 l = roman.length From 802de11927ca5c5bcb2100ea262fa75fcb7f9939 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 17:43:05 +0000 Subject: [PATCH 26/54] extend integer --- .../extend_built_in_classes.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index c3e793933..036bfc27e 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 Integer # your code here + def factorial + return "Number needs to be more than 0" if self <= 0 + self * (self-1).factorial + end + + def to_roman + num = self + (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) + + numerals = {1000=>"M", 500=>"D", 400=>"CD", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 9=>"IX", 5=>"V", 4=>"IV", 1=>"I" } + roman = "" + + numerals.keys.each do |k| + n = num/k + if n > 0 + roman += numerals[k]*n + num = num % k + end + end + return roman + end end \ No newline at end of file From 2c058421411983c656f26b25f26ccb16e0bcb7d2 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 17:53:08 +0000 Subject: [PATCH 27/54] extend integer --- ch13-creating-new-classes/extend_built_in_classes.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 036bfc27e..812235c32 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,12 +1,13 @@ class Integer # your code here def factorial - return "Number needs to be more than 0" if self <= 0 + return "Number needs to be more than 0" if self < 0 + return 1 if self <= 1 self * (self-1).factorial end def to_roman - num = self + num = self (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) numerals = {1000=>"M", 500=>"D", 400=>"CD", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 9=>"IX", 5=>"V", 4=>"IV", 1=>"I" } From 1d93ae5701a7ca94af4bbb7ac8de0b53690ad8fc Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 18:01:39 +0000 Subject: [PATCH 28/54] extend integer --- ch13-creating-new-classes/extend_built_in_classes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 812235c32..a3b051534 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -10,7 +10,7 @@ def to_roman num = self (puts "Invalid input. Choose a number between 1 and 3000"; return) if ((num.to_i < 1) || (num.to_i > 3000)) - numerals = {1000=>"M", 500=>"D", 400=>"CD", 100=>"C", 90=>"XC", 50=>"L", 40=>"XL", 10=>"X", 9=>"IX", 5=>"V", 4=>"IV", 1=>"I" } + numerals = {1000=>"M", 500=>"D", 100=>"C", 50=>"L", 10=>"X", 5=>"V", 1=>"I" } roman = "" numerals.keys.each do |k| From 0565b12b583386c67283596d801f51ab6557b643 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 18:02:36 +0000 Subject: [PATCH 29/54] orange tree --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index d14174041..0210a5b81 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -26,7 +26,7 @@ def height def oneYearPasses #method, which, when called, ages the tree one year. if @age < 5 - @height += 0.4) + @height += 0.4 @orangeCount = 0 @age = @age + 1 end From a317b4626b2c0df7095501b3df06c40cff182849 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 18:28:25 +0000 Subject: [PATCH 30/54] return organge tree info --- ch13-creating-new-classes/orange_tree.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 0210a5b81..c8f27ed91 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -20,7 +20,7 @@ def age end def height - puts "Your tree is #{@height}" + return "Your tree is #{@height}" end def oneYearPasses @@ -40,7 +40,7 @@ def oneYearPasses end def countTheOranges - puts "You have #{@orangeCount} oranges on the tree..." + return "You have #{@orangeCount} oranges on the tree..." end def pickAnOrange From bb6ccb16757d40bdf7f2c7004d2830eea10061e8 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 18:34:13 +0000 Subject: [PATCH 31/54] return organge tree info --- ch13-creating-new-classes/orange_tree.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index c8f27ed91..b10cadffa 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -23,7 +23,7 @@ def height return "Your tree is #{@height}" end - def oneYearPasses + def one_year_passes #method, which, when called, ages the tree one year. if @age < 5 @height += 0.4 @@ -39,11 +39,11 @@ def oneYearPasses end end - def countTheOranges + def count_the_oranges return "You have #{@orangeCount} oranges on the tree..." end - def pickAnOrange + def pick_an_orange if @orangeCount == 0 return "There are no more oranges to pick this year!" else From fee3e4e3355769295e1d9beae01a2d5c48789c26 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 22:25:26 +0000 Subject: [PATCH 32/54] organge tree --- ch13-creating-new-classes/orange_tree.rb | 58 +++++++++++++++--------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index b10cadffa..51a728534 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -10,46 +10,62 @@ class OrangeTree def initialize @height = 0 @age = 0 - @orangeCount = 0 + @orange_ount = 0 @live = true - puts "You orange tree is planted..." end def age - puts "Your tree is #{@age} years old..." + "Your tree is #{@age} years old..." end def height - return "Your tree is #{@height}" + if @alive + @height.round(1) + else + "Your tree is dead!" + end end def one_year_passes #method, which, when called, ages the tree one year. - if @age < 5 - @height += 0.4 + if @ailve + @height += 0.4 + @age += 1 + @orange_count=0 + if @age <= 5 @orangeCount = 0 - @age = @age + 1 - end - puts "The orange tree is #{@age} years old..." - if @age > 5 - @orangeCount += (@height * 15 - 25) - elsif @age ==25 - @live = false - return "Your tree is dead..." + "This year your tree grew to #{@height.round(1)}m tall," + + " but is still too young to bear fruit." + elsif @age > 5 && @age <=25 + @orangeCount = (@height * 15 - 25) + "This year your tree grew to #{@height}m tall, and produced #{@orange_count} oranges." + else + @live = false + 'Oh, no! The tree is too old, and has died. :(' + end + else + 'A year later, the tree is still dead. :(' end end def count_the_oranges - return "You have #{@orangeCount} oranges on the tree..." + if @alive + @orange_count + else + "A dead tree has no oranges" + end end def pick_an_orange - if @orangeCount == 0 - return "There are no more oranges to pick this year!" + if @alive + if @orangeCount == 0 + 'You search every branch, but find no oranges.' + else + @orangeCount -= 1 + 'You pick a juicy, delicious orange!' + end else - @orangeCount -= 1 - puts "You picked an orange. It is delicious!" - puts "There are now #{@orangeCount} oranges left on the tree." - end + 'A dead tree has nothing to pick. :(' + end end end \ No newline at end of file From 2f66737b07cdeda93d37e49bcf3f181e36e3c736 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 22:29:14 +0000 Subject: [PATCH 33/54] organge tree --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 51a728534..654ce42bd 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -11,7 +11,7 @@ def initialize @height = 0 @age = 0 @orange_ount = 0 - @live = true + @alive = true end def age From 52ce5faf393414f3e70e1a8bef67a6b5e2959c79 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 22:42:11 +0000 Subject: [PATCH 34/54] orange --- ch13-creating-new-classes/orange_tree.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 654ce42bd..4741039e6 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -28,7 +28,7 @@ def height def one_year_passes #method, which, when called, ages the tree one year. - if @ailve + if @alive @height += 0.4 @age += 1 @orange_count=0 @@ -68,4 +68,7 @@ def pick_an_orange 'A dead tree has nothing to pick. :(' end end -end \ No newline at end of file +end + +o = OrangeTree.new +puts o.one_year_passes \ No newline at end of file From a5cd5a99879a9c56a5ff732cfff6ed866a212e9e Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 23:44:51 +0000 Subject: [PATCH 35/54] orange --- ch13-creating-new-classes/orange_tree.rb | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 4741039e6..26bfba03a 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -14,15 +14,11 @@ def initialize @alive = true end - def age - "Your tree is #{@age} years old..." - end - def height if @alive @height.round(1) else - "Your tree is dead!" + 'A dead tree is not very tall. :(' end end @@ -33,14 +29,13 @@ def one_year_passes @age += 1 @orange_count=0 if @age <= 5 - @orangeCount = 0 "This year your tree grew to #{@height.round(1)}m tall," + " but is still too young to bear fruit." - elsif @age > 5 && @age <=25 - @orangeCount = (@height * 15 - 25) - "This year your tree grew to #{@height}m tall, and produced #{@orange_count} oranges." + elsif @age > 5 && @age <= 25 + @orange_count = (@height * 15 - 25).to_i + "This year your tree grew to #{@height.round(1)}m tall, and produced #{@orange_count} oranges." else - @live = false + @alive = false 'Oh, no! The tree is too old, and has died. :(' end else @@ -70,5 +65,7 @@ def pick_an_orange end end -o = OrangeTree.new -puts o.one_year_passes \ No newline at end of file + ot = OrangeTree.new + 23.times do + ot.one_year_passes + end \ No newline at end of file From bd5329f34c5fa4553f05ad1001ca3346fbe7969d Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 23:55:05 +0000 Subject: [PATCH 36/54] orange --- ch13-creating-new-classes/orange_tree.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 26bfba03a..655240b8a 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -63,9 +63,4 @@ def pick_an_orange 'A dead tree has nothing to pick. :(' end end -end - - ot = OrangeTree.new - 23.times do - ot.one_year_passes - end \ No newline at end of file +end \ No newline at end of file From da32618350325fadc06ff2a654286930e6bb8860 Mon Sep 17 00:00:00 2001 From: daviryan Date: Mon, 12 Jun 2017 23:56:34 +0000 Subject: [PATCH 37/54] orange --- ch13-creating-new-classes/orange_tree.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 655240b8a..3e27c295c 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -53,10 +53,10 @@ def count_the_oranges def pick_an_orange if @alive - if @orangeCount == 0 + if @orange_count == 0 'You search every branch, but find no oranges.' else - @orangeCount -= 1 + @orange_count -= 1 'You pick a juicy, delicious orange!' end else From 6648ad32c47097492ab1de0a97cb45331af9bf12 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 14:32:30 +0000 Subject: [PATCH 38/54] orange --- ch13-creating-new-classes/orange_tree.rb | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 3e27c295c..9932c4756 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -63,4 +63,22 @@ def pick_an_orange 'A dead tree has nothing to pick. :(' end end -end \ No newline at end of file +end + +=begin + 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) +-end \ No newline at end of file From dfe2fc71f2334574d89f11cb826af419c4674548 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 14:39:07 +0000 Subject: [PATCH 39/54] orange --- ch13-creating-new-classes/orange_tree.rb | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 9932c4756..3e27c295c 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -63,22 +63,4 @@ def pick_an_orange 'A dead tree has nothing to pick. :(' end end -end - -=begin - 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) --end \ No newline at end of file +end \ No newline at end of file From 64dbd804f6ee59e1320328d7c16eb2b17184c807 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 14:45:24 +0000 Subject: [PATCH 40/54] orange --- ch13-creating-new-classes/orange_tree.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 3e27c295c..4fe969ed6 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -47,7 +47,7 @@ def count_the_oranges if @alive @orange_count else - "A dead tree has no oranges" + "A dead tree has no oranges. :(" end end From 184f533adf90a3d8ca60c2534bd095dd4041f5a9 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 15:25:40 +0000 Subject: [PATCH 41/54] clock --- ch14-blocks-and-procs/grandfather_clock.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..6173e2fc1 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,6 @@ def grandfather_clock &block # your code here + Time.now.strftime("%I").to_i.times do + block.call + end end \ No newline at end of file From bb70b6dfba33877f4eb6865f63c220814c545e4f Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 15:40:50 +0000 Subject: [PATCH 42/54] prog log --- ch14-blocks-and-procs/program_logger.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..3b44c174b 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,6 @@ def log desc, &block # your code here + puts "Beginning \"" + desc + "\"..." + + puts "...\"" + desc + "\" finished, returning: " + block.call.to_s end \ No newline at end of file From 36f68dbd8407b4d925e261b08ab0862b94e7d2b2 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 15:47:22 +0000 Subject: [PATCH 43/54] better logger --- ch14-blocks-and-procs/better_program_logger.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..35f45a63c 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,9 @@ +$depth = 0 def log desc, &block # your code here + puts ("\t"*$depth) + "Beginning \"" + desc + "\"..." + $depth +=1 + result = block.call + $depth -=1 + puts ("\t"*$depth) + "...\"" + desc + "\" finished, returning: " + result.to_s end \ No newline at end of file From 98b0075fa8a0ea3bae2e296d4de7f82a1a77cb27 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 16:03:53 +0000 Subject: [PATCH 44/54] ch14 --- ch14-blocks-and-procs/better_program_logger.rb | 8 ++++---- ch14-blocks-and-procs/even_better_profiling.rb | 9 +++++++++ ch14-blocks-and-procs/program_logger.rb | 8 ++++---- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 35f45a63c..1a205b762 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,9 +1,9 @@ $depth = 0 -def log desc, &block +def better_log desc, &block # your code here - puts ("\t"*$depth) + "Beginning \"" + desc + "\"..." + puts "Beginning \"#{desc}\"..." $depth +=1 result = block.call - $depth -=1 - puts ("\t"*$depth) + "...\"" + desc + "\" finished, returning: " + result.to_s + $depth -=1 + puts "...\"#{desc}\" finished, returning: #{ result.to_s}" 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..ece92afb4 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,12 @@ +$profiling=true def profile block_description, &block # your code here + if $profiling + start_time = Time.new + block.call + duration = Time.new - start_time + puts "#{block_description}: #{duration} seconds" + else + block.call + end 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 3b44c174b..de83c4699 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,6 +1,6 @@ -def log desc, &block +def program_log desc, &block # your code here - puts "Beginning \"" + desc + "\"..." - - puts "...\"" + desc + "\" finished, returning: " + block.call.to_s + puts "Beginning \"#{desc}\"..." + result = block.call + puts "...\"#{desc}\" finished, returning: #{ result.to_s}" end \ No newline at end of file From fae49534b4f9d98c70059df895f22e54f2984db7 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 16:16:27 +0000 Subject: [PATCH 45/54] ch14 --- ch14-blocks-and-procs/better_program_logger.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 1a205b762..ac1c4eec7 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,9 +1,9 @@ $depth = 0 def better_log desc, &block # your code here - puts "Beginning \"#{desc}\"..." + puts "#{"\t"*$depth}Beginning \"#{desc}\"..." $depth +=1 result = block.call $depth -=1 - puts "...\"#{desc}\" finished, returning: #{ result.to_s}" + puts "#{"\t"*$depth}...\"#{desc}\" finished, returning: #{ result.to_s}" end \ No newline at end of file From 0638ccdace41a2d6876f6b100400a71365dfcde8 Mon Sep 17 00:00:00 2001 From: daviryan Date: Tue, 13 Jun 2017 16:28:46 +0000 Subject: [PATCH 46/54] ch14 --- ch14-blocks-and-procs/better_program_logger.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index ac1c4eec7..9fd3a78e1 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,9 +1,9 @@ $depth = 0 def better_log desc, &block # your code here - puts "#{"\t"*$depth}Beginning \"#{desc}\"..." + puts "#{" "*$depth}Beginning \"#{desc}\"..." $depth +=1 result = block.call $depth -=1 - puts "#{"\t"*$depth}...\"#{desc}\" finished, returning: #{ result.to_s}" + puts "#{" "*$depth}...\"#{desc}\" finished, returning: #{ result.to_s}" end \ No newline at end of file From 3501077a41180ca68e6e225dd6d7c6fa1112a25b Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 10:04:32 +0000 Subject: [PATCH 47/54] seconds --- .../one_billion_seconds.rb | 126 +++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 801de24bd..092f471ca 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,125 @@ -# your code here \ No newline at end of file +# your code here +def english_number number + 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! + + numString = '' # This is the string we will return. + + onesPlace = ['one', 'two', 'three', 'four', 'five', + 'six', 'seven', 'eight', 'nine'] + tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty', + 'sixty', 'seventy', 'eighty', 'ninety'] + teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', + 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + nums = {'quindecillion'=>10**48, 'quattuordecillion'=>10**45, 'tredecillion'=>10**42, 'duodecillion'=>10**39, 'undecillion'=>10**36, + 'decillion'=>10**33, 'nonillion'=>10**30, 'octillion'=>10**27, 'septillion'=>10**24, 'sextillion'=>10**21, + 'quintillion'=> 10**18, 'quadrillion'=> 10**15, + 'trillion'=> 10**12, 'billion'=> 10**9, 'million'=> 10**6, 'thousand'=> 10**3, 'hundred'=> 10**2 } + + # "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 + +nums.each do |name,value| + #print name + write = left/value # How many millions left to write out? + left = left - write*value # Subtract off those millions. + +if write > 0 + # trillions + trillions = english_number write + numString = numString + (english_number write) + " " + name.to_s + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end +end + +=begin +write = left/1000000000000 # How many millions left to write out? + left = left - write*1000000000000 # Subtract off those millions. + + if write > 0 + # Now here's a really sly trick: + hundreds = english_number write + numString = numString + hundreds + ' hundred' + # That's called "recursion". So what did I just do? + # I told this method to call itself, but with "write" instead of + # "number". Remember that "write" is (at the moment) the number of + # hundreds we have to write out. After we add "hundreds" to + # "numString", we add the string ' hundred' after it. + # So, for example, if we originally called english_number with + # 1999 (so "number" = 1999), then at this point "write" would + # be 19, and "left" would be 99. The laziest thing to do at this + # point is to have english_number write out the 'nineteen' for us, + # then we write out ' hundred', and then the rest of + # english_number writes out 'ninety-nine'. + + if left > 0 + # So we don't write 'two hundredfifty-one'... + numString = numString + ' ' + end + end +=end +#### ------------> TENS + + write = left/10 # How many tens left to write out? + 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. + numString = numString + 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 + numString = numString + tensPlace[write-1] + # The "-1" is because tensPlace[3] is 'forty', not 'thirty'. + end + + if left > 0 + # So we don't write 'sixtyfour'... + numString = numString + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + numString = numString + onesPlace[write-1] + # The "-1" is because onesPlace[3] is 'four', not 'three'. + end + + # Now we just return "numString"... + return numString +end +=begin +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) +=end \ No newline at end of file From 69344aa52d2c3e69926820077140cd69be9d6de7 Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 10:15:52 +0000 Subject: [PATCH 48/54] seconds --- .../one_billion_seconds.rb | 128 +----------------- 1 file changed, 3 insertions(+), 125 deletions(-) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 092f471ca..b22fbb472 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1,125 +1,3 @@ -# your code here -def english_number number - 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! - - numString = '' # This is the string we will return. - - onesPlace = ['one', 'two', 'three', 'four', 'five', - 'six', 'seven', 'eight', 'nine'] - tensPlace = ['ten', 'twenty', 'thirty', 'forty', 'fifty', - 'sixty', 'seventy', 'eighty', 'ninety'] - teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', - 'sixteen', 'seventeen', 'eighteen', 'nineteen'] - nums = {'quindecillion'=>10**48, 'quattuordecillion'=>10**45, 'tredecillion'=>10**42, 'duodecillion'=>10**39, 'undecillion'=>10**36, - 'decillion'=>10**33, 'nonillion'=>10**30, 'octillion'=>10**27, 'septillion'=>10**24, 'sextillion'=>10**21, - 'quintillion'=> 10**18, 'quadrillion'=> 10**15, - 'trillion'=> 10**12, 'billion'=> 10**9, 'million'=> 10**6, 'thousand'=> 10**3, 'hundred'=> 10**2 } - - # "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 - -nums.each do |name,value| - #print name - write = left/value # How many millions left to write out? - left = left - write*value # Subtract off those millions. - -if write > 0 - # trillions - trillions = english_number write - numString = numString + (english_number write) + " " + name.to_s - - if left > 0 - # So we don't write 'two hundredfifty-one'... - numString = numString + ' ' - end - end -end - -=begin -write = left/1000000000000 # How many millions left to write out? - left = left - write*1000000000000 # Subtract off those millions. - - if write > 0 - # Now here's a really sly trick: - hundreds = english_number write - numString = numString + hundreds + ' hundred' - # That's called "recursion". So what did I just do? - # I told this method to call itself, but with "write" instead of - # "number". Remember that "write" is (at the moment) the number of - # hundreds we have to write out. After we add "hundreds" to - # "numString", we add the string ' hundred' after it. - # So, for example, if we originally called english_number with - # 1999 (so "number" = 1999), then at this point "write" would - # be 19, and "left" would be 99. The laziest thing to do at this - # point is to have english_number write out the 'nineteen' for us, - # then we write out ' hundred', and then the rest of - # english_number writes out 'ninety-nine'. - - if left > 0 - # So we don't write 'two hundredfifty-one'... - numString = numString + ' ' - end - end -=end -#### ------------> TENS - - write = left/10 # How many tens left to write out? - 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. - numString = numString + 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 - numString = numString + tensPlace[write-1] - # The "-1" is because tensPlace[3] is 'forty', not 'thirty'. - end - - if left > 0 - # So we don't write 'sixtyfour'... - numString = numString + '-' - end - end - - write = left # How many ones left to write out? - left = 0 # Subtract off those ones. - - if write > 0 - numString = numString + onesPlace[write-1] - # The "-1" is because onesPlace[3] is 'four', not 'three'. - end - - # Now we just return "numString"... - return numString -end -=begin -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) -=end \ No newline at end of file +birth_time = Time.mktime(1983, 9, 11, 7, 40, 54) +billion_seconds_old = birth_time + 1000000000 +puts billion_seconds_old \ No newline at end of file From 26f3471e37ec86e9cc3209154fc71f2aff4b1c49 Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 11:15:40 +0000 Subject: [PATCH 49/54] ch12 --- .../birthday_helper.rb | 44 ++++++++++++++++++- ch12-new-classes-of-objects/birthdays.csv | 8 ++++ ch12-new-classes-of-objects/happy_birthday.rb | 25 ++++++++++- 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 ch12-new-classes-of-objects/birthdays.csv diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..b7988515f 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,43 @@ -# your code here \ No newline at end of file +# your code here +filename = 'birthdays.csv' +birth_dates = { } +months ={'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12} + +(puts "Could not load birthdays from #{filename}"; exit) if !File.exists?(filename) +puts "Loading birthdays from #{filename}..." + +File.open filename do |f| +f.each_line do |entry| +name = entry.slice(0, entry.index(",")) +date = entry.slice(entry.index(",")+2, entry.length) +birth_dates[name] = date +end +end + +puts 'Enter a name and their next birthday will be returned' +name = gets.chomp +(puts "Name not found in file, exiting..."; exit) if !birth_dates.keys.include?(name) + +date = birth_dates[name] +date = date.split(" ") +date[1] = date[1].chomp(",") +year = date[2] +day = date[1] + +month_string = date[0] +month = months[month_string] + +today = Time.new +curr_year = today.strftime("%Y") +birthdate = Time.mktime(year,month,day) +birthday = Time.mktime(curr_year.to_i,month,day) + +age = ((birthday - birthdate)/(60*60*24*365)).to_i + +if birthday > today +puts name + '\'s next birthday will be ' + day.to_s + " " + month_string.to_s + ", #{curr_year}" +puts 'and he/she will be ' + age.to_s + ' years old' +else +puts name + '\'s next birthday will be ' + day.to_s + " " + month_string.to_s + ", #{curr_year.to_i + 1}" +puts 'and he/she will be ' + age.to_s + ' years old' +end \ No newline at end of file diff --git a/ch12-new-classes-of-objects/birthdays.csv b/ch12-new-classes-of-objects/birthdays.csv new file mode 100644 index 000000000..e5115be6a --- /dev/null +++ b/ch12-new-classes-of-objects/birthdays.csv @@ -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..3da0e7dca 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,24 @@ -# your code here \ No newline at end of file +# your code here +=begin +Happy birthday! 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 in?" +year = gets.chomp +puts "What month were you born in? (please enter 1-12 for month)" +month = gets.chomp +puts "What day of the month were you born?" +day = gets.chomp + +now = Time.new +birthday = Time.mktime(year.to_i, month.to_i, day.to_i) + +age = (now - birthday)/(60*60*24*365) + +puts "You are #{age.to_i} years old!" + +age.to_i.times do +puts 'spank' +end \ No newline at end of file From 21408829fb681f97e32cb3b8bc9a88cd17439dde Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 11:24:10 +0000 Subject: [PATCH 50/54] ch13 --- .../interactive_baby_dragon.rb | 166 +++++++++++++++++- 1 file changed, 165 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index 801de24bd..c56dde19e 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,165 @@ -# your code here \ No newline at end of file +# your code here +=begin +Write a program so that you can interact with your baby dragon. +You should be able to enter commands like feed and walk, +and have those methods be called on your dragon. Of course, +since what you are inputting are just strings, you will have +to have some sort of method dispatch, where your program checks +which string was entered, and then calls the appropriate method. +=end + +puts "Program starts" + +class Dragon + + def initialize name + @name = name + @asleep = false + @stuffInBelly = 10 # He's full. + @stuffInIntestine = 0 # He doesn't need to go. + @input = "" + + puts @name + ' is born.' + end + + def interact + puts "How do you want to interact with the baby dragon?" + puts "You can choose \"feed\", \"walk\", \"put to bed\", \"toss\" and \"rock\"." + @input = gets.chomp + + while @input != "end" + puts "How do you want to interact with the baby dragon?" + puts "You can choose \"feed\", \"walk\", \"put to bed\", \"toss\", \"rock\", or \"end\" to finish." + + case @input + + when "feed" + feed + + when "walk" + walk + + when "put to bed" + putToBed + + when "toss" + toss + + when "rock" + rock + + when "end" + break + + else + puts "That does not match any option." + end + @input = gets.chomp + end + puts "You say goodbye to the baby dragon" + end + + def feed + puts 'You feed ' + @name + '.' + @stuffInBelly = 10 + passageOfTime + end + + def walk + puts 'You walk ' + @name + '.' + @stuffInIntestine = 0 + passageOfTime + end + + def putToBed + puts 'You put ' + @name + ' to bed.' + @asleep = true + 3.times do + if @asleep + passageOfTime + 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.' + passageOfTime + end + + def rock + puts 'You rock ' + @name + ' gently.' + @asleep = true + puts 'He briefly dozes off...' + passageOfTime + if @asleep + @asleep = false + puts '...but wakes when you stop.' + end + end + + private + + # "private" means that the methods defined here are + # methods internal to the object. (You can feed + # your dragon, but you can't ask him if he's hungry.) + + def hungry? + # Method names can end with "?". + # Usually, we only do this if the method + # returns true or false, like this: + @stuffInBelly <= 2 + end + + def poopy? + @stuffInIntestine >= 8 + end + + def passageOfTime + if @stuffInBelly > 0 + # Move food from belly to intestine. + @stuffInBelly = @stuffInBelly - 1 + @stuffInIntestine = @stuffInIntestine + 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 @stuffInIntestine >= 10 + @stuffInIntestine = 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 + +puts "You create a baby dragon" +pet = Dragon.new 'Norbert' +pet.interact \ No newline at end of file From ef70c758ed7570e075f9b361ea9acd7caf0f237b Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 11:27:42 +0000 Subject: [PATCH 51/54] bottles --- .../ninety_nine_bottles_of_beer.rb | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 840182dcb..763950176 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -164,7 +164,7 @@ def weddingNumber number numString end -#num = 99 + def ninety_nine_bottles_of_beer num while (num > 0) puts weddingNumber(num) + " bottles of beer on the wall, " + weddingNumber(num) + " bottles of beer." @@ -173,19 +173,4 @@ def ninety_nine_bottles_of_beer num end end -=begin -puts weddingNumber( 0) -puts weddingNumber( 9) -puts weddingNumber( 10) -puts weddingNumber( 11) -puts weddingNumber( 17) -puts weddingNumber( 32) -puts weddingNumber( 88) -puts weddingNumber( 99) -puts weddingNumber(100) -puts weddingNumber(101) -puts weddingNumber(234) -puts weddingNumber(3211) -puts weddingNumber(999999) -puts weddingNumber(1000000000000) -=end \ No newline at end of file +ninety_nine_bottles_of_beer 99 From 9de0778054eab463d4936d394ebe44f165cfd03f Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 14:15:00 +0000 Subject: [PATCH 52/54] pics --- .../safer_picture_downloading.rb | 119 +++++++++++++++++- 1 file changed, 118 insertions(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 801de24bd..4c42e770e 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,118 @@ -# your code here \ No newline at end of file +# your code here +=begin +Safer picture downloading. Adapt the picture-downloading/file-renaming +program to your computer by adding some safety features to make sure +you never overwrite a file. A few methods you might find useful are +File.exist? (pass it a filename, and it will return true or false) and exit (like if +return and Napoleon had a baby—it kills your program right where it stands; +this is good for spitting out an error message and then quitting). +=end +# This is where she stores her pictures before + +# she gets her YAML on and moves them to the server. + +# Just for my own convenience, I'll go there now. + +#Dir.chdir 'C:/Documents and Settings/Katy/PictureInbox' + +# First we find all of the pictures to be moved. + +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: " + +# This will be our counter. We'll start at 1 today, + +# though normally I like to count from 0. + +pic_number = 1 + +pic_names.each do |name| + +print '.' # This is our "progress bar". + +# Make sure we don't save over another file!! + + + +new_name = if pic_number < 10 + +"#{batch_name}0#{pic_number}.jpg" + +else + +"#{batch_name}#{pic_number}.jpg" + +end + +i=0 +while File.exist?(new_name) + puts "That file already exists, renaming new file." + new_name = new_name[0..-5] + (i+=1).to_s + ".jpg" +end +i=0 + + + +# This renames the picture, but since "name" +# has a big long path on it, and "new_name" +# doesn't, it also moves the file to the +# current working directory, which is now +# Katy's PictureInbox folder. +# Since it's a *move*, this effectivey +# downloads and deletes the originals. +# And since this is a memory card, not a +# hard drive, each of these takes a second +# or so; hence, the little dots let her +# know that my program didn't hose her machine. +# (Some marriage advice from your favorite +# author/programmer: it's all about the +# little things.) +=begin +The first time I wrote +this program, I forgot +that little line that +increments the +counter. What +happened? It copied +every picture to the +same new filename... +over the previous +picture! This +effectively deleted +every picture except +for the last one to be +copied. Good thing I +always, always, +always make +backups. Because, +you know, the thing +about computers.... +=end + +# Now where were we? Oh, yeah... + +File.rename name, new_name + +# Finally, we increment the counter. + +pic_number = pic_number + 1 + +end + +puts # This is so we aren't on progress bar line. + +puts 'Done, cutie!' +=begin +Nice! Of course, the full program I wrote for her also downloads the movies, +deletes the thumbnails from the camera (since only the camera can use them), +extracts the time and date from the actual .jpg or .avi file, and renames the file +using that. It also makes sure never to copy over an existing file. Yep, it’s a +pretty fancy program, but that’s for another day. +=end \ No newline at end of file From e12932965d66acab466f45a95867a21aaa8e74ea Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 14:24:42 +0000 Subject: [PATCH 53/54] ch11 --- ch11-reading-and-writing/build_a_better_playlist.rb | 8 ++++++++ ch11-reading-and-writing/build_your_own_playlist.rb | 5 ++++- ch11-reading-and-writing/playlist.m3u | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 ch11-reading-and-writing/playlist.m3u diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..c559d1ad4 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,11 @@ def music_shuffle filenames # your code here + #filenames = + filenames.shuffle.shuffle + #l = filenames.length + 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 41b9a1746..5af13b23c 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -25,4 +25,7 @@ def build_your_own_playlist f.write track+"\n" end end -end \ No newline at end of file +puts "Playlist made!" +end + +build_your_own_playlist \ No newline at end of file diff --git a/ch11-reading-and-writing/playlist.m3u b/ch11-reading-and-writing/playlist.m3u new file mode 100644 index 000000000..d4d6328b2 --- /dev/null +++ b/ch11-reading-and-writing/playlist.m3u @@ -0,0 +1,5 @@ +Herbie hancock/02 Watermelon Man.wma +Herbie hancock/03 Sly.wma +Herbie hancock/04 Vein Melter.wma +Herbie hancock/01 Chameleon.wma +Herbie hancock/Head Hunters/02 Watermelon Man.wma From d80ffbe093eccea9d4c1ee67accf50d3eb8df8ef Mon Sep 17 00:00:00 2001 From: daviryan Date: Wed, 14 Jun 2017 14:53:28 +0000 Subject: [PATCH 54/54] ch11 --- .../build_a_better_playlist.rb | 40 ++++++++++++++++--- .../safer_picture_downloading.rb | 6 +-- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index c559d1ad4..59313080c 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,11 +1,41 @@ def music_shuffle filenames - # your code here - #filenames = - filenames.shuffle.shuffle - #l = filenames.length + + arr = filenames.sort + l = filenames.length + mid = l/2 + + 5.times do |i| + li = 0 + ri = mid + shuf = [] + while shuf.length < l + if shuf.length%2 == 0 + shuf << arr[li] + li+=1 + else + shuf << arr[ri] + ri+=1 + end + + end #while + #puts shuf + #print shuf + arr = shuf +end # times do + -end + cut = rand(l) + l.times do |i| + filenames[(cut+i)%l] = arr[i] + end + filenames +end # end of def + songs = ['aa/bbb', 'aa/ccc', 'aa/ddd', 'AAA/xxxx', 'AAA/yyyy', 'AAA/zzzz', 'foo/bar'] puts(music_shuffle(songs)) + +=begin + +=end diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index 4c42e770e..8d6abd485 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -8,13 +8,9 @@ this is good for spitting out an error message and then quitting). =end # This is where she stores her pictures before - # she gets her YAML on and moves them to the server. - # Just for my own convenience, I'll go there now. - -#Dir.chdir 'C:/Documents and Settings/Katy/PictureInbox' - +#Dir.chdir '/Photos/PictureInbox' # First we find all of the pictures to be moved. pic_names = Dir["**/*\.{jpg,JPG}"]