From 977d274a14616d88bc6dedc125c81ce51523bb80 Mon Sep 17 00:00:00 2001 From: Angela Wolff Date: Thu, 5 Jul 2018 13:17:46 +0100 Subject: [PATCH 1/4] Ch 9 Q1 Answer --- ch09-writing-your-own-methods/ask.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/ch09-writing-your-own-methods/ask.rb b/ch09-writing-your-own-methods/ask.rb index 01716eb35..c543d30d0 100644 --- a/ch09-writing-your-own-methods/ask.rb +++ b/ch09-writing-your-own-methods/ask.rb @@ -1,3 +1,9 @@ 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 'Answer yes or no' + end +end From 21fa8d764c666121c2a7dcb76fad9df59420bcde Mon Sep 17 00:00:00 2001 From: Angela Wolff Date: Thu, 5 Jul 2018 15:52:38 +0100 Subject: [PATCH 2/4] Ch 9 Q2 Solution - Old Roman Numerals --- .../old_school_roman_numerals.rb | 14 ++++++++++++-- 1 file changed, 12 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..bc9ea8390 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,13 @@ def old_roman_numeral num - # your code here -end \ No newline at end of file + + roman_array = [] + roman_array << 'M' * (num / 1000) + roman_array << 'D' * (num % 1000 / 500) + roman_array << 'C' * (num % 500 / 100) + roman_array << 'L' * (num % 100 / 50) + roman_array << 'X' * (num % 50 / 10) + roman_array << 'V' * (num % 10 / 5) + roman_array << 'I' * (num % 5 / 1) + return roman_array.join + +end From 486f58a605badb7247fb6551e1efba08607c3a78 Mon Sep 17 00:00:00 2001 From: Angela Wolff Date: Thu, 5 Jul 2018 17:02:56 +0100 Subject: [PATCH 3/4] Ch9 Q3 Roman Numerals - Clunky Solution --- .../roman_numerals.rb | 45 ++++++++++++++++++- 1 file changed, 43 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..75970b8a4 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,3 +1,44 @@ def roman_numeral num - # your code here -end \ No newline at end of file + new_roman_array = [] + thousands = (num / 1000) + hundreds = (num % 1000 / 100) + tens = (num % 100 / 10) + ones = (num % 10 / 1) + + new_roman_array << 'M' * thousands + + if hundreds == 9 + new_roman_array << 'CM' + elsif hundreds == 5 + new_roman_array << 'D' + elsif hundreds == 4 + new_roman_array << 'CD' + else + new_roman_array << 'D' * (num % 1000 / 500) + new_roman_array << 'C' * (num % 500 / 100) + end + + if tens == 9 + new_roman_array << 'XC' + elsif tens == 5 + new_roman_array << 'L' + elsif tens == 4 + new_roman_array << 'XL' + else + new_roman_array << 'L' * (num % 100 / 50) + new_roman_array << 'X' * (num % 50 / 10) + end + + if ones == 9 + new_roman_array << 'IX' + elsif ones == 5 + new_roman_array << 'V' + elsif ones == 4 + new_roman_array << 'IV' + else + new_roman_array << 'V' * (num % 10 / 5) + new_roman_array << 'I' * (num % 5 / 1) + end + + new_roman_array.join +end From 57a39bb4323720639f05baf260cee213581a4ab6 Mon Sep 17 00:00:00 2001 From: Angela Wolff Date: Thu, 5 Jul 2018 17:10:58 +0100 Subject: [PATCH 4/4] Ch9 Q3 Roman Numerals - Neat Solution --- .../roman_numerals.rb | 60 +++++++------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/ch09-writing-your-own-methods/roman_numerals.rb b/ch09-writing-your-own-methods/roman_numerals.rb index 75970b8a4..14bd7fbd8 100644 --- a/ch09-writing-your-own-methods/roman_numerals.rb +++ b/ch09-writing-your-own-methods/roman_numerals.rb @@ -1,44 +1,26 @@ def roman_numeral num - new_roman_array = [] - thousands = (num / 1000) - hundreds = (num % 1000 / 100) - tens = (num % 100 / 10) - ones = (num % 10 / 1) + values = [ + [1000, 'M'], + [900, 'CM'], + [500, 'D'], + [400, 'CD'], + [100, 'C'], + [50, 'L'], + [40, 'XL'], + [10, 'X'], + [9, 'IX'], + [5, 'V'], + [4, 'IV'], + [1, 'I'] + ] + new_roman_array = "" - new_roman_array << 'M' * thousands - - if hundreds == 9 - new_roman_array << 'CM' - elsif hundreds == 5 - new_roman_array << 'D' - elsif hundreds == 4 - new_roman_array << 'CD' - else - new_roman_array << 'D' * (num % 1000 / 500) - new_roman_array << 'C' * (num % 500 / 100) - end - - if tens == 9 - new_roman_array << 'XC' - elsif tens == 5 - new_roman_array << 'L' - elsif tens == 4 - new_roman_array << 'XL' - else - new_roman_array << 'L' * (num % 100 / 50) - new_roman_array << 'X' * (num % 50 / 10) - end - - if ones == 9 - new_roman_array << 'IX' - elsif ones == 5 - new_roman_array << 'V' - elsif ones == 4 - new_roman_array << 'IV' - else - new_roman_array << 'V' * (num % 10 / 5) - new_roman_array << 'I' * (num % 5 / 1) + values.each do |number, roman| + while num >= number + new_roman_array << roman + num -= number + end end - new_roman_array.join + new_roman_array end