How can I reduce my if statements? - ruby

Going through Chris Pine's Learn To Program and working on the number to roman numeral conversion project. The code below works, however it's pretty ugly w/ all those if (and end) statements. However, when I use elsif the program doesn't respond (appears to freeze up). Any thoughts would be helpful!
def calc input
roman_numeral = ''
while true
if input >= 1000
roman_numeral += 'M' * (input / 1000)
input = input - (1000 * (input / 1000))
if input <= 999 || input >= 500
roman_numeral += 'D' * (input / 500)
input = input - (500 * (input / 500))
if input <= 499 || input >= 100
roman_numeral += 'C' * (input / 100)
input = input - (100 * (input / 100))
if input <= 99 || input >= 50
roman_numeral += 'L' * (input / 50)
input = input - (50 * (input / 50))
if input <= 49 || input >= 10
roman_numeral += 'X' * (input / 10)
input = input - (10 * (input / 10))
if input <= 9 || input >= 5
roman_numeral += 'V' * (input / 5)
input = input - (5 * (input / 5))
if input <= 4 || input >= 1
roman_numeral += 'I' * (input / 1)
input = input - (1 * (input / 1))
puts roman_numeral
break
end
end
end
end
end
end
end
end
end
puts 'Give me a number, any number:'
input = gets.chomp.to_i
calc(input)

It's convenient to use the method Enumerable#find with an array:
ARR = [[1000,'M'], [ 500,'D'], [100,'C'], [50,'L'], [10,'X'], [5,'V'], [1,'I']]
def which(input)
ARR.find { |v,_| input >= v }
end
which(2) #=> [1, "I"]
which(7) #=> [5, "V"]
which(17) #=> [10, "X"]
which(77) #=> [50, "L"]
which(777) #=> [500, "D"]
which(7777) #=> [1000, "M"]
Assuming you are converting an integer to a roman numeral, consider making use of the method Fixnum#divmod. Suppose the integer were 2954 and you've already determined that there are two "M"'s and one "D" (so the beginning of your roman numeral string is "MMD"), and that 454 is left over. Then:
c, rem = 454.divmod(100)
#=>[4, 54]
c #=> 4
rem #=> 54
tells you there are four "C"'s with 54 left over.
Four "C"'s are written "CD" (not "CCCC"), however, so you may want to use a hash such as the following:
REP = {..., "C"=>["C", "CC", "CCC", "CD"], ...}
to convert the number of "C"'s to a roman numeral. Here you would append REP["C"][4-1] #=> "CD" to "MMD": "MMD" << "CD" #=> "MMDCD".

The answer from Cary Swoveland is an excellent way to decrease your if block nesting.
His answer tells you which numeral comes next, but not how many (as in your code). A natural way to tie it together is with a recursive function call:
class Romans
def self.calc(input, acc = "")
raise ArgumentError.new("Roman Numerals must be positve") if input < 0
raise ArgumentError.new("Roman Numerals must be integers") if ! input.is_a? Integer
return acc if input == 0
amount, numeral = which(input)
acc += numeral
input -= amount
calc(input, acc)
end
##ARR = [[1000,'M'], [ 500,'D'], [100,'C'], [50,'L'], [10,'X'], [5,'V'], [1,'I']]
def self.which(input)
##ARR.find { |v,_| input >= v }
end
end
In use:
pry(main)> (1..10).each{|i| puts "#{i}=> #{Romans.calc(i)}"}
1=> I
2=> II
3=> III
4=> IIII
5=> V
6=> VI
7=> VII
8=> VIII
9=> VIIII
10=> X
pry(main)> [Random.rand(1..100000)].each{|i| puts "#{i}=> #{Romans.calc(i)}"}
63124=> MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMCXXIIII
Be aware that ruby doesn't have TCO, and so will blow the stack with large enough numbers- but if you need the Roman numeral version of 8 million, you might want to make up some new letters.

Here's one that uses string multiplication. For example: ('M' * 3) = 'MMM'
def to_roman(number)
raise 'Must use positive numbers.' if number <= 0
roman = ''
roman << 'M' * (number / 1000)
roman << 'D' * (number % 1000 / 500)
roman << 'C' * (number % 500 / 100)
roman << 'L' * (number % 100 / 50)
roman << 'X' * (number % 50 / 10)
roman << 'V' * (number % 10 / 5)
roman << 'I' * (number % 5 / 1)
roman
end
puts to_roman(1234) # <-- test
Reference: Learn to Program 2nd by Chris Pine

Related

Codewars: "Return or rotate": why isn't my attempted solution working?

These were the instructions given on Codewars (https://www.codewars.com/kata/56b5afb4ed1f6d5fb0000991/train/ruby):
The input is a string str of digits. Cut the string into chunks (a chunk here is a substring of the initial string) of size sz (ignore the last chunk if its size is less than sz).
If a chunk represents an integer such as the sum of the cubes of its digits is divisible by 2, reverse that chunk; otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.
If
sz is <= 0 or if str is empty return ""
sz is greater (>) than the length of str it is impossible to take a chunk of size sz hence return "".
Examples:
revrot("123456987654", 6) --> "234561876549"
revrot("123456987653", 6) --> "234561356789"
revrot("66443875", 4) --> "44668753"
revrot("66443875", 8) --> "64438756"
revrot("664438769", 8) --> "67834466"
revrot("123456779", 8) --> "23456771"
revrot("", 8) --> ""
revrot("123456779", 0) --> ""
revrot("563000655734469485", 4) --> "0365065073456944"
This was my code (in Ruby):
def revrot(str, sz)
# your code
if sz > str.length || str.empty? || sz <= 0
""
else
arr = []
while str.length >= sz
arr << str.slice!(0,sz)
end
arr.map! do |chunk|
if chunk.to_i.digits.reduce(0) {|s, n| s + n**3} % 2 == 0
chunk.reverse
else
chunk.chars.rotate.join
end
end
arr.join
end
end
It passed 13/14 test and the error I got back was as follows:
STDERR/runner/frameworks/ruby/cw-2.rb:38:in `expect': Expected: "", instead got: "095131824330999134303813797692546166281332005837243199648332767146500044" (Test::Error)
from /runner/frameworks/ruby/cw-2.rb:115:in `assert_equals'
from main.rb:26:in `testing'
from main.rb:84:in `random_tests'
from main.rb:89:in `<main>'
I'm not sure what I did wrong, I have been trying to find what it could be for over an hour. Could you help me?
I will let someone else identify the problem with you code. I merely wish to show how a solution can be speeded up. (I will not include code to deal with edge cases, such as the string being empty.)
You can make use of two observations:
the cube of an integer is odd if and only if the integer is odd; and
the sum of collection of integers is odd if and only if the number of odd integers is odd.
We therefore can write
def sum_of_cube_odd?(str)
str.each_char.count { |c| c.to_i.odd? }.odd?
end
Consider groups of 4 digits in the last example, "563000655734469485".
sum_of_cube_odd? "5630" #=> false (so reverse -> "0365")
sum_of_cube_odd? "0065" #=> true (so rotate -> "0650")
sum_of_cube_odd? "5734" #=> true (so rotate -> "7345")
sum_of_cube_odd? "4694" #=> true (so rotate -> "6944")
so we are to return "0365065073456944".
Let's create another helper.
def rotate_chars_left(str)
str[1..-1] << s[0]
end
rotate_chars_left "0065" #=> "0650"
rotate_chars_left "5734" #=> "7345"
rotate_chars_left "4694" #=> "6944"
We can now write the main method.
def revrot(str, sz)
str.gsub(/.{,#{sz}}/) do |s|
if s.size < sz
''
elsif sum_of_cube_odd?(s)
rotate_chars_left(s)
else
s.reverse
end
end
end
revrot("123456987654", 6) #=> "234561876549"
revrot("123456987653", 6) #=> "234561356789"
revrot("66443875", 4) #=> "44668753"
revrot("66443875", 8) #=> "64438756"
revrot("664438769", 8) #=> "67834466"
revrot("123456779", 8) #=> "23456771"
revrot("563000655734469485", 4) #=> "0365065073456944"
It might be slightly faster to write
require 'set'
ODD_DIGITS = ['1', '3', '5', '7', '9'].to_set
#=> #<Set: {"1", "3", "5", "7", "9"}>
def sum_of_cube_odd?(str)
str.each_char.count { |c| ODD_DIGITS.include?(c) }.odd?
end

I have a issue.. Appending

I have this code:
1 #!/local/usr/bin/ruby
2
3 users = (1..255).to_a
4
5 x = " "
6 y = " "
7 z = " "
8 #a = " "
9
10 count = 1
11 users.each do |i|
12 x << i if count == 1
13 y << i if count == 2
14 z << i if count == 3
15 # if x.length == 60
16 # a << i if count == 1
17 # a << i if count == 2
18 # a << i if count == 3
19 # else
20 # end
21 if count == 3
22 count = 1
23 else
24 count += 1
25 end
26 end
27
28 puts x.length
29 puts y.length
30 puts z.length
31 #puts a.length
32
What this code does is append The numbers 1-255 into three different strings and outputs how many numbers are in each string.
IT WORKS
Example of working code:
[user#server ruby]$ ruby loadtest.rb
86
86
86
[user#server ruby]$
Now what I want it to do is have a failsafe called a as seen above, commented out, What I want is this, if each string contains 60 numbers I want it to append into the a string until there are no more numbers.
When I try to do it with the commented out section it outputs this:
[user#server ruby]$ ruby loadtest.rb
86
86
86
4
[user#server ruby]$ ruby loadtest.rb
WHY?! What am I doing wrong?
What this code does is append The numbers 1-255 into three different strings and outputs how many numbers are in each string.
After reducing the number of values being iterated for readability, here's what it's doing:
users = (1..5).to_a
x = " "
y = " "
z = " "
count = 1
users.each do |i|
x << i if count == 1 # => " \u0001", nil, nil, " \u0001\u0004", nil
y << i if count == 2 # => nil, " \u0002", nil, nil, " \u0002\u0005"
z << i if count == 3 # => nil, nil, " \u0003", nil, nil
if count == 3
count = 1
else
count += 1
end
end
x # => " \u0001\u0004"
y # => " \u0002\u0005"
z # => " \u0003"
puts x.length
puts y.length
puts z.length
# >> 3
# >> 3
# >> 2
Your code is creating binary inside the strings, not "numbers" as we normally think of them, as digits.
Moving on, you can clean up your logic using each_with_index and case/when. To make the results more readable I switched from accumulating into strings into arrays:
users = (1..5).to_a
x = []
y = []
z = []
users.each_with_index do |i, count|
case count % 3
when 0
x << i
when 1
y << i
when 2
z << i
end
end
x # => [1, 4]
y # => [2, 5]
z # => [3]
puts x.length
puts y.length
puts z.length
# >> 2
# >> 2
# >> 1
The real trick in this is the use of %, which does a modulo on the value.
... if each string contains 60 numbers I want it to append into the a string until there are no more numbers
As written, you are unconditionally appending to x,y,z even after they hit your limit.
You need to add a conditional around this code:
x << i if count == 1
y << i if count == 2
z << i if count == 3
so that it stops appending once it hits your limit.
By the looks of the else block that does nothing, I think you were headed in that direction:
if x.length == 60
a << i if count == 1
a << i if count == 2
a << i if count == 3
else
x << i if count == 1
y << i if count == 2
z << i if count == 3
end
Even that, though, won't do exactly what you want.
You'll want to check the string you are appending to to see if it has hit your limit yet.
I'd suggest refactoring to make it cleaner:
users.each do |i|
target_string = case count
when 1 then x
when 2 then y
when 3 then z
end
target_string = a if target_string.length == 60
target_string << i
if count == 3
count = 1
else
count += 1
end
end
It may be better to use an array instead of string as you are pushing numbers into those variables.
Let me propose a solution which achieves more or less what you are trying to do, but uses few Ruby tricks that may be useful in future.
x, y, z = r = Array.new(3) {[]}
a = []
iter = [0,1,2].cycle
(1..255).each do |i|
r.all? {|i| i.size == 60} ? a << i : r[iter.next] << i
end
p x.size, y.size, z.size
p a.size
Let's define our arrays. Even though I have arrays x, y, and z, they are there only because they were present in your code - I think we just need three arrays, each of which would collect numbers as they are picked from a range of numbers - between 1 to 255 - one by one. x,y,z = r uses parallel assignment technique and is equivalent to x,y,z = r[0],r[1],r[2]. Also, use of Array.new(3) {[]} helps in creating the Array of Array such that when we access r[1] it is initialized with empty array([]) by default.
x, y, z = r = Array.new(3) {[]}
a = []
In order to determine which array the next number picked from range has to be placed in, we will use an Enumerator generated from Enumerable#cycle. This enumerator is special - because it is soft of infinite in nature - and we can keep asking it to give an element by calling next, and it will cycle through the array elements of [0,1,2] - returning us 0,1,2,0,1,2,0,1,2... infinitely.
iter = [0,1,2].cycle
Next, we will iterate through the range of numbers 1..255. During each iteration, we will check whether all the 3 arrays in which we are collecting number have desired size of 60 with the help of Enumerable#all? - if so, we will append the number to array a - else we will assign it to one of the sub arrays of r based on the array index returned by iter enumerator.
(1..255).each do |i|
r.all? {|i| i.size == 60} ? a << i : r[iter.next] << i
end
Finally, we print the size of each of the array.
p x.size, y.size, z.size
#=> 60, 60, 60
p a.size
#=> 75

Ruby: Reducing Repetition

I'm a beginner and wrote a script for the following question below in ruby. I read that repetition isn't recommended and would like to reduce the repetition of if, elsif, else statements but can't seem to find a way.
Old-school Roman numerals. In the early days of Roman numer- als, the Romans didn’t bother with any of this new-fangled sub- traction “IX” nonsense. No sir, it was straight addition, biggest to littlest—so 9 was written “VIIII,” and so on. Write a method that when passed an integer between 1 and 3000 (or so) returns a string containing the proper old-school Roman numeral. In other words, old_roman_numeral 4 should return 'IIII'. Make sure to test your method on a bunch of different numbers. Hint: Use the inte- ger division and modulus methods on page 37.
For reference, these are the values of the letters used:
I =1 V=5 X=10 L=50 C=100 D=500 M=1000
Here is my script...
puts "What is your number?"
n = gets.chomp
num = n.to_i
number = ""
l = n.length
i = 0
while true
if num > 3000
puts "Enter another number."
elsif l == 0
break
else
if l == 4
number += "M" * n[i].to_i
l -= 1
i += 1
elsif l == 3
if 1 <= n[i].to_i && n[i].to_i <= 4
number += "C" * n[i].to_i
elsif n[i].to_i == 5
number += "D"
elsif 6 <= n[i].to_i && n[i].to_i <= 9
number += "D" + "C" * (n[i].to_i - 5)
end
l -= 1
i += 1
elsif l == 2
if 1 <= n[i].to_i && n[i].to_i <= 4
number += "X" * n[i].to_i
elsif n[i].to_i == 5
number += "L"
elsif 6 <= n[i].to_i && n[i].to_i <= 9
number += "L" + "X" * (n[i].to_i - 5)
end
l -= 1
i += 1
else
if 1 <= n[i].to_i && n[i].to_i <= 4
number += "I" * n[i].to_i
elsif n[i].to_i == 5
number += "V"
elsif 6 <= n[i].to_i && n[i].to_i <= 9
number += "V" + "I" * (n[i].to_i - 5)
end
l -= 1
i += 1
end
end
end
This doesn't use integer division or modulus, but it might be instructive.
puts "What is your number?"
input = gets.to_i
numerals = {
1000 => "M",
500 => "D",
100 => "C",
50 => "L",
10 => "X",
5 => "V",
1 => "I"
}
digits = []
numerals.each do |n, digit|
while input >= n
digits << digit
input = input - n
end
end
puts digits.join
Another way, that builds a string, as #sawa suggested, rather than constructing an array and then using join:
numerals = {
1000 => "M",
500 => "D",
100 => "C",
50 => "L",
10 => "X",
5 => "V",
1 => "I"
}
input = 9658
numerals.each_with_object('') do |(n, digit),str|
nbr, input = input.divmod(n)
str << digit*nbr
end
#=> "MMMMMMMMMDCLVIII"

Why doesn't my conversion code for roman numbers ('mcmxcix' for e.g) to real numbers work?

I want to convert Roman numerals, such as "mcmxcix", to arabic integers like "1999".
My code looks like:
#~ I = 1 V = 5 X = 10 L = 50
#~ C = 100 D = 500 M = 1000
def roman_to_integer roman
len = roman.length
x = 1
while x <= len
arr = Array.new
arr.push roman[x]
x += 1
end
num = 0
arr.each do |i|
if i == 'I'
num += 1
elsif i == 'V'
num += 5
elsif i == 'X'
num += 10
elsif i == 'L'
num += 50
elsif i == 'C'
num += 100
elsif i == 'D'
num += 500
elsif i == 'M'
num += 1000
end
end
num
end
puts(roman_to_integer('MCMXCIX'))
The output is 0, but I don't understand why?
Ruby doesn't have a post-increment operator. When it sees ++ it interprets that as one infix + followed by one prefix (unary) +. Since it expects an operand to follow after that, but instead finds the keyword end, you get a syntax error.
You need to replace x++ with x += 1.
Furthermore note that x isn't actually in scope inside the roman_to_integer method (which isn't a syntax error, but nevertheless wrong).
Additionally you'll have to replace all your ifs except the first with elsifs. The way you wrote it all the ifs are nested, which means that a) you don't have enough ends and b) the code doesn't have the semantics you want.
You are missing a closing parentheses so
puts(roman_to_integer('mcmxcix')
should be
puts roman_to_integer('mcmxcix')
or
puts(roman_to_integer('mcmxcix'))
The arr keeps getting annihilated in your while loop, and it is not in the scope outside of the loop. Move the following line above the while statement:
arr = Array.new

Ruby Greed Koan - How can I improve my if/then soup?

I'm working my way through the Ruby Koans in order to try and learn Ruby, and so far, so good. I've gotten to the greed koan, which at the time of this writing is 183. I've got a working solution, but I feel like I've cobbled together just a bunch of if/then logic and that I'm not embracing Ruby patterns.
In the following code, are there ways you would point me to more fully embracing Ruby patterns? (My code is wrapped in "MY CODE [BEGINS|ENDS] HERE" comments.
# Greed is a dice game where you roll up to five dice to accumulate
# points. The following "score" function will be used calculate the
# score of a single roll of the dice.
#
# A greed roll is scored as follows:
#
# * A set of three ones is 1000 points
#
# * A set of three numbers (other than ones) is worth 100 times the
# number. (e.g. three fives is 500 points).
#
# * A one (that is not part of a set of three) is worth 100 points.
#
# * A five (that is not part of a set of three) is worth 50 points.
#
# * Everything else is worth 0 points.
#
#
# Examples:
#
# score([1,1,1,5,1]) => 1150 points
# score([2,3,4,6,2]) => 0 points
# score([3,4,5,3,3]) => 350 points
# score([1,5,1,2,4]) => 250 points
#
# More scoring examples are given in the tests below:
#
# Your goal is to write the score method.
# MY CODE BEGINS HERE
def score(dice)
# set up basic vars to handle total points and count of each number
total = 0
count = [0, 0, 0, 0, 0, 0]
# for each die, make sure we've counted how many occurrencess there are
dice.each do |die|
count[ die - 1 ] += 1
end
# iterate over each, and handle points for singles and triples
count.each_with_index do |count, index|
if count == 3
total = doTriples( index + 1, total )
elsif count < 3
total = doSingles( index + 1, count, total )
elsif count > 3
total = doTriples( index + 1, total )
total = doSingles( index + 1, count % 3, total )
end
end
# return the new point total
total
end
def doTriples( number, total )
if number == 1
total += 1000
else
total += ( number ) * 100
end
total
end
def doSingles( number, count, total )
if number == 1
total += ( 100 * count )
elsif number == 5
total += ( 50 * count )
end
total
end
# MY CODE ENDS HERE
class AboutScoringProject < EdgeCase::Koan
def test_score_of_an_empty_list_is_zero
assert_equal 0, score([])
end
def test_score_of_a_single_roll_of_5_is_50
assert_equal 50, score([5])
end
def test_score_of_a_single_roll_of_1_is_100
assert_equal 100, score([1])
end
def test_score_of_multiple_1s_and_5s_is_the_sum_of_individual_scores
assert_equal 300, score([1,5,5,1])
end
def test_score_of_single_2s_3s_4s_and_6s_are_zero
assert_equal 0, score([2,3,4,6])
end
def test_score_of_a_triple_1_is_1000
assert_equal 1000, score([1,1,1])
end
def test_score_of_other_triples_is_100x
assert_equal 200, score([2,2,2])
assert_equal 300, score([3,3,3])
assert_equal 400, score([4,4,4])
assert_equal 500, score([5,5,5])
assert_equal 600, score([6,6,6])
end
def test_score_of_mixed_is_sum
assert_equal 250, score([2,5,2,2,3])
assert_equal 550, score([5,5,5,5])
end
end
Thanks so much to any help you can give as I try to get my head around Ruby.
Wow! There are a lot of really cool approaches being done here. I like everybody's creativity. However, I have a pedagogical problem with all of the answers presented here. ("Pedagogy is the study of … the process of teaching." -- Wikipedia)
It is obvious from the first several koans (back in about_asserts.rb) that the Path to Enlightenment does not require any prior/outside knowledge of Ruby. It also seems fairly clear that the Path doesn't even require prior programming experience. So from an educational standpoint, this koan must be answerable using only the methods, language constructs, and programming techniques taught in earlier koans. That means:
no Enumerable#each_with_index
no Enumerable#count
no Enumerable#sort
no Hash.new(0) specifying a default value
no Numeric#abs
no Numeric#divmod
no recursion
no case when
etc
Now, I'm not saying that you are not allowed to use these things in your implementation, but the koan mustn't require using them. There must be a solution that only uses constructs introduced by prior koans.
Also, since the template was just
def score(dice)
# You need to write this method
end
it seemed implied that the solution should not define other methods or classes. That is, you should only replace the # You need to write this method line.
Here is a solution that fits my philosophical requirements:
def score (dice)
sum = 0
(1..6).each do |i|
idice = dice.select { |d| d == i }
count = idice.size
if count >= 3
sum += (i==1 ? 1000 : i*100)
end
sum += (count % 3) * 100 if i == 1
sum += (count % 3) * 50 if i == 5
end
sum
end
The methods/constructs here are introduced in the following koan files:
Enumerable#each about_iteration.rb
Enumerable#select about_iteration.rb
Array#size about_arrays.rb
a ? b : c about_control_statements.rb
% about_control_statements.rb
Related StackOverflow Questions:
Ruby Koans 182. Refactor help
https://codereview.stackexchange.com/questions/423/is-this-good-ruby-ruby-koans-greed-task
A student asked Joshu, "How can I write an algorithm to calculate the scores for a dice game?"
Joshu struck the student with his stick and said: "Use a calculator."
def score(dice)
score = [0, 100, 200, 1000, 1100, 1200][dice.count(1)]
score += [0, 50, 100, 500, 550, 600][dice.count(5)]
[2,3,4,6].each do |num|
if dice.count(num) >= 3 then score += num * 100 end
end
score
end
I went through and passed each of the tests one at a time. Not sure this is a very "ruby" solution, but I do like that it's obvious what each section is doing and that there are no excess declarations of values
def score(dice)
## score is set to 0 to start off so if no dice, no score
score = 0
## setting the 1000 1,1,1 rule
score += 1000 if (dice.count(1) / 3) == 1
## taking care of the single 5s and 1s here
score += (dice.count(5) % 3) * 50
score += (dice.count(1) % 3) * 100
## set the other triples here
[2, 3, 4, 5, 6].each do |num|
score += num * 100 if (dice.count(num) / 3 ) == 1
end
score
end
Looks OK. I might have written some things slightly differently, say:
def do_triples number, total
total + (number == 1 ? 1000 : number * 100)
end
If you want to do something that few languages other than Ruby can do, I suppose the following might be justifiable under DIE and DRY, on alternate Tuesdays, but I don't think those Ruby maxims were really intended to apply to common subexpression elimination. Anyway:
def do_triples number, total
total +
if number == 1
1000
else
number * 100
end
end
def do_triples number, total
if number == 1
1000
else
number * 100
end + total
end
Here's what I did. Looks pretty similar to a few of the older replies. I would love to find some ingenious usage of inject for this one (mikeonbike's one is niiiice).
def score(dice)
total = 0
# handle triples scores for all but '1'
(2..6).each do |num|
total += dice.count(num) / 3 * num * 100
end
# non-triple score for '5'
total += dice.count(5) % 3 * 50
# all scores for '1'
total += dice.count(1) / 3 * 1000 + dice.count(1) % 3 * 100
total
end
You can condense this down to fewer lines but the readability of the algorithm gets lost so I ended up with this:
def score(dice)
result = 0;
(1..6).each do |die|
multiplier = die == 1 ? 1000 : 100
number_of_triples = dice.count(die) / 3
result += die * multiplier * number_of_triples
end
result += 100 * (dice.count(1) % 3)
result += 50 * (dice.count(5) % 3)
end
And if you're using 1.8.6, you'll have to use backports or add the count method to Array yourself:
class Array
def count(item)
self.select { |x| x == item }.size
end
end
Here is the answer I went with after about four iterations and trying to take advantage of the Ruby constructs I'm learning doing the koans:
def score(dice)
total = 0
(1..6).each { |roll| total += apply_bonus(dice, roll)}
return total
end
def apply_bonus(dice, roll, bonus_count = 3)
bonus = 0
bonus = ((roll == 1 ? 1000 : 100) * roll) if (dice.count(roll) >= bonus_count)
bonus += 50 * (dice.count(5) % bonus_count) if (roll == 5)
bonus += 100 * (dice.count(1) % bonus_count) if (roll == 1)
return bonus
end
Yet another answer :)
def score(dice)
score = 0
for num in 1..6
occurrences = dice.count {|dice_num| dice_num == num}
score += 1000 if num == 1 and occurrences >= 3
score += 100 * (occurrences % 3) if num == 1
score += 100 * num if num != 1 and occurrences >= 3
score += 50 * (occurrences % 3) if num == 5
end
score
end
This is the simplest and most readable solution that I came up with. This also accounts for a few situations not in the tests, such as a roll of six 5's or six 1's.
def score(dice)
score = 0
(1..6).each { |d|
count = dice.find_all { |a| a == d }
score = ( d == 1 ? 1000 : 100 ) * d if count.size >= 3
score += (count.size - 3) * 50 if (count.size >= 4) && d == 5
score += (count.size - 3) * 100 if (count.size >= 4) && d == 1
score += count.size * 50 if (count.size < 3) && d == 5
score += count.size * 100 if (count.size < 3) && d == 1
}
score
end
I opted to use the size method instead of the count method as count isn't supported by all versions of Ruby and the koans had not tested count up to this test.
def score(dice)
total = 0
sets = dice.group_by{|num| num }
sets.each_pair do |num, values|
number_of_sets, number_of_singles = values.length.divmod(3)
number_of_sets.times { total += score_set(num) }
number_of_singles.times { total += score_single(num) }
end
total
end
def score_set(num)
return 1000 if num == 1
num * 100
end
def score_single(num)
return 100 if num == 1
return 50 if num == 5
0
end
This was my eventual solution after initially having a similar if/then/else mess on my first attempt.
def score(dice)
score = 0
dice.uniq.each do |roll|
score += dice.count(roll) / 3 * (roll == 1 ? 1000 : 100*roll)
score += dice.count(roll) % 3 * (roll == 1 ? 100 : (roll == 5 ? 50 : 0))
end
score
end
I'd say you have it looking very Ruby-like already. The only thing that doesn't look very Rubyish to me would be the use of camelCase method names instead of snake_case, but of course that's a personal convention and I haven't read the koans myself.
Other than that, your example wouldn't be improved much by using case/when or any other solution for that matter. Aim for anything less than 3 elseif operations, anything more than that and you'd probably want to hunt for a better solution.
You could shorten [0, 0, 0, 0, 0, 0] to [0] * 6 but aside from the camelCase #injekt mentioned it looks fine to me. I'd be quite happy to see this in a code review.
Also I suppose your doTriples and doSingles don't really need their temporary variables.
def doTriples( number, total )
if number == 1
total + 1000
else
total + ( number ) * 100 # be careful with precedence here
end
end
You may want to change
# for each die, make sure we've counted how many occurrencess there are
dice.each do |die|
count[ die - 1 ] += 1
end
into a hash, such as
count = Hash.new(0)
dice.each do |die|
count[die] += 1
end
or even
count = {} # Or Hash.new(0)
grouped_by_dots = dice.group_by {|die| die}
1.upto(6) do |dots| # Or grouped_by_dots.each do |dots, dice_with_those_dots|
dice_with_those_dots = grouped_by_dots.fetch(dots) {[]}
count_of_that_dots = dice_with_those_dots.length
count[dots] = count_of_that_dots
end
That way you don't have to have index + 1 littered throughout your code.
It'd be nice if Ruby had a count_by method built in.
My 2 cents. Having new methods for singles/doubles seems like a roundabout way of doing something very simple.
def score(dice)
#fill initial throws
thrown = Hash.new(0)
dice.each do |die|
thrown[die]+=1
end
#calculate score
score = 0
faces.each do |face,amount|
if amount >= 3
amount -= 3
score += (face == 1 ? 1000 : face * 100)
end
score += (100 * amount) if (face == 1)
score += (50 * amount) if (face == 5)
end
score
end
Well,
Here's my solution:
def score(dice)
total = 0
#Iterate through 1-6, and add triples to total if found
(1..6).each { |roll| total += (roll == 1 ? 1000 : 100 * roll) if dice.count(roll) > 2 }
#Handle Excess 1's and 5's
total += (dice.count(1) % 3) * 100
total += (dice.count(5) % 3) * 50
end
Once I found the "count" method for an array, this exercise was pretty easy.
Here is my answer. I do not know if it is good or not, but at least, it looks clear :)
RULEHASH = {
1 => [1000, 100],
2 => [100,0],
3 => [100,0],
4 => [100,0],
5 => [100,50],
6 => [100,0]
}
def score(dice)
score = 0
RULEHASH.each_pair do |i, rule|
mod = dice.count(i).divmod(3)
score += mod[0] * rule[0] * i + mod[1] * rule[1]
end
score
end
My solution is not ruby-like style. Just for fun and shortest code. We can set rules through hash p.
def score(dice)
p = Hash.new([100,0]).merge({1 => [1000,100], 5 => [100,50]})
dice.uniq.inject(0) { |sum, n| sum + dice.count(n) / 3 * n * p[n][0] + dice.count(n) % 3 * p[n][1] }
end
My answer uses a "lookup table" approach...
def score(dice)
tally = (1..6).inject(Array.new(7,0)){|a,i| a[i] = dice.count(i); a}
rubric = {1 => [0,100,200,1000,1100,1200], 5 => [0,50,100,500,550,600]}
score = rubric[1][tally[1]] + rubric[5][tally[5]]
[2,3,4,6].each do |i| score += 100 * i if dice.count(i) >= 3 end
score
end
Mine was similar to a couple of others posted here.
score = 0
[1,2,3,4,5,6].each {|d|
rolls = dice.count(d)
score = (d==1 ? 1000 : 100)*d if rolls >= 3
score += 100*(rolls % 3) if d == 1
score += 50*(rolls % 3) if d == 5
}
score
Me and my girlfriend were going through these rubykoans this weekend and I had quite a bit of fun golfing on this and trying many different solutions. Here is a reasonably short data-driven solution:
SCORES = [[1000, 100], [200, 0], [300, 0], [400, 0], [500, 50], [600, 0]]
def score(dice)
counts = dice.group_by(&:to_i).map { |i, j| [i-1, j.length] }
counts.inject(0) do |score, (i, count)|
sets, singles = count.divmod 3
score + sets * SCORES[i][0] + singles * SCORES[i][1]
end
end
Here is my obligatory one-liner (and perhaps FP version):
SCORES = [[1000, 100], [200, 0], [300, 0], [400, 0], [500, 50], [600, 0]]
def score(dice)
dice.group_by(&:to_i).inject(0) {|s,(i,j)| s + j.size / 3 * SCORES[i-1][0] + j.size % 3 * SCORES[i-1][1]}
end
I also went some weird routes as well:
SCORES = [[1000, 100], [200, 0], [300, 0], [400, 0], [500, 50], [600, 0]]
def score(dice)
dice.group_by(&:to_i).inject(0) do |s, (i,j)|
s + j.size.divmod(3).zip(SCORES[i-1]).map {|a,b| a*b }.reduce(:+)
end
end
All programmers should be screwing around with little problems like this...It is like performing morning stretches :)
def score(dice)
result = 0
result += 1000 * (dice.find_all{|e| e == 1}).length.divmod(3)[0]
result += 100 * (dice.find_all{|e| e == 1}).length.divmod(3)[1]
result += 50 * (dice.find_all{|e| e == 5}).length.divmod(3)[1]
(2..6).each {|value| result += value*100 * (dice.find_all{|e| e == value}).length.divmod(3)[0]}
return result
end
And what about this solution?
Thanks for the feedback!
def score(dice)
count = Hash.new(0)
dice.each do |die|
count[die] += 1
end
total = 0
count.each_pair { |die, set| total += set < 3 ? single_value(die,set) : triple_value(die,set)}
total
end
def single_value(die,set)
value = 0
value += (set * 100) if die == 1
value += (set * 50) if die == 5
value
end
def triple_value(die,set)
value = 0
diff = set - 3
value += single_value(die,diff)
value += die == 1 ? 1000 : die * 100
value
end
I used a slightly different method to others here, and (naturally) it's one that I see as preferable. It's very DRY and uses ruby methods fairly extensively to avoid manual loops and branches as much as possible. Should be relatively obvious, but essentially what is happening is we loop through each unique dice roll, and use iterative erosion of the number of occurences of that roll to add the appropriate points to an aggregate total score.
def score(dice)
score = 0 # An initial score of 0.
throw_scores = { 1 => 10, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6 }
# A hash to store the scores for each dice throw
dice.uniq.each { |throw| # for each unique dice value present in the "hand"
throw_count = (dice.select { |item| item == throw }).count
# use select to store the number of times this throw occurs
while throw_count > 0
# iteratively erode the throw count, accumulating
# points as appropriate along the way.
if throw_count >= 3
score += throw_scores[throw] * 100
throw_count -= 3
elsif throw == 1 || throw == 5
score += throw_scores[throw] * 10
throw_count -= 1
else
throw_count -= 1
end
end
}
return score
end
And another one, just for the fun of it:
def score(dice)
result = 0
dice.uniq.each { |k|
result += ((dice.count(k) / 3) * 1000 + (dice.count(k) % 3) * 100) if k == 1
result += ((dice.count(k) / 3) * 100 * k + (dice.count(k) % 3) * ( k == 5 ? 50 : 0 )) if k != 1
}
result
end
Here's my opinion. All other solutions here try to be clever. There's a place for learning clever tricks, but it's even more important to learn to write clear and maintainable code. The main problem I see with all of these solutions is that it's very difficult to discern the scoring rules from the code. Can you read your solution and make sure that it's correct in your head? Then imagine someone asks you to add a new scoring rule, or remove one. Can you quickly point to the place where the rule must be added or removed?
Here's my solution. I'm sure it can be improved, but look at the shape of the "score" function. This is the sort of code that I would not mind to maintain.
class Array
def occurrences_of(match)
self.select{ |number| match == number }.size
end
def delete_one(match)
for i in (0..size)
if match == self[i]
self.delete_at(i)
return
end
end
end
end
def single_die_rule(match, score, dice)
dice.occurrences_of(match) * score
end
def triple_rule(match, score, dice)
return 0 if dice.occurrences_of(match) < 3
3.times { dice.delete_one match }
score
end
def score(dice)
triple_rule(1, 1000, dice) +
triple_rule(2, 200, dice) +
triple_rule(3, 300, dice) +
triple_rule(4, 400, dice) +
triple_rule(5, 500, dice) +
triple_rule(6, 600, dice) +
single_die_rule(1, 100, dice) +
single_die_rule(5, 50, dice)
end
I'm gonna have to go with:
def score(dice)
# some checks
raise ArgumentError, "input not array" unless dice.is_a?(Array)
raise ArgumentError, "invalid array size" unless dice.size <= 5
raise ArgumentError, "invalid dice result" if dice.any? { |x| x<1 || x>6 }
# setup (output var, throws as hash)
out = 0
freqs = dice.inject(Hash.new(0)) { |m,x| m[x] += 1; m }
# 3-sets
1.upto(6) { |i| out += freqs[i]/3 * (i == 1 ? 10 : i) * 100 }
# one not part of 3-set
out += (freqs[1] % 3) * 100
# five not part of 3-set
out += (freqs[5] % 3) * 50
out
end
Because most of the solutions presented so far lack basic checks. And some of them are fairly unreadable (in my book) and not very idiomatic.
Granted, the 3-set condition could be made more readable by splitting into two clauses:
# 3-sets of ones
out += freqs[1]/3 * 1_000
# 3-sets of others
2.upto(6) { |i| out += freqs[i]/3 * i * 100 }
but that's IMO mostly about personal preference.
Coming from Perl, my instinct is to use a hash:
def score(dice)
# You need to write this method
score = 0
count = Hash.new(0)
for die in dice
count[die] += 1
is_triple = (count[die] % 3 == 0)
if die == 1 then
score += is_triple ? 800 : 100
elsif die == 5 then
score += is_triple ? 400 : 50
elsif is_triple
score += 100 * die
end
end
return score
end
This has the advantage that it makes a single pass over dice. I could probably have used an Array in place of the Hash.
I grouped the dice by face, then looped over these groups, first scoring the threes, then individual dice. This is how I would score the game were I playing IRL
def score(dice)
points = 0
dice.group_by {|face| face}.each do |face,group|
while group.size >= 3
if face == 1
# A set of three ones is 1000 points
points += 1000
else
# A set of three numbers (other than ones) is worth 100 times the number.
points += 100 * face
end
group.pop(3)
end
group.each do |x|
# A one (that is not part of a set of three) is worth 100 points.
points += 100 if x==1
# A five (that is not part of a set of three) is worth 50 points.
points += 50 if x==5
end
end
return points
end
That's how I roll
Late to the party, but wanted to take a shot at answering only using knowledge introduced thus far in the Koans. Specifically, I don't use Enumerable#count like most others have.
This seems very straightforward to me, but if anyone happens along, I'd be happy to hear about an optimizations you may have.
And what can I say? I like taking advantage of array indexing.
def score(dice)
return 0 if dice.empty? # Immediately recognize an empty roll
# Create an array to hold the scores for each die face
totals = []
7.times { totals << 0 }
# Handle each roll and calculate new score
dice.each do |roll|
if roll == 5
# If we have seen two 5s thus far, make the score 500 for 5s, otherwise add 50
totals[roll] == 100 ? totals[roll] = 500 : totals[roll] += 50
elsif roll == 1
# If we have seen two 1s thus far, make the score 1000 for 5s, otherwise add 100
totals[roll] == 200 ? totals[roll] = 1000 : totals[roll] += 100
else
# If we see any other number three times, score is the number times 100
totals[roll] == 2 ? totals[roll] = roll * 100 : totals[roll] += 1
end
end
# Count up the scores for each die face; if score is less than 50, then it's just zero
return totals.inject(0) { |sum, points| points >= 50 ? sum += points : sum }
end

Resources