From 98e2438c9d9be978e2da9a7eef217a287c693841 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Tue, 13 Sep 2016 15:48:11 +0100 Subject: [PATCH 1/5] Chp 9 Ask exercise --- ch09-writing-your-own-methods/ask.rb | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..99b5c8c7a 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,18 @@ def ask question - # your code here -end \ No newline at end of file + while true + puts question + reply = gets.chomp.downcase + + return true if reply == 'yes' + return false if reply == 'no' + + puts 'Please answer "yes" or "no".' + end + answer +end + +ask 'Do you wanna dance?' +ask 'Do you wanna sleep?' +ask 'Do you wanna eat?' +likes_it = ask 'Do you wanna eat tacos?' +puts likes_it From 89409a967eaa96a2dc000a99b19d7a580a1487d0 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Thu, 15 Sep 2016 12:43:43 +0100 Subject: [PATCH 2/5] Chp Old School Roman Numerals exercise --- .../old_school_roman_numerals.rb | 17 +++++++++++++++-- 1 file changed, 15 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..12a58da2d 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,16 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + roman = '' + + roman = roman + 'M' * (num / 1000) + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + roman +end + +puts old_roman_numeral 2457 +puts old_roman_numeral 13 +puts old_roman_numeral 1952 From 224756f3562a5594623844478b93ab34b627f0a2 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Fri, 16 Sep 2016 10:55:42 +0100 Subject: [PATCH 3/5] Roman numerals solution chp 9 --- .../roman_numerals.rb | 40 ++++++++++++++++++- 1 file changed, 38 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..ca4b2bdc6 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,39 @@ def roman_numeral num - # your code here -end \ No newline at end of file + thousands = (num / 1000) + hundreds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10) + + roman = 'M' * thousands + + if hundreds == 9 + roman = roman + 'CM' + elsif hundreds == 4 + roman = roman + 'CD' + else + roman = roman + 'D' * (num % 1000 / 500) + roman = roman + 'C' * (num % 500 / 100) + end + + if tens == 9 + roman = roman + 'XC' + elsif tens == 4 + roman = romann + 'XL' + else + roman = roman + 'L' * (num % 100 / 50) + roman = roman + 'X' * (num % 50 / 10) + end + + if ones == 9 + roman = roman + 'IX' + elsif ones == 4 + roman = roman + 'IV' + else + roman = roman + 'V' * (num % 10 / 5) + roman = roman + 'I' * (num % 5 / 1) + end + roman +end + +puts(roman_numeral(1999)) +puts (roman_numeral(99)) From 6c28cd35cdaf0cb5ff14da8c46dfc4ea27c5bf53 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Sun, 18 Sep 2016 12:13:54 +0100 Subject: [PATCH 4/5] Chp 10 exercises --- ch10-nothing-new/dictionary_sort.rb | 28 ++++- ch10-nothing-new/english_number.rb | 101 +++++++++++++++- .../ninety_nine_bottles_of_beer.rb | 109 +++++++++++++++++- ch10-nothing-new/shuffle.rb | 28 ++++- 4 files changed, 260 insertions(+), 6 deletions(-) diff --git a/ch10-nothing-new/dictionary_sort.rb b/ch10-nothing-new/dictionary_sort.rb index c9893d0fd..2ca994384 100644 --- a/ch10-nothing-new/dictionary_sort.rb +++ b/ch10-nothing-new/dictionary_sort.rb @@ -1,3 +1,27 @@ def dictionary_sort arr - # your code here -end \ No newline at end of file + rec_dict_sort arr, [] + end + + def rec_dict_sort unsorted, sorted + if unsorted.length <= 0 + return sorted + end + + smallest = unsorted.pop + still_unsorted = [] + + unsorted.each do |tested_object| + if tested_object.downcase < smallest.downcase + still_unsorted.push smallest + smallest = tested_object + else + still_unsorted.push tested_object + end + end + + sorted.push smallest + + rec_dict_sort still_unsorted, sorted + end + +puts(dictionary_sort([' space' ,' time' ,' continuum' ,' countdown' ,' space' ,' blastoff' ])) diff --git a/ch10-nothing-new/english_number.rb b/ch10-nothing-new/english_number.rb index c0129bc4e..7a95d64d9 100644 --- a/ch10-nothing-new/english_number.rb +++ b/ch10-nothing-new/english_number.rb @@ -1,3 +1,102 @@ def english_number number - # your code here + if number < 0 + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end + + num_string = '' + + ones_place = ['one', 'two', 'three', + 'four', 'five', 'six', + 'seven', 'eight', 'nine'] + + tens_place = ['ten', 'twenty', 'thirty', + 'forty', 'fifty', 'sixty', + 'seventy', 'eighty', 'ninety'] + + + teenagers = ['eleven', 'twelve', 'thirteen', + 'fourteen', 'fifteen', 'sixteen', + 'seventeen', 'eighteen', 'nineteen'] + + zillions = [['hundred', 2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + + left = number + + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base + left = left - write*zil_base + + if write > 0 + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + num_string = num_string + ' ' + end + end + end + + write = left/10 + left = left - write*10 + + if write > 0 + if ((write == 1) and (left > 0)) + + num_string = num_string + teenagers[left-1] + + left = 0 + else + num_string = num_string + tens_place[write-1] + + end + + if left > 0 + num_string = num_string + '-' + end + end + + write = left + left = 0 + + if write > 0 + num_string = num_string + ones_place[write-1] + end + num_string end + +puts english_number( 0) +puts english_number( 8) +puts english_number( 13) +puts english_number( 23) +puts english_number(2001) +puts english_number(99999) +puts english_number(2000000000500) +puts english_number(109236225102938986129834709285360238475667874561034) diff --git a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb index 801de24bd..4c49979be 100644 --- a/ch10-nothing-new/ninety_nine_bottles_of_beer.rb +++ b/ch10-nothing-new/ninety_nine_bottles_of_beer.rb @@ -1 +1,108 @@ -# your code here \ No newline at end of file +def english_number number + if number < 0 + return 'Please enter a number that isn\'t negative.' + end + if number == 0 + return 'zero' + end + + num_string = '' + + ones_place = ['one', 'two', 'three', + 'four', 'five', 'six', + 'seven', 'eight', 'nine'] + + tens_place = ['ten', 'twenty', 'thirty', + 'forty', 'fifty', 'sixty', + 'seventy', 'eighty', 'ninety'] + + + teenagers = ['eleven', 'twelve', 'thirteen', + 'fourteen', 'fifteen', 'sixteen', + 'seventeen', 'eighteen', 'nineteen'] + + zillions = [['hundred', 2], + ['thousand', 3], + ['million', 6], + ['billion', 9], + ['trillion', 12], + ['quadrillion', 15], + ['quintillion', 18], + ['sextillion', 21], + ['septillion', 24], + ['octillion', 27], + ['nonillion', 30], + ['decillion', 33], + ['undecillion', 36], + ['duodecillion', 39], + ['tredecillion', 42], + ['quattuordecillion', 45], + ['quindecillion', 48], + ['sexdecillion', 51], + ['septendecillion', 54], + ['octodecillion', 57], + ['novemdecillion', 60], + ['vigintillion', 63], + ['googol', 100]] + + left = number + + while zillions.length > 0 + zil_pair = zillions.pop + zil_name = zil_pair[0] + zil_base = 10 ** zil_pair[1] + write = left/zil_base + left = left - write*zil_base + + if write > 0 + prefix = english_number write + num_string = num_string + prefix + ' ' + zil_name + + if left > 0 + num_string = num_string + ' ' + end + end + end + + write = left/10 + left = left - write*10 + + if write > 0 + if ((write == 1) and (left > 0)) + + num_string = num_string + teenagers[left-1] + + left = 0 + else + num_string = num_string + tens_place[write-1] + + end + + if left > 0 + num_string = num_string + '-' + end + end + + write = left + left = 0 + + if write > 0 + num_string = num_string + ones_place[write-1] + end + num_string +end + +num_at_start = 99 +num_now = num_at_start +while num_now > 2 + puts english_number(num_now).capitalize + ' bottles of beer on the wall, ' + + english_number(num_now) + ' bottles of beer!' + num_now = num_now - 1 + puts 'Take one down, pass it around, ' + + english_number(num_now) + ' bottles of beer on the wall!' +end + +puts "Two bottles of beer on the wall, two bottles of beer!" +puts "Take one down, pass it around, one bottle of beer on the wall!" +puts "One bottle of beer on the wall, one bottle of beer!" +puts "Take one down, pass it around, no more bottles of beer on the wall!" diff --git a/ch10-nothing-new/shuffle.rb b/ch10-nothing-new/shuffle.rb index a486ad94c..a39c8a104 100644 --- a/ch10-nothing-new/shuffle.rb +++ b/ch10-nothing-new/shuffle.rb @@ -1,3 +1,27 @@ def shuffle arr - # your code here -end \ No newline at end of file + shuf = [] + + while arr.length > 0 + rand_index = rand(arr.length) + + curr_index = 0 + new_arr = [] + + arr.each do |item| + if curr_index == rand_index + shuf.push item + else + new_arr.push item + end + + curr_index = curr_index + 1 + end + + + arr = new_arr + end + + shuf +end + +puts (shuffle ([5,6,7,8,9,10,11,12])) From 404d6318c3e0c9a7037caf08991667d207074167 Mon Sep 17 00:00:00 2001 From: Caitlin Date: Mon, 19 Sep 2016 11:57:24 +0100 Subject: [PATCH 5/5] chapter 11 exercise build a playlist --- .../build_a_better_playlist.rb | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 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 3b31bd241..018f01f27 100644 --- a/ch11-reading-and-writing/build_a_better_playlist.rb +++ b/ch11-reading-and-writing/build_a_better_playlist.rb @@ -1,3 +1,34 @@ def music_shuffle filenames - # your code here + filenames = filenames.sort + len = filenames.length + + 2.times do + l_idx = 0 + r_idx = len/2 + shuf = [] + + while shuf.length < len + if shuf.length%2 == 0 + shuf.push(filenames[r_idx]) + r_idx = r_idx + 1 + else + shuf.push(filenames[l_idx]) + l_idx = l_idx + 1 + end + end + + filenames = shuf + end + arr = [] + cut = rand(len) + idx = 0 + + while idx < len + arr.push(filenames[(idx+cut)%len]) + idx = idx + 1 + end + + arr end +songs = ['Sorry', 'Hold Up', 'Freedom','6 inch', 'Daddy Lessons', 'Don\'t Hurt Yourself'] +puts(music_shuffle(songs))