Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. def bowling(input)
  2. scores = []
  3. result = []
  4. scores = input.split.collect { |e| e.to_i }
  5. cf = 0 # current frame
  6. i = 0
  7. while(cf < 10) do
  8. if scores[i] == 10 # For strike
  9.  
  10. result[cf] = scores[i] + scores[i + 1] + (scores[i + 2] ? scores[i + 2] : 0)
  11. cf += 1
  12. else
  13. if (scores[i] + scores[i + 1] == 10) # For spair
  14. result[cf] = scores[i] + scores[i + 1] + scores[i + 2]
  15. cf += 1
  16. i += 1
  17. else
  18. result[cf] = scores[i] + scores[i + 1] # normal
  19. cf += 1
  20. i += 1
  21. end
  22. end
  23. i += 1
  24. end
  25. cumulative_sum = 0
  26. puts "Scores : #{scores}"
  27. puts "Result : #{result.map { |r| cumulative_sum += r}}"
  28. end
  29.  
  30. if $PROGRAM_NAME == __FILE__
  31. while(str = gets)
  32. bowling(str.chomp)
  33. end
  34. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement