Skip to content

Instantly share code, notes, and snippets.

@pallavsharma
Created February 5, 2015 12:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pallavsharma/a06a99604ad2f1d8e2ff to your computer and use it in GitHub Desktop.
Save pallavsharma/a06a99604ad2f1d8e2ff to your computer and use it in GitHub Desktop.
Bowling match score calculator.
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