Answer the question
In order to leave comments, you need to log in
Is a Ruby Koans solution good enough?
Hello everyone on the Ruby path.
To consolidate my programming skills in Ruby, I decided to finish off Ruby Koans .
To explain what it is, I'll quote egoholic from his post :
… the Ruby Koans project, which is a learning environment built around Test::Unit. The bottom line is that all tests pass correctly as a result of your editing the content of the test matchers. RubyKoans can be downloaded to your machine, but especially for lazy people like me, a web interface has been implemented, which means that you don’t need to install anything, just go to the address below and fill in the fields so that there are no errors in the matchers. Project website: rubykoans.com/ , online version: koans.heroku.com/.
<pre><code class="ruby">def score(dice)
# no dice given
return 0 if dice.empty?
total = 0
# find triple match
# collect duplicates into array (not more than 3)
duplicates = dice.map do |n|
n if dice.count{|d| d == n} > 2
end.compact
# get number or nil
triple = duplicates.pop
# if there IS triple match
if triple
# triple one gives 1000
if triple == 1
total += 1000
# other values give x100
else
total += triple*100
end
# remove those numbers from the collection
# also ensure that only 3 array items can be removed
array_min_size = dice.count - 3
dice = dice.delete_if do |n|
true if (n == triple && dice.count > array_min_size)
end
end
# look for 1 or 5
dice.each do |n|
total += 50 if n == 5
total += 100 if n == 1
end
return total
end
Answer the question
In order to leave comments, you need to log in
Something like this, I didn’t quite understand whether [1, 1, 1, 1, 1, 1] could get caught.
def score(dice)
result = 0;
(1..6).each do |digit|
multiplier = 100
multiplier = 1000 if digit == 1
triple = dice.count(digit) / 3 # принять во внимание [1, 1, 1, 1, 1, 1]
result += digit * multiplier * triple
end
result += dice.count(1) % 3 * 100
result += dice.count(5) % 3 * 50
end
Still, it is very difficult to switch from PHP to Ruby. I can't get used to it myself.
It still looks like. But this will pass with time. I will duplicate here some of the recommendations traditional for Ruby.
return 0 if dice.empty?
The tests pass, although to be honest these tests are not enough to guarantee the correctness of the implementation
def score(dice)
# You need to write this method
digits = Hash.new(0)
dice.each { |n| digits[n] += 1 }
result = 0
result += 1000 if digits[1] >= 3 # 1
(digits.keys - [1]).each do |n| # 2
result += n * 100 if digits[n] >= 3
end
result += 100 * (digits[1] % 3) # 3
result += 50 * (digits[5] % 3) # 4
result
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question