Created
February 5, 2015 12:25
-
-
Save pallavsharma/a06a99604ad2f1d8e2ff to your computer and use it in GitHub Desktop.
Bowling match score calculator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bowling(input) | |
scores = [] | |
result = [] | |
scores = input.split.collect { |e| e.to_i } | |
cf = 0 # current frame | |
i = 0 | |
while(cf < 10) do | |
if scores[i] == 10 # For strike | |
result[cf] = scores[i] + scores[i + 1] + (scores[i + 2] ? scores[i + 2] : 0) | |
cf += 1 | |
else | |
if (scores[i] + scores[i + 1] == 10) # For spair | |
result[cf] = scores[i] + scores[i + 1] + scores[i + 2] | |
cf += 1 | |
i += 1 | |
else | |
result[cf] = scores[i] + scores[i + 1] # normal | |
cf += 1 | |
i += 1 | |
end | |
end | |
i += 1 | |
end | |
cumulative_sum = 0 | |
puts "Scores : #{scores}" | |
puts "Result : #{result.map { |r| cumulative_sum += r}}" | |
end | |
if $PROGRAM_NAME == __FILE__ | |
while(str = gets) | |
bowling(str.chomp) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment