From ea98acc26f0412ec60e27598d2990c817b8c6cdc Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 09:29:47 +0000 Subject: [PATCH 01/36] ask.rb reworked --- ch09-writing-your-own-methods/ask.rb | 34 ++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..3a12b6f51 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,33 @@ 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 #answer = true + else + return false #answer = false + end + break + else + puts 'Please answer "yes" or "no".' + end +end +answer # This is what we return (true or false). +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 11effba1a9949377dedefae8a18194af74042611 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 09:38:13 +0000 Subject: [PATCH 02/36] ask.rb commented out ext 'puts' --- ch09-writing-your-own-methods/ask.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 3a12b6f51..2e780db06 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -16,6 +16,7 @@ def ask question answer # This is what we return (true or false). end +=begin puts 'Hello, and thank you for...' puts ask 'Do you like eating tacos?' # Ignore this return value @@ -30,4 +31,5 @@ def ask question puts 'DEBRIEFING:' puts 'Thank you for...' puts -puts wets_bed \ No newline at end of file +puts wets_bed +=end \ No newline at end of file From efbf80c89446cf1e1bbb7f49bfc560976e9d7eee Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 10:04:45 +0000 Subject: [PATCH 03/36] old_school_roman_numerals.rb --- .../old_school_roman_numerals.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 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..2e5805f56 100644 --- a/ch09-writing-your-own-methods/old_school_roman_numerals.rb +++ b/ch09-writing-your-own-methods/old_school_roman_numerals.rb @@ -1,3 +1,14 @@ -def old_roman_numeral num - # your code here -end \ No newline at end of file +def old_roman_numeral num + # I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000 + m = (num / 1000) #Integer division so produces 0 if no 1,000's. + d = ((num % 1000) / 500) + c = ((num % 500) / 100) + l = ((num % 100) / 50) + x = ((num % 50) / 10) + v = ((num % 10) / 5) + i = (num % 5) + "M"*m + "D"*d + "C"*c + "L"*l + "X"*x + "V"*v + "I"*i +end + +#puts old_roman_numeral 3000 +#puts old_roman_numeral 879 From a81949cc7ccfa2e1fbaa0ac5a2c063b32201f3e5 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 11:11:06 +0000 Subject: [PATCH 04/36] roman_numerals.rb modern method --- .../roman_numerals.rb | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 5c93b59ac..0ab3de2fc 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,50 @@ +=begin +I placed before V or X indicates one less, so four is IV (one less than five) and nine is IX (one less than ten) +X placed before L or C indicates ten less, so forty is XL (ten less than fifty) and ninety is XC (ten less than +a hundred) +C placed before D or M indicates a hundred less, so four hundred is CD (a hundred less than five hundred) and +nine hundred is CM (a hundred less than a thousand)[5] +For example, MCMIV is one thousand nine hundred and four, 1904 (M is a thousand, CM is nine hundred and IV is four). +=end + def roman_numeral num - # your code here -end \ No newline at end of file + # I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000 + retstr = "" + m = (num / 1000) #Integer division so produces 0 if no 1,000's. + d = ((num % 1000) / 500) + c = ((num % 500) / 100) + l = ((num % 100) / 50) + x = ((num % 50) / 10) + v = ((num % 10) / 5) + i = (num % 5) + + hunds = ((num % 1000) / 100) + tens = ((num % 100) / 10) + units = (num % 10) + + retstr += "M"*m + if hunds == 9 + retstr += "CM" #900 Special case + elsif hunds == 4 #400 Special case + retstr += "CD" + else + retstr += + "D"*d + "C"*c #Use default build of Roman Num for hundreds using C's and D's. + end + if tens == 9 + retstr += "XC" #90 Special case + elsif tens == 4 + retstr += "XL" #40 Special case + else + retstr += "L"*l + "X"*x #Use default build of Roman Num for tens using L's and X's. + end + if units == 9 + retstr += "IX" #9 Special case + elsif units == 4 + retstr += "IV" #4 Special case + else + retstr += "V"*v + "I"*i #Use default build of Roman Num for ones using V's and I's. + end + retstr +end + +#puts roman_numeral 1954 \ No newline at end of file From 5ff8f67e86847b0b6f808d3bfd7c68e96de459e3 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 12:36:11 +0000 Subject: [PATCH 05/36] shuffle.rb --- ch10-nothing-new/shuffle.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..68b6699a2 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,21 @@ def shuffle arr - # your code here -end \ No newline at end of file + return arr if arr.length == 1 + #arr.shuffle #Cheating I guess? + #arr.sort_by { rand } #Cheating I guess? + shuffledkeys = [] + shuffled = [] + while shuffledkeys.length < arr.length + shuffledkeys << (0...arr.length).to_a.sample + if shuffledkeys.length >= 2 + if (shuffledkeys.slice(0,shuffledkeys.length - 1)).include? (shuffledkeys.last) + shuffledkeys.pop + end + end + end + for count in 0...arr.length + shuffled << arr[shuffledkeys[count]] + end + shuffled +end + +#print shuffle ["1a","2b","3c","4d","5e","6f","7g","8h","9i","10j","11k","12l"] \ No newline at end of file From 31b2c1358689c8776939d6de8d22cc2bc55ce5c8 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 14:23:44 +0000 Subject: [PATCH 06/36] sort.rb --- ch10-nothing-new/sort.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/sort.rb b/ch10-nothing-new/sort.rb index 44c6deb58..912d37112 100644 --- a/ch10-nothing-new/sort.rb +++ b/ch10-nothing-new/sort.rb @@ -1,3 +1,21 @@ def sort arr - # your code here -end \ No newline at end of file +return arr if arr.count <= 1 + still_unsorted = arr + already_sorted = [] + for count in 0...arr.length + smallest = still_unsorted[0].downcase + key = "" + for counter in 0...still_unsorted.length + if still_unsorted[counter].downcase <= smallest + smallest = still_unsorted[counter].downcase + smallest_orig_case = still_unsorted[counter] + key = counter + end + end + already_sorted.push smallest_orig_case + still_unsorted.delete_at(key.to_i) + end + already_sorted +end + +#print sort ["Glenn", "Is", "writing", "a", "sort", "method", "did", "You", "Know"] \ No newline at end of file From 7dbe6e74dce6633db258fd0395399357c0760ad8 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 14:38:14 +0000 Subject: [PATCH 07/36] dictionary_sort.rb --- ch10-nothing-new/dictionary_sort.rb | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..272d1fafb 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,24 @@ +#I did a dictionary sort for the sort.rb exercise so this code is the same! def dictionary_sort arr - # your code here -end \ No newline at end of file +return arr if arr.count <= 1 + still_unsorted = arr + already_sorted = [] + for count in 0...arr.length + smallest = still_unsorted[0].downcase + key = "" + for counter in 0...still_unsorted.length + if still_unsorted[counter].downcase <= smallest + smallest = still_unsorted[counter].downcase + smallest_orig_case = still_unsorted[counter] + key = counter + end + end + already_sorted.push smallest_orig_case + still_unsorted.delete_at(key.to_i) + end + already_sorted +end + +#print dictionary_sort ["Glenn", "Is", "writing", "a", "sort", "method", "did", "You", "Know"] + +#print dictionary_sort ["singing","A","feel","can", "like","can"] \ No newline at end of file From faa6145bb9ca087b05d1d3e54846bfb9ac46e56f Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 15:57:41 +0000 Subject: [PATCH 08/36] english_number.rb --- ch10-nothing-new/english_number.rb | 209 ++++++++++++++++++++++++++++- 1 file changed, 208 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..89eb9d1da 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,210 @@ def english_number number - # your code here + +if number < 0 # No negative numbers. + +return 'Please enter a number that isn\'t negative.' + end + +if number == 0 + +return 'zero' + +end + +# No more special cases! No more returns! + +num_string = '' # This is the string we will return. + +ones_place = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] + +tens_place = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] + +teenagers = ['eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] + +# "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 +#NEW CODE START3 +write = left/1000000000 # How many billions left? + +left = left - write*1000000000 # Subtract off those billions. + +if write > 0 + +# Now here's the recursion: + +billions = english_number write + +num_string = num_string + billions + ' billion' + +if left > 0 + +# So we don't write 'one billionseventy million'... + +num_string = num_string + ' ' + +end + +end + +#NEW CODE END3 + + +#NEW CODE START2 +write = left/1000000 # How many millions left? + +left = left - write*1000000 # Subtract off those millions. + +if write > 0 + +# Now here's the recursion: + +millions = english_number write + +num_string = num_string + millions + ' million' + +if left > 0 + +# So we don't write 'one millionseventy thousand'... + +num_string = num_string + ' ' + +end + +end + +#NEW CODE END2 + +#NEW CODE START1 +write = left/1000 # How many thousands left? + +left = left - write*1000 # Subtract off those thousands. + +if write > 0 + +# Now here's the recursion: + +thousands = english_number write + +num_string = num_string + thousands + ' thousand' + +if left > 0 + +# So we don't write 'nine thousandnine hundred'... + +num_string = num_string + ' ' + +end + +end + +#NEW CODE END1 +write = left/100 # How many hundreds left? + +left = left - write*100 # Subtract off those hundreds. + +if write > 0 + +# Now here's the recursion: + +hundreds = english_number write + +num_string = num_string + hundreds + ' hundred' + +if left > 0 + +# So we don't write 'two hundredfifty-one'... + +num_string = num_string + ' ' + +end + +end + +write = left/10 # How many tens left? + +left = left - write*10 # Subtract off those tens. + +if write > 0 + +if ((write == 1) and (left > 0)) + +# Since we can't write "tenty-two" instead of + +# "twelve", we have to make a special exception + +# for these. + + +num_string = num_string + teenagers[left-1] + +# The "-1" is because teenagers[3] is + +# 'fourteen', not 'thirteen'. + +# Since we took care of the digit in the + +# ones place already, we have nothing left to write. + +left = 0 + +else + +num_string = num_string + tens_place[write-1] + +# The "-1" is because tens_place[3] is + +# 'forty', not 'thirty'. + +end + +if left > 0 + +# So we don't write 'sixtyfour'... + +num_string = num_string + '-' + +end + +end + +write = left # How many ones left to write out? + +left = 0 # Subtract off those ones. + +if write > 0 + +num_string = num_string + ones_place[write-1] + +# The "-1" is because ones_place[3] is + +# 'four', not 'three'. + +end + +# Now we just return "num_string"... + +num_string + +end + +print english_number( 0); print " "; print 0; puts +print english_number( 9); print " "; print 9; puts +print english_number( 10); print " "; print 10; puts +print english_number( 11); print " "; print 11; puts +print english_number( 17); print " "; print 17; puts +print english_number( 32); print " "; print 32; puts +print english_number( 88); print " "; print 88; puts +print english_number( 99); print " "; print 99; puts +print english_number(100); print " "; print 100; puts +print english_number(101); print " "; print 101; puts +print english_number(234); print " "; print 234; puts +print english_number(3211); print " "; print 3211; puts +print english_number(999999); print " "; print 999999; puts +print english_number(1000000000000); print " "; print 1000000000000; puts + +print english_number(10001); print " "; print 10001; puts +print english_number(1070301); print " "; print 1070301; puts \ No newline at end of file From cc98454212720127171978f5fdaa138988c25dd6 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 16:19:18 +0000 Subject: [PATCH 09/36] ninety_nine_bottles_of_beer.rb set to run 5 (five:) times --- .../ninety_nine_bottles_of_beer.rb | 155 +++++++++++++++++- 1 file changed, 154 insertions(+), 1 deletion(-) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..fb68161fb 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,154 @@ -# your code here \ No newline at end of file +#Using the solution. +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! + + num_string = '' # This is the string we will return. + + ones_place = ['one', 'two', 'three', + 'four', 'five', 'six', + 'seven', 'eight', 'nine'] + + tens_place = ['ten', 'twenty', 'thirty', + 'forty', 'fifty', 'sixty', + 'seventy', 'eighty', 'ninety'] + + + teenagers = ['eleven', 'twelve', 'thirteen', + 'fourteen', 'fifteen', 'sixteen', + 'seventeen', 'eighteen', 'nineteen'] + + zillions = [['hundred', 2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + + # "left" is how much of the number + # we still have left to write out. + # "write" is the part we are + # writing out right now. + # write and left...get it? :) + left = number + + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base # How many zillions left? + left = left - write*zil_base # Subtract off those zillions. + + if write > 0 + # Now here's the recursion: + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + # So we don't write 'two billionfifty-one'... + num_string = num_string + ' ' + end + end + end + + write = left/10 # How many tens left? + left = left - write*10 # Subtract off those tens. + + if write > 0 + if ((write == 1) and (left > 0)) + # Since we can't write "tenty-two" instead of + # "twelve", we have to make a special exception + # for these. + num_string = num_string + teenagers[left-1] + # The "-1" is because teenagers[3] is + # 'fourteen', not 'thirteen'. + + # Since we took care of the digit in the + # ones place already, we have nothing left to write. + left = 0 + else + num_string = num_string + tens_place[write-1] + # The "-1" is because tens_place[3] is + # 'forty', not 'thirty'. + end + + if left > 0 + # So we don't write 'sixtyfour'... + num_string = num_string + '-' + end + end + + write = left # How many ones left to write out? + left = 0 # Subtract off those ones. + + if write > 0 + num_string = num_string + ones_place[write-1] + # The "-1" is because ones_place[3] is + # 'four', not 'three'. + end + + # Now we just return "num_string"... + num_string +end + +# puts english_number( 0) +# puts english_number( 9) +# puts english_number( 10) +# puts english_number( 11) +# puts english_number( 17) +# puts english_number( 32) +# puts english_number( 88) +# puts english_number( 99) +# puts english_number(100) +# puts english_number(101) +# puts english_number(234) +# puts english_number(3211) +# puts english_number(999999) +# puts english_number(1000000000000) +# puts english_number(109238745102938560129834709285360238475982374561034) + +#My code to incorporate the enlgish_number program into "X bottles of beer" +#Note that the "english_number" method needs to be above the code below. + +num_at_start = 5 #9999 # change to 99 if you want + +num_now = num_at_start + +while num_now > 2 + +puts "#{english_number (num_now)} bottles of beer on the wall, " + + +"#{english_number (num_now)} bottles of beer!" + +num_now = num_now - 1 + +puts 'Take one down, pass it around, ' + + +"#{english_number (num_now)} bottles of beer on the wall!" + +end + + From 5fc997568085b5405fa57faf3f995438372dc00f Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 16:32:46 +0000 Subject: [PATCH 10/36] ninety_nine_bottles_of_beer.rb Capitalised the first word. Added some explanatory notes at the top. --- ch10-nothing-new/ninety_nine_bottles_of_beer.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index fb68161fb..57a56724e 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1,4 +1,6 @@ -#Using the solution. +#Using the solution for english_number. +#english_number is included here so that the call to english_number method will run. +#Code for "X bottles of beer" is right at the bottom. def english_number number if number < 0 # No negative numbers. return 'Please enter a number that isn\'t negative.' @@ -130,7 +132,7 @@ def english_number number # puts english_number(1000000000000) # puts english_number(109238745102938560129834709285360238475982374561034) -#My code to incorporate the enlgish_number program into "X bottles of beer" +#My code to incorporate the english_number program into "X bottles of beer" #Note that the "english_number" method needs to be above the code below. num_at_start = 5 #9999 # change to 99 if you want @@ -139,7 +141,7 @@ def english_number number while num_now > 2 -puts "#{english_number (num_now)} bottles of beer on the wall, " + +puts "#{(english_number (num_now)).capitalize} bottles of beer on the wall, " + "#{english_number (num_now)} bottles of beer!" From 4bfd8b3fdf0e5ba0c6d9a0ebd1c38b91079ab0bf Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 18:48:01 +0000 Subject: [PATCH 11/36] safer_picture_downloading.rb Couldn't get code to run. Included code at top to prevent file being overwritten. --- .../safer_picture_downloading.rb | 67 ++++++++++++++++++- 1 file changed, 66 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..d1c21168c 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1 +1,66 @@ -# your code here \ No newline at end of file +=begin +I couldn't get the original file to work. +Error msg was ".rb:19:in `chdir': No such file or directory @ dir_chdir - D:/RUBY_TEST_DELETE/Orig_loc (Errno::ENOENT)" + +My solution would be to insert the following code at the place indicated in the code below +(see "#GB Insert code above here." below). +=end +while FileTest.exist? new_name + new_name += '(duplicate)' + save_name = new_name + '.jpg' +end + +=begin +A few methods you might find useful are File.exist? (pass it a filename, and it will return true or false) +and exit — it kills your program right where it stands; this is good for spitting out an error message and +then quitting). +=end +# For Katy, with love. (I always write little notes in the programs +# I write for her. I deleted all of the dirty ones, though, so that one is all that's left.) +# 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 'D:/RUBY_TEST_DELETE/Orig_loc' + +# First we find all of the pictures to be moved. + +#pic_names = Dir['F:/**/*.jpg'] +pic_names = Dir['D:/RUBY_TEST_DELETE/New_loc/*.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". + +new_name = +if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" +else + "#{batch_name}#{pic_number}.jpg" +end + +#GB Insert code above here. + +# 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 effectively +# 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.) + +# 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!' \ No newline at end of file From 0eb9cc561122de6c0a0a7031c85d3a1a5cec8677 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 12 Sep 2016 20:19:50 +0000 Subject: [PATCH 12/36] build_your_own_playlist.rb Not working. Ref = 'https://github.com/makersacademy/pre_course/issues/639' --- .../build_your_own_playlist.rb | 25 ++++++++++++++++++- 1 file changed, 24 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..b6674e614 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1 +1,24 @@ -# your code here \ No newline at end of file +#I can't get this to work +#ref = https://github.com/makersacademy/pre_course/issues/639 + +# using the shuffle method as defined above + +all_mp3s = (Dir["c:/**/*.mp3"]).shuffle + +#all_mp3s = Dir.entries("C:/Users/g/Desktop/Zim photos - move!/Johnny Clegg & Savuka").select {|f| !File.directory? f} + +=begin +all_mp3s = +"C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\06 Dela.mp3 +C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\07 Siyayilanda.mp3" +=end + +puts all_mp3s + +File.open 'playlist.m3u', 'w' do |f| + all_mp3s.each do |mp3| + f.write mp3+"\n" + end +end + +puts 'Done!' \ No newline at end of file From d7d1941c8a2544fd5b417d11d28fa50ff5c0cbcf Mon Sep 17 00:00:00 2001 From: Glenn Date: Wed, 14 Sep 2016 10:10:18 +0000 Subject: [PATCH 13/36] playlist commit 14-09-2016 --- .../build_a_better_playlist.rb | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index 3b31bd241..f9b79e2ec 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,30 @@ def music_shuffle filenames # your code here end + +#Copy of "build_your_own_playlist.rb" below. +#I can't get this to work +#ref = https://github.com/makersacademy/pre_course/issues/639 + +# using the shuffle method as defined above + +all_mp3s = (Dir["c://**/*.mp3"]).shuffle + +#all_mp3s = Dir.entries("C:/Users/g/Desktop/Zim photos - move!/Johnny Clegg & Savuka").select {|f| !File.directory? f} + +=begin +all_mp3s = +"C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\06 Dela.mp3 +C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\07 Siyayilanda.mp3" +=end + +puts all_mp3s + +File.open 'playlist.m3u', 'w' do |f| + all_mp3s.each do |mp3| + f.write mp3+"\n" + end +end + +puts 'Done!' + From 2ba0ff3cc660e0568aaf9c334fbcb8f8c7289124 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 10:07:51 +0000 Subject: [PATCH 14/36] Test --- ch11-reading-and-writing/build_your_own_playlist.rb | 2 +- ch11-reading-and-writing/playlist.m3u | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 ch11-reading-and-writing/playlist.m3u diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index b6674e614..d15ace7d9 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1,4 +1,4 @@ -#I can't get this to work +#I can't get this to work... #ref = https://github.com/makersacademy/pre_course/issues/639 # using the shuffle method as defined above diff --git a/ch11-reading-and-writing/playlist.m3u b/ch11-reading-and-writing/playlist.m3u new file mode 100644 index 000000000..bf013679f --- /dev/null +++ b/ch11-reading-and-writing/playlist.m3u @@ -0,0 +1 @@ +Testgb \ No newline at end of file From cde027af213a109bdf406845da0eea8c89a13b33 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 11:13:50 +0000 Subject: [PATCH 15/36] Happy birthday to you! --- ch12-new-classes-of-objects/happy_birthday.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ch12-new-classes-of-objects/happy_birthday.rb b/ch12-new-classes-of-objects/happy_birthday.rb index 801de24bd..19738da27 100644 --- a/ch12-new-classes-of-objects/happy_birthday.rb +++ b/ch12-new-classes-of-objects/happy_birthday.rb @@ -1 +1,16 @@ -# your code here \ No newline at end of file +require 'date' +puts "What is your birth year?" +year = gets.chomp.to_i +puts "What is your birth month (enter a number from 1 to 12)?" +month = gets.chomp.to_i +puts "What day of the month were you born on?" +day = gets.chomp.to_i + +birthday = Date.new(year,month,day) +puts birthday +puts DateTime.now +age = (DateTime.now - birthday).to_i/365 +puts "Age = #{age}." +arr = [] +age.times {arr << "Spank"} +print arr.join(", ") + "!!!" \ No newline at end of file From bcafe2c1a3f88db2c1093073efff6881834a287f Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 12:11:48 +0000 Subject: [PATCH 16/36] 1 billions secs old. --- ch12-new-classes-of-objects/one_billion_seconds.rb | 14 +++++++++++++- 1 file changed, 13 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..0eb35386a 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1 +1,13 @@ -# your code here \ No newline at end of file +require 'date' +puts "When is your birthday? (dd/mm/yyyy)" +bday = gets.chomp +day = bday[0..1].to_i +month = bday[3..4].to_i +year = bday[6..9].to_i +dob = Time.new(year,month,day) #A Time is a number of seconds since an epoch whereas a DateTime is a number of days since an epoch which is why adding 1 to a DateTime adds a whole day. +bill_secs = (dob + 1000000000) +if bill_secs <= Time.now + print "You were 1 billion seconds old on #{bill_secs.strftime "%d/%m/%Y"}." +elsif bill_secs > Time.now + print "You will be 1 billion seconds old on #{bill_secs.strftime "%d/%m/%Y"}." +end \ No newline at end of file From 361bbc467551e474a93d7cd9b36f7753ee27886b Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 12:43:00 +0000 Subject: [PATCH 17/36] Amended to include (optional) entry of hours, mins and secs. --- .../one_billion_seconds.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/ch12-new-classes-of-objects/one_billion_seconds.rb b/ch12-new-classes-of-objects/one_billion_seconds.rb index 0eb35386a..bde5a374d 100644 --- a/ch12-new-classes-of-objects/one_billion_seconds.rb +++ b/ch12-new-classes-of-objects/one_billion_seconds.rb @@ -1,13 +1,24 @@ require 'date' puts "When is your birthday? (dd/mm/yyyy)" bday = gets.chomp +puts "What was the time? (hh:mm:ss). Leave blank if you do not know." +puts "Use 24 hour clock with 2 digits for hours, mins and secs." +puts "e.g. (for 1pm - when mins and secs are not known, enter 13), " +puts "(for 2:35am - when seconds are not known, enter 02:35), " +puts "(for 8:35:26pm, enter 20:35:26)" +time = gets.chomp +hours = time[0..1].to_i +mins = time[3..4].to_i +secs = time[6..8].to_i + day = bday[0..1].to_i month = bday[3..4].to_i year = bday[6..9].to_i -dob = Time.new(year,month,day) #A Time is a number of seconds since an epoch whereas a DateTime is a number of days since an epoch which is why adding 1 to a DateTime adds a whole day. +dob = Time.new(year,month,day,hours,mins,secs) #A Time is a number of seconds since an epoch whereas a DateTime is a number of days since an epoch which is why adding 1 to a DateTime adds a whole day. + bill_secs = (dob + 1000000000) if bill_secs <= Time.now - print "You were 1 billion seconds old on #{bill_secs.strftime "%d/%m/%Y"}." + print "You were 1 billion seconds old on #{bill_secs.strftime "%d/%m/%Y"} at #{bill_secs.strftime "%H:%M:%S"}." elsif bill_secs > Time.now - print "You will be 1 billion seconds old on #{bill_secs.strftime "%d/%m/%Y"}." + print "You will be 1 billion seconds old on #{bill_secs.strftime "%d/%m/%Y"} at #{bill_secs.strftime "%H:%M:%S"}." end \ No newline at end of file From f30a38b5ea8931af387d2a7fea10f37390d2a2f1 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 16:32:38 +0000 Subject: [PATCH 18/36] Roman numerals to decimal conversion prog. --- ...party_like_its_roman_to_integer_mcmxcix.rb | 86 ++++++++++++++++++- 1 file changed, 84 insertions(+), 2 deletions(-) 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..95b5503b1 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,85 @@ +#Not all rules have been implemented. + def roman_to_integer roman - # your code here -end \ No newline at end of file + roman = roman.upcase + roman_num = roman.split(//) + nums = {"I" => 1,"V" => 5,"X" => 10,"L" => 50,"C" => 100,"D" => 500,"M" => 1000} + total = 0 + prev = 0 + num_less_count = 0 + continuous_chars = 1 + invalid_char = false + roman_num.reverse! + if (roman_num - ["I","V","X","L","C","D","M"]) != [] + invalid_char = true + end + if invalid_char != true + for counter in 0...roman_num.length-1 #Check that not more than 3 of the same chars in a row + if nums[roman_num[counter]] == nums[roman_num[counter+1]] + continuous_chars += 1 + break if continuous_chars > 3 + elsif nums[roman_num[counter]] != nums[roman_num[counter+1]] + continuous_chars = 1 + end + end + for count in 0...roman_num.length + if nums[roman_num[count]] >= prev + total += nums[roman_num[count]] + prev = nums[roman_num[count]] + num_less_count = 0 + elsif nums[roman_num[count]] < prev + total -= nums[roman_num[count]] + num_less_count += 1 #You would not put more than one smaller number in front of a larger number to subtract. + break if num_less_count > 1 + end + end + + end + if invalid_char + puts "Invalid character in your roman numeral. Only I, V, X, L, C, D and M allowed." + return + end + if num_less_count > 1 + puts "Invalid Roman Numeral - only 1 character can denote less than, e.g. 'IX' is valid but 'IIX' is not." + return + end + if continuous_chars > 3 + puts "Invalid Roman Numeral - more than 3 of the same character in a row." + return + end + total + end + +# print roman_to_integer('XXkIV');puts #Invalid representation of 23 - should be "XXIII" +# print roman_to_integer('aXXIV');puts #Invalid representation of 23 - should be "XXIII" +# print roman_to_integer('XXIVr');puts #Invalid representation of 23 - should be "XXIII" +# print roman_to_integer('XXIIV');puts #Invalid representation of 23 - should be "XXIII" +# print roman_to_integer('MXXXLV');puts #Invalid representation of 1025 - should be "MXXV" +# print roman_to_integer('MXXXX');puts #Invalid representation of 1040 - should be "MXL" +# print roman_to_integer('MXXXXV');puts #Invalid representation of 1045 - should be "MXLV" +# print roman_to_integer('mcmlxviii');puts #for 1968 +# print roman_to_integer('XXVI');puts #for 26 +# print roman_to_integer('XXIV');puts #for 24 +# print roman_to_integer('MCMLXXXVI');puts #for 1986 +# print roman_to_integer ("mcmxcix");puts #for 1999 + +=begin +RULES +http://www.blackwasp.co.uk/RomanToNumber.aspx +1)When a symbol appears after a larger symbol it is added. +2)A small-value numeral may be placed to the left of a larger value. +Where this occurs, for example IX, the smaller numeral is subtracted from the larger. +This means that IX is nine and IV is four. The subtracted digit must be at least one tenth +of the value of the larger numeral and must be either I, X or C. Accordingly, ninety-nine is not +IC but rather XCIX. The XC part represents ninety and the IX adds the nine. In addition, once a value +has been subtracted from another, no further numeral or pair may match or exceed the subtracted value. +This disallows values such as MCMD or CMC. + +You would not put more than one smaller number in front of a larger number to subtract. For example, IIV would not mean 3. + +3) Don't use the same symbol more than three times in a row. + +4) You must separate ones, tens, hundreds, and thousands as separate items. That means that 99 is XCIX, 90 + 9, +but never should be written as IC. Similarly, 999 cannot be IM and 1999 cannot be MIM. + +=end From 3e18d280caf424083baa62ee8d45a14c3835ba2a Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 18:55:55 +0000 Subject: [PATCH 19/36] 1st draft of happy_birthday.rb. --- .../birthday_helper.rb | 26 ++++++++++++++++++- ch12-new-classes-of-objects/names.txt | 8 ++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 ch12-new-classes-of-objects/names.txt diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 801de24bd..5c3eb63ce 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1 +1,25 @@ -# your code here \ No newline at end of file +# your code here +# You’ll probably want to break each line up and put it in a hash, using the name as your key +#and the date as your value. In other words: words: birth_dates['The King of Spain'] = 'Jan 5, 1938' + +#[{:name=>"Christopher Alexander", :month_day=>"Oct 4", :year=>"1936"}, +#{:name=>"Christopher Lambert", :month_day=>"Mar 29", :year=>"1957"}, etc.] +birth_dates=[] +file = File.open("names.txt", "r") +file.readlines.each{|person| +name,month_day,year = person.chomp.split(",") +name.strip!; month_day.strip!; year.strip! + birth_dates << {name: name,month_day: month_day,year: year} +} +file.close +month_day_get = "" +year_get = "" +puts "Enter a name." +name_get = gets.chomp +person = birth_dates.select {|person| person[:name] == name_get } +month_day_get = person[0]["month_day".to_sym] +year_get = person[0][:year] + +puts "Hi #{name_get}! You were born on #{month_day_get} in #{year_get}" + + diff --git a/ch12-new-classes-of-objects/names.txt b/ch12-new-classes-of-objects/names.txt new file mode 100644 index 000000000..1f27bb6f4 --- /dev/null +++ b/ch12-new-classes-of-objects/names.txt @@ -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 From 5f4ced6e0e22f959e6821f101f9a438176416279 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 20:24:46 +0000 Subject: [PATCH 20/36] birthday_helper.rb calculates next birthday and age on next birthday. --- .../birthday_helper.rb | 33 +++++++++++++------ ch12-new-classes-of-objects/names.txt | 1 + 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/ch12-new-classes-of-objects/birthday_helper.rb b/ch12-new-classes-of-objects/birthday_helper.rb index 5c3eb63ce..8d5d25a76 100644 --- a/ch12-new-classes-of-objects/birthday_helper.rb +++ b/ch12-new-classes-of-objects/birthday_helper.rb @@ -1,13 +1,12 @@ -# your code here -# You’ll probably want to break each line up and put it in a hash, using the name as your key -#and the date as your value. In other words: words: birth_dates['The King of Spain'] = 'Jan 5, 1938' - -#[{:name=>"Christopher Alexander", :month_day=>"Oct 4", :year=>"1936"}, -#{:name=>"Christopher Lambert", :month_day=>"Mar 29", :year=>"1957"}, etc.] +require 'date' birth_dates=[] file = File.open("names.txt", "r") file.readlines.each{|person| name,month_day,year = person.chomp.split(",") +if name.nil? + puts "Error: Blank lines in the file." + exit +end name.strip!; month_day.strip!; year.strip! birth_dates << {name: name,month_day: month_day,year: year} } @@ -17,9 +16,23 @@ puts "Enter a name." name_get = gets.chomp person = birth_dates.select {|person| person[:name] == name_get } +if person == [] + puts "Error: That person does not exist in the file." + exit +end month_day_get = person[0]["month_day".to_sym] -year_get = person[0][:year] +year_get = person[0][:year].to_i +month_get = month_day_get[0..2] +day_get = month_day_get.delete("^0-9") +year_now = Time.now.year +puts year_now +date_now = Time.now +if date_now > Time.new(year_now,month_get,day_get) + year_now+=1 +end +puts "Hi #{name_get}! You were born on #{month_day_get} in #{year_get}." +puts "Your next birthday is on #{day_get} #{month_get} in #{year_now}." +puts "On your next birthday you will be #{year_now-year_get} years old." -puts "Hi #{name_get}! You were born on #{month_day_get} in #{year_get}" - - +#[{:name=>"Christopher Alexander", :month_day=>"Oct 4", :year=>"1936"}, +#{:name=>"Christopher Lambert", :month_day=>"Mar 29", :year=>"1957"}, etc.] diff --git a/ch12-new-classes-of-objects/names.txt b/ch12-new-classes-of-objects/names.txt index 1f27bb6f4..06a26f855 100644 --- a/ch12-new-classes-of-objects/names.txt +++ b/ch12-new-classes-of-objects/names.txt @@ -6,3 +6,4 @@ Christopher Pine, Aug 3, 1976 Christopher Plummer, Dec 13, 1927 Christopher Walken, Mar 31, 1943 The King of Spain, Jan 5, 1938 +Eamon Targaryen, Jan 1 ,1923 From 31dd4ba048ad12a9d70e3559ea7e4ac776ab5c27 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 20:48:46 +0000 Subject: [PATCH 21/36] Amended array class to include shuffle method. --- .../extend_built_in_classes.rb | 30 +++++++++++++++++-- 1 file changed, 28 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 c3e793933..35ea502bf 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,3 +1,29 @@ class Integer - # your code here -end \ No newline at end of file + +end + +class Array + def shuffle #Using my shuffle method from before! + arr = self + return arr if arr.length == 1 + shuffledkeys = [] + shuffled = [] + while shuffledkeys.length < arr.length + shuffledkeys << (0...arr.length).to_a.sample + if shuffledkeys.length >= 2 + if (shuffledkeys.slice(0,shuffledkeys.length - 1)).include? (shuffledkeys.last) + shuffledkeys.pop + end + end + end + for count in 0...arr.length + shuffled << arr[shuffledkeys[count]] + end + shuffled + end + +end + +testy = Array.new ["1a","2b","3c","4d","5e","6f","7g","8h","9i","10j","11k","12l"] +print testy.shuffle + \ No newline at end of file From d46cfce8498d7566f67f572b5e82609d1f968167 Mon Sep 17 00:00:00 2001 From: Glenn Date: Thu, 22 Sep 2016 21:07:35 +0000 Subject: [PATCH 22/36] Amended integer class to include factorial method. --- ch13-creating-new-classes/extend_built_in_classes.rb | 12 ++++++++++-- 1 file changed, 10 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 35ea502bf..5597b1007 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -1,7 +1,15 @@ class Integer + def factorial + n = self + #n < 2 ? 1 : factorial(n-1) * n + n < 2 ? 1 : (n-1).factorial * n #Now a mthod of the class integer. + end end +#testfact = Integer.new 8 #Don't need this as automatically added??? +print 8.factorial + class Array def shuffle #Using my shuffle method from before! arr = self @@ -24,6 +32,6 @@ def shuffle #Using my shuffle method from before! end -testy = Array.new ["1a","2b","3c","4d","5e","6f","7g","8h","9i","10j","11k","12l"] -print testy.shuffle +#testy = Array.new ["1a","2b","3c","4d","5e","6f","7g","8h","9i","10j","11k","12l"] +#print testy.shuffle \ No newline at end of file From 11bb47c30798ce6d69724a4950306235ff1a4719 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 07:30:41 +0000 Subject: [PATCH 23/36] Added the 'roman' method to the Integer class. --- .../extend_built_in_classes.rb | 48 +++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/ch13-creating-new-classes/extend_built_in_classes.rb b/ch13-creating-new-classes/extend_built_in_classes.rb index 5597b1007..5df23ab34 100644 --- a/ch13-creating-new-classes/extend_built_in_classes.rb +++ b/ch13-creating-new-classes/extend_built_in_classes.rb @@ -4,11 +4,53 @@ def factorial #n < 2 ? 1 : factorial(n-1) * n n < 2 ? 1 : (n-1).factorial * n #Now a mthod of the class integer. end - + + def roman + num = self + # I = 1 V = 5 X = 10 L = 50 C = 100 D = 500 M = 1000 + retstr = "" + m = (num / 1000) #Integer division so produces 0 if no 1,000's. + d = ((num % 1000) / 500) + c = ((num % 500) / 100) + l = ((num % 100) / 50) + x = ((num % 50) / 10) + v = ((num % 10) / 5) + i = (num % 5) + + hunds = ((num % 1000) / 100) + tens = ((num % 100) / 10) + units = (num % 10) + + retstr += "M"*m + if hunds == 9 + retstr += "CM" #900 Special case + elsif hunds == 4 #400 Special case + retstr += "CD" + else + retstr += + "D"*d + "C"*c #Use default build of Roman Num for hundreds using C's and D's. + end + if tens == 9 + retstr += "XC" #90 Special case + elsif tens == 4 + retstr += "XL" #40 Special case + else + retstr += "L"*l + "X"*x #Use default build of Roman Num for tens using L's and X's. + end + if units == 9 + retstr += "IX" #9 Special case + elsif units == 4 + retstr += "IV" #4 Special case + else + retstr += "V"*v + "I"*i #Use default build of Roman Num for ones using V's and I's. + end + retstr + end end -#testfact = Integer.new 8 #Don't need this as automatically added??? -print 8.factorial + +#print 1248.roman;puts +#testfact = Integer.new 8 #Don't need this as a number is automatically added to the Integer class??? +#print 9.factorial class Array def shuffle #Using my shuffle method from before! From ea2261b2da2c9284429bf11369e78374895088fc Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 09:48:09 +0000 Subject: [PATCH 24/36] Profiling can be turned on and off now. --- .../even_better_profiling.rb | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/even_better_profiling.rb b/ch14-blocks-and-procs/even_better_profiling.rb index b01b78fd8..b9e3f21b8 100644 --- a/ch14-blocks-and-procs/even_better_profiling.rb +++ b/ch14-blocks-and-procs/even_better_profiling.rb @@ -1,3 +1,20 @@ def profile block_description, &block - # your code here -end \ No newline at end of file + do_profile = false + if do_profile + start_time = Time.new + block.call + duration = Time.new - start_time + puts "'#{block_description}' took #{duration} seconds to run." + else + block.call + end +end + +profile 'Count to a high number' do + num = 0 + 1000000.times do + num += 1 + end + puts num +end + From 4be6c7e7e2de8c6c78b6c56db8cecff2e533969d Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 10:20:12 +0000 Subject: [PATCH 25/36] When called, clock 'dongs' according to the number of hours in the current time. --- ch14-blocks-and-procs/grandfather_clock.rb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/ch14-blocks-and-procs/grandfather_clock.rb b/ch14-blocks-and-procs/grandfather_clock.rb index 916f6d354..d12a6fb44 100644 --- a/ch14-blocks-and-procs/grandfather_clock.rb +++ b/ch14-blocks-and-procs/grandfather_clock.rb @@ -1,3 +1,18 @@ +=begin +Grandfather clock. Write a method that takes a block and calls it once for each hour that has passed today. +That way, if I were to pass in the block: do puts 'DONG!' end +it would chime (sort of) like a grandfather clock. Test your method out with a few different blocks. +Hint: You can use Time.new.hour to get the current hour. However, this returns a number between 0 and 23, +so you will have to alter those numbers in order to get ordinary clock-face numbers (1 to 12). +=end + + def grandfather_clock &block - # your code here + hour = Time.new.hour + 1 + hour = hour - 12 if hour > 13 + hour.times {block.call} +end + +grandfather_clock do + print "DONG!" end \ No newline at end of file From 9f28c6633880a87d2b0ac0d31012afe5ab4b11f0 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 11:22:00 +0000 Subject: [PATCH 26/36] Prog logger. --- ch14-blocks-and-procs/program_logger.rb | 35 +++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/program_logger.rb b/ch14-blocks-and-procs/program_logger.rb index 0e2e18d57..4b652c45a 100644 --- a/ch14-blocks-and-procs/program_logger.rb +++ b/ch14-blocks-and-procs/program_logger.rb @@ -1,3 +1,34 @@ +=begin +Write a method called log that takes a string description of a block (and, of course, a block). +Similar to the method do_self_importantly, it should puts a string telling you it started the block +and another string at the end telling you it finished and also telling you what the block returned. +Test your method by sending it a code block. Inside the block, put another call to log, passing a block to it. +In other words, your output should look something like this: +Beginning "outer block"... +Beginning "some little block"... +..."some little block" finished, returning: +5 +Beginning "yet another block"... +..."yet another block" finished, returning: +I like Thai food! +..."outer block" finished, returning: false +=end def log desc, &block - # your code here -end \ No newline at end of file + puts "Beginning \"#{desc}\"..." + called = block.call + puts "...\"#{desc}\" finished, returning: " + puts called + false +end + +log "outer block" do + log "some little block" do + time = Time.now + "The time 5 hours ago was #{time - (5*60*60)}" + end + + log "yet another block" do + "Thai food is my favourite!!!" + end + false +end \ No newline at end of file From a4187d9943e69950e3c1e24ddd26d80c2e09e103 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 12:49:19 +0000 Subject: [PATCH 27/36] 'better_program_logger.rb' with double spacing for each sub level reached. --- .../better_program_logger.rb | 54 ++++++++++++++++++- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/ch14-blocks-and-procs/better_program_logger.rb b/ch14-blocks-and-procs/better_program_logger.rb index 0e2e18d57..3a283c6d9 100644 --- a/ch14-blocks-and-procs/better_program_logger.rb +++ b/ch14-blocks-and-procs/better_program_logger.rb @@ -1,3 +1,53 @@ +=begin +Beginning "outer block"... + Beginning "some little block"... + Beginning "teeny-tiny block"... + ..."teeny-tiny block" finished, returning: + lots of love + ..."some little block" finished, returning: + 42 + Beginning "yet another block"... + ..."yet another block" finished, returning: + I love Indian food! +..."outer block" finished, returning: +true + +=end +$nesting_depth = 0 def log desc, &block - # your code here -end \ No newline at end of file + $nesting_depth.times {print " "} + puts "Beginning \"#{desc}\"..." + $nesting_depth += 1 + called = block.call + $nesting_depth -= 1 + $nesting_depth.times {print " "} + puts "...\"#{desc}\" finished, returning: " + $nesting_depth.times {print " "} + puts called + false +end + +log "outer block" do + log "some little block" do + log "block a 3rd level down" do + str = "" + if rand(2) == 1 + str += "This is a great day!!! " + end + if rand(3) == 1 + str += "Correction: not such a great day afterall!!!" if str != "" + end + if str == "" + str = "No comment on the day!!!" + end + str + end + time = Time.now + "The time 5 hours ago was #{time - (5*60*60)}" + end + + log "yet another block" do + "Thai food is my favourite!!!" + end + false +end \ No newline at end of file From 9633babe9a7866b34936ae44e96840011e642904 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 16:07:49 +0000 Subject: [PATCH 28/36] Interim orange tree. Does all but can't sponteously die yet! --- ch13-creating-new-classes/orange_tree.rb | 89 +++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index 025d08907..adc3bd797 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -7,5 +7,92 @@ class OrangeTree - # your code here + def initialize name + @name = name + @growth = 0 + @oranges = false + @orange_count = 0 + @age = 0 + @alive = true + end + + def height + @height + end + + def pick_an_orange(picked = 1) + if @alive && @oranges + print "You picked #{picked} juicy orange" + puts "." if picked == 1 + puts "s." if picked > 1 + if @orange_count - picked < 0 + puts "Sorry, not enough oranges on the tree to pick #{picked}." + else + @orange_count -= picked + puts "There is 1 orange left on the tree." if @orange_count == 1 + puts "There are #{@orange_count} oranges left on the tree." if @orange_count > 1 + end + elsif @alive && !@oranges + puts "Sorry, the tree is too young to produce oranges so you can't pick any." + else + puts "Sorry, a dead tree produces no oranges so you can't pick any." + end + end + + + def one_year_passes + @growth += 0.4 + @age += 1 + @orange_count = ((@growth * 15) - 25).round #Resets each year. + @oranges = true if @age > 5 && @alive == true + if @age <= 25 + + puts "Your orange tree '#{@name}' is now #{@age} years old." + puts "It is #{@growth.round(2)}m high." + + puts "Your orange tree '#{@name}' is now old enough to produce oranges." if @age == 6 + puts "Your tree has produced #{((@growth * 15) - 25).round} oranges this year!" if @age >= 6 + elsif @age > 25 + puts "Your orange tree '#{@name}' has died." + @alive = false + @oranges = false + end + end end + +ot = OrangeTree.new "JuicyLucy" + +ot.one_year_passes +ot.pick_an_orange +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.pick_an_orange 7 +ot.pick_an_orange 2 +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.pick_an_orange(5) +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.pick_an_orange (9) +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.one_year_passes +ot.pick_an_orange 21 +ot.one_year_passes From 3712fe1752db599d48ac96daa0261f3ecff98b0a Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 16:45:13 +0000 Subject: [PATCH 29/36] orange tree. Now the tree can sponteously die after the age of 20. --- ch13-creating-new-classes/orange_tree.rb | 43 ++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/ch13-creating-new-classes/orange_tree.rb b/ch13-creating-new-classes/orange_tree.rb index adc3bd797..196768389 100644 --- a/ch13-creating-new-classes/orange_tree.rb +++ b/ch13-creating-new-classes/orange_tree.rb @@ -20,6 +20,11 @@ def height @height end + def count_the_oranges + @orange_count = 0 if !@alive || @age <= 5 + @orange_count + end + def pick_an_orange(picked = 1) if @alive && @oranges print "You picked #{picked} juicy orange" @@ -39,22 +44,37 @@ def pick_an_orange(picked = 1) end end + def possible_spontaneous_death_by_nematode_infestation + if @age >= 20 && rand(3) == 1 && @alive + puts "Unfortunately the tree just died. There was a 66.66% chance it would survive but sometimes bad stuff beats the odds!" + @alive = false + @orange_count = 0 + elsif @age < 20 && @alive + puts "The tree is young enough to fight off the nematode infection." + elsif !@alive + puts "This tree is already dead. Nematodes do not affect a dead tree!" + else + puts "The tree successfully fought off the nematode infection." + end + end + def one_year_passes @growth += 0.4 @age += 1 @orange_count = ((@growth * 15) - 25).round #Resets each year. @oranges = true if @age > 5 && @alive == true - if @age <= 25 + if @age <= 25 && @alive puts "Your orange tree '#{@name}' is now #{@age} years old." puts "It is #{@growth.round(2)}m high." puts "Your orange tree '#{@name}' is now old enough to produce oranges." if @age == 6 puts "Your tree has produced #{((@growth * 15) - 25).round} oranges this year!" if @age >= 6 - elsif @age > 25 + elsif @age > 25 || !@alive puts "Your orange tree '#{@name}' has died." @alive = false + @orange_count = 0 @oranges = false end end @@ -65,14 +85,23 @@ def one_year_passes ot.one_year_passes ot.pick_an_orange ot.one_year_passes +puts ot.count_the_oranges +ot.possible_spontaneous_death_by_nematode_infestation ot.one_year_passes ot.one_year_passes +puts ot.count_the_oranges ot.one_year_passes ot.one_year_passes ot.pick_an_orange 7 +puts ot.count_the_oranges ot.pick_an_orange 2 +ot.pick_an_orange 3 +puts ot.count_the_oranges ot.one_year_passes +puts ot.count_the_oranges +ot.pick_an_orange 17 ot.one_year_passes +ot.possible_spontaneous_death_by_nematode_infestation ot.one_year_passes ot.one_year_passes ot.one_year_passes @@ -80,19 +109,29 @@ def one_year_passes ot.one_year_passes ot.one_year_passes ot.one_year_passes +ot.pick_an_orange(100) ot.one_year_passes ot.one_year_passes ot.one_year_passes +puts ot.count_the_oranges ot.one_year_passes ot.one_year_passes ot.one_year_passes ot.one_year_passes +ot.possible_spontaneous_death_by_nematode_infestation ot.one_year_passes ot.one_year_passes ot.pick_an_orange (9) +ot.possible_spontaneous_death_by_nematode_infestation +ot.one_year_passes +ot.possible_spontaneous_death_by_nematode_infestation ot.one_year_passes +ot.possible_spontaneous_death_by_nematode_infestation +puts ot.count_the_oranges ot.one_year_passes +ot.possible_spontaneous_death_by_nematode_infestation ot.one_year_passes +puts ot.count_the_oranges ot.one_year_passes ot.pick_an_orange 21 ot.one_year_passes From 1007d860fffae3d8c3163f206a0f31df199485a7 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 18:49:49 +0000 Subject: [PATCH 30/36] dragon prog enhanced to include user interface. --- .../interactive_baby_dragon.rb | 78 ++++++++++++++++++- 1 file changed, 77 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..db922f59e 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -1 +1,77 @@ -# your code here \ No newline at end of file +=begin + Interactive baby dragon. Write a program that lets you enter commands such as feed and walk and + calls those methods on your dragon. Of course, since you are inputting just strings, you will need + some sort of method dispatch, where your program checks which string was entered and then calls the + appropriate method. +=end + +class Dragon + def initialize (name) + @name = name.capitalize + puts "Your new Dragon's name is '#{@name}'." + end + + def name + @name + end + + def feed + puts "You have just fed your pet dragon '#{@name}'." + end + + def walk + puts "You have just walked your pet dragon '#{@name}'." + end + + def tickle + puts "'#{@name}' is enjoying having his tummy tickled. \"Gneeearh, gneeearh, gneeearh\", he goes which is like purring but for dragons." + end + + def quit + puts "'#{@name}' is mighty sad to see you go!" + exit + end + +end + +puts "I've heard you got yourself a dragon pet. What's his name?" +name = gets.chomp +petd = Dragon.new (name) +puts "What would you like to do now?" +puts "You can do the following with '#{petd.name}' the dragon: 'feed', 'walk', 'tickle' or 'quit'." +puts "Enter one of these commands:" +cmd = gets.chomp +askagain = true +while askagain == true + cmd.downcase! + case cmd + when "feed" + petd.feed + cmd = "" + when "walk" + petd.walk + cmd = "" + when "tickle" + petd.tickle + cmd = "" + when "quit" + petd.quit + #askagain = false + else + puts "That command is not recognised. Please enter a recognised command." + askagain = true + end + puts + puts "What would you like to do now?" + puts "You can do the following with '#{petd.name}' the dragon: 'feed', 'walk', 'tickle' or 'quit'." + puts "Enter one of these commands:" + cmd = gets.chomp + +end +#puts petd.name + +#petd.dispatch "feed" +#petd.dispatch "walk" + + + From d178d105b9fcc30d58ccb1aff8a06c056d5b9d45 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 18:55:55 +0000 Subject: [PATCH 31/36] Tidied up the program. --- .../interactive_baby_dragon.rb | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/ch13-creating-new-classes/interactive_baby_dragon.rb b/ch13-creating-new-classes/interactive_baby_dragon.rb index db922f59e..f96aafff6 100644 --- a/ch13-creating-new-classes/interactive_baby_dragon.rb +++ b/ch13-creating-new-classes/interactive_baby_dragon.rb @@ -10,28 +10,22 @@ def initialize (name) @name = name.capitalize puts "Your new Dragon's name is '#{@name}'." end - def name @name end - def feed puts "You have just fed your pet dragon '#{@name}'." end - def walk puts "You have just walked your pet dragon '#{@name}'." end - def tickle puts "'#{@name}' is enjoying having his tummy tickled. \"Gneeearh, gneeearh, gneeearh\", he goes which is like purring but for dragons." end - def quit puts "'#{@name}' is mighty sad to see you go!" exit end - end puts "I've heard you got yourself a dragon pet. What's his name?" @@ -42,7 +36,7 @@ def quit puts "Enter one of these commands:" cmd = gets.chomp askagain = true -while askagain == true +while true cmd.downcase! case cmd when "feed" @@ -56,22 +50,15 @@ def quit cmd = "" when "quit" petd.quit - #askagain = false else puts "That command is not recognised. Please enter a recognised command." - askagain = true end puts puts "What would you like to do now?" puts "You can do the following with '#{petd.name}' the dragon: 'feed', 'walk', 'tickle' or 'quit'." puts "Enter one of these commands:" cmd = gets.chomp - end -#puts petd.name - -#petd.dispatch "feed" -#petd.dispatch "walk" From 471865d2838dc4b199e27592005bbb8226c1a930 Mon Sep 17 00:00:00 2001 From: Glenn Date: Fri, 23 Sep 2016 21:41:03 +0000 Subject: [PATCH 32/36] Amended to append "(duplicate)" onto a file that would be overwritten, i.e. has the same name. --- .../safer_picture_downloading.rb | 84 +++++++------------ 1 file changed, 31 insertions(+), 53 deletions(-) diff --git a/ch11-reading-and-writing/safer_picture_downloading.rb b/ch11-reading-and-writing/safer_picture_downloading.rb index d1c21168c..8a9463e6e 100644 --- a/ch11-reading-and-writing/safer_picture_downloading.rb +++ b/ch11-reading-and-writing/safer_picture_downloading.rb @@ -1,66 +1,44 @@ -=begin -I couldn't get the original file to work. -Error msg was ".rb:19:in `chdir': No such file or directory @ dir_chdir - D:/RUBY_TEST_DELETE/Orig_loc (Errno::ENOENT)" - -My solution would be to insert the following code at the place indicated in the code below -(see "#GB Insert code above here." below). -=end -while FileTest.exist? new_name - new_name += '(duplicate)' - save_name = new_name + '.jpg' -end - -=begin -A few methods you might find useful are File.exist? (pass it a filename, and it will return true or false) -and exit — it kills your program right where it stands; this is good for spitting out an error message and -then quitting). -=end -# For Katy, with love. (I always write little notes in the programs -# I write for her. I deleted all of the dirty ones, though, so that one is all that's left.) -# 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 'D:/RUBY_TEST_DELETE/Orig_loc' - -# First we find all of the pictures to be moved. - -#pic_names = Dir['F:/**/*.jpg'] -pic_names = Dir['D:/RUBY_TEST_DELETE/New_loc/*.jpg'] +working_dir = 'D:/RUBY_TEST_DELETE/New_loc' #Where files will be moved. The current working directory. +Dir.chdir working_dir +pic_names = Dir['D:/RUBY_TEST_DELETE/Orig_loc/*.jpg'] # First we find all of the pictures to be moved. +puts "This is a list of all the files:" +puts pic_names; puts puts 'What would you like to call this batch?' batch_name = gets.chomp puts +dups = "" #String to hold duplicate file names. 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". -new_name = -if pic_number < 10 - "#{batch_name}0#{pic_number}.jpg" -else - "#{batch_name}#{pic_number}.jpg" -end - -#GB Insert code above here. - -# 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 effectively -# 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.) - -# Now where were we? Oh, yeah... - + new_name = #number will be preceded by 0 if < 9. + if pic_number < 10 + "#{batch_name}0#{pic_number}.jpg" + else + "#{batch_name}#{pic_number}.jpg" + end + + if FileTest.exist? new_name + dups += "File '#{new_name}' exists.\n" + end + while FileTest.exist? new_name #File.exist? (pass it a filename, and it will return true or false) + new_name += '(duplicate).jpg' # or could "exit" from the prog altogether as another option. + end + +# This renames the picture, but since "name" has the full path on it, and "new_name" +# doesn't, it also moves the file to the current working directory. Since it's a *move*, this effectively +# downloads and deletes the originals. File.rename name, new_name -# Finally, we increment the counter. -pic_number = pic_number + 1 +pic_number = pic_number + 1 #If counter is not incremented the files will all write to the same name - hence all but one will be deleted! end +if dups != "" + puts; puts + puts "The following files already exist in the target directory:" + puts dups +end -puts # This is so we aren't on progress bar line. -puts 'Done, cutie!' \ No newline at end of file +puts +puts 'The rename/move is complete.' \ No newline at end of file From 0af680cf1635bbe6be37490a04b0b1079dda30df Mon Sep 17 00:00:00 2001 From: Glenn Date: Sat, 24 Sep 2016 09:51:22 +0000 Subject: [PATCH 33/36] Playlist now works on my local machine. --- .../build_your_own_playlist.rb | 24 ++++------- ch11-reading-and-writing/playlist.m3u | 42 ++++++++++++++++++- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/ch11-reading-and-writing/build_your_own_playlist.rb b/ch11-reading-and-writing/build_your_own_playlist.rb index d15ace7d9..d07680e64 100644 --- a/ch11-reading-and-writing/build_your_own_playlist.rb +++ b/ch11-reading-and-writing/build_your_own_playlist.rb @@ -1,18 +1,8 @@ -#I can't get this to work... -#ref = https://github.com/makersacademy/pre_course/issues/639 - -# using the shuffle method as defined above - -all_mp3s = (Dir["c:/**/*.mp3"]).shuffle - -#all_mp3s = Dir.entries("C:/Users/g/Desktop/Zim photos - move!/Johnny Clegg & Savuka").select {|f| !File.directory? f} - -=begin -all_mp3s = -"C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\06 Dela.mp3 -C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\07 Siyayilanda.mp3" -=end - +output_dir = 'D:/RUBY_TEST_DELETE/' +Dir.chdir output_dir #Working directory where the m3u file will be writtten to. + #If this is not specified the m3u file will be written to the directory where the program was run from. +search_dir = "D:/RUBY_TEST_DELETE/**/*.mp3" #Where the mp3 files should be searched for. +all_mp3s = (Dir[search_dir]).shuffle puts all_mp3s File.open 'playlist.m3u', 'w' do |f| @@ -20,5 +10,5 @@ f.write mp3+"\n" end end - -puts 'Done!' \ No newline at end of file +puts +puts "A randomized list of songs found in \"#{search_dir}\" has been created in \"#{output_dir}\"." \ No newline at end of file diff --git a/ch11-reading-and-writing/playlist.m3u b/ch11-reading-and-writing/playlist.m3u index bf013679f..664c1be75 100644 --- a/ch11-reading-and-writing/playlist.m3u +++ b/ch11-reading-and-writing/playlist.m3u @@ -1 +1,41 @@ -Testgb \ No newline at end of file +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/15 Touch Enough.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/05 Ibhola Lethu.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/14 These Days.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/08 I Wonder.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/09 Jane S. Piddy.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/02 Only Good for Conversation.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/07 Siyayilanda.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/08 One (Hu) 'Man One Vote.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/05 Hate Street Dialogue.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/07 Inner City Blues.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/12 I Call Your Name.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/03 Crucify Your Mind.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/11 Can't Get Away [-].mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/10 Cause.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/11 Rich Folks Hoax.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/06 Dela.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/04 Establishment Blues.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/07 To Whom It May Concern.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/12 Street Boy [-].mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/13 In My African Dream.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/13 I'll Slip Away [-].mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/02 A Most Disgusting Song.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/03 Great Heart.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/01 Orphans of the Empire.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/06 Sandrevan Lullaby - Lifestyles.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/09 The Crossing.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/10 Take My Heart Away.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/08 It Started Out So Nice.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/16 Africa (What Made You So Strong) [#].mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/04 Asimbonanga.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/01 Climb Up on My Music.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/03 I Think of You.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/02 Scatterlings of Africa.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Johnny Clegg & Savuka/In My African Dream/11 Cruel, Crazy, Beautiful World.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/05 Silver Words-.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/04 Heikki's Suburbia Bus Tour.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/12 Like Janis.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/10 Gommorah (A Nursery Rhyme).mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Coming from Reality/09 Halfway Up the Stairs.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/06 Forget It.mp3 +D:/RUBY_TEST_DELETE/Music-dups-Delete/Sixto Rodriguez/Cold Fact [CD Reissue]/01 Sugar Man.mp3 From c22099ac1eb61b1b23220f1f1d0ce1a631f12551 Mon Sep 17 00:00:00 2001 From: Glenn Date: Sat, 24 Sep 2016 10:56:23 +0000 Subject: [PATCH 34/36] Interim dev on shuffle method. It works but need to make it more random now. --- .../build_a_better_playlist.rb | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index f9b79e2ec..d01e54354 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,23 +1,27 @@ def music_shuffle filenames - # your code here + #print filenames; puts + length = filenames.length + indexes = (0...length).to_a + #print indexes; puts + indexes.shuffle! + # print indexes; puts "KKKKKKKK" + # puts indexes[1]; puts "GGGGGGGGG" + # puts filenames[indexes[1]]; puts "BBBBBBBBB" + randomized_files = [] + for count in 0...length + randomized_files << filenames[indexes[count]] + end + randomized_files #return filenames to music_shuffle. end #Copy of "build_your_own_playlist.rb" below. -#I can't get this to work -#ref = https://github.com/makersacademy/pre_course/issues/639 - -# using the shuffle method as defined above - -all_mp3s = (Dir["c://**/*.mp3"]).shuffle - -#all_mp3s = Dir.entries("C:/Users/g/Desktop/Zim photos - move!/Johnny Clegg & Savuka").select {|f| !File.directory? f} - -=begin -all_mp3s = -"C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\06 Dela.mp3 -C:\Users\g\Desktop\Zim photos - move!\Johnny Clegg & Savuka\In My African Dream\07 Siyayilanda.mp3" -=end +output_dir = 'D:/RUBY_TEST_DELETE/' +Dir.chdir output_dir #Working directory where the m3u file will be writtten to. + #If this is not specified the m3u file will be written to the directory where the program was run from. +search_dir = "D:/RUBY_TEST_DELETE/**/*.mp3" #Where the mp3 files should be searched for. +all_mp3s = (Dir[search_dir]) #.shuffle +all_mp3s = music_shuffle all_mp3s puts all_mp3s File.open 'playlist.m3u', 'w' do |f| @@ -25,6 +29,13 @@ def music_shuffle filenames f.write mp3+"\n" end end - -puts 'Done!' - +puts +puts "A randomized list of songs found in \"#{search_dir}\" has been created in \"#{output_dir}\"." + + +=begin + Instead of using your old shuffle, write a new music_shuffle method. It should take an array of filenames + (like those listed previously) and mix them up good and proper. You’ll probably need to use the split method for strings. + It returns an array of chopped-up pieces of the original string, split where you specify, like this: + awooga = 'this/is/not/a/daffodil'.split '/' +=end \ No newline at end of file From b6649a538374243cd3a859ec745cf5b321d81119 Mon Sep 17 00:00:00 2001 From: Glenn Date: Sat, 24 Sep 2016 12:07:52 +0000 Subject: [PATCH 35/36] Shuffle method completed. --- .../build_a_better_playlist.rb | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index d01e54354..b2866adc0 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,13 +1,37 @@ def music_shuffle filenames - #print filenames; puts + randomized_files = [] length = filenames.length indexes = (0...length).to_a - #print indexes; puts indexes.shuffle! - # print indexes; puts "KKKKKKKK" - # puts indexes[1]; puts "GGGGGGGGG" - # puts filenames[indexes[1]]; puts "BBBBBBBBB" - randomized_files = [] + temp1 = []; temp2 = []; temp3 = [] + indexes.each{ + loop do + item = indexes[rand(length)] + if !temp1.include? item + temp1 << item + break + end + end + } + temp1.each{ + loop do + item = temp1[rand(length)] + if !temp2.include? item + temp2 << item + break + end + end + } + temp2.each{ + loop do + item = temp2[rand(length)] + if !temp3.include? item + temp3 << item + break + end + end + } + indexes = temp3.shuffle! for count in 0...length randomized_files << filenames[indexes[count]] end @@ -19,7 +43,7 @@ def music_shuffle filenames Dir.chdir output_dir #Working directory where the m3u file will be writtten to. #If this is not specified the m3u file will be written to the directory where the program was run from. search_dir = "D:/RUBY_TEST_DELETE/**/*.mp3" #Where the mp3 files should be searched for. -all_mp3s = (Dir[search_dir]) #.shuffle +all_mp3s = (Dir[search_dir]) all_mp3s = music_shuffle all_mp3s puts all_mp3s From 1d0bdfb486f70fa70921fc01fe9dde21afac1492 Mon Sep 17 00:00:00 2001 From: Glenn Date: Mon, 26 Sep 2016 10:27:13 +0000 Subject: [PATCH 36/36] test commit to see if this appears on http://hub.makersacademy.com/students/899. --- ch11-reading-and-writing/build_a_better_playlist.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ch11-reading-and-writing/build_a_better_playlist.rb b/ch11-reading-and-writing/build_a_better_playlist.rb index b2866adc0..f52cdc598 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -10,7 +10,7 @@ def music_shuffle filenames if !temp1.include? item temp1 << item break - end + end end } temp1.each{