------ art_42468_22454907.1225063850753
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Sorry, I forgot to translate zeros into -, here's the fixxed version:
CODE START (FIX)
in
## Bowling Scores (#181)
Whether it is real or on the Wii, bowling is a fun game. (Okay, for the sake
of the quiz, let's assume it's a fun game.) But I've known folks who just
don't understand [how to score properly][1]. They can count pins knocked
down, and know that getting all ten pins in one roll is good, but they still
can't keep score.
Your task this week is to tally scores for these people. The input will be
the player's name and the number of pins felled from each roll. For example:
ruby bowling_scores.rb John 6 2 7 1 10 9 0 8 2 10 10 3 5 7 2 5 5 8
Your should tally the per-frame scores and generate output in table form,
such as:
John's final score: 140
Frame Roll Roll Score
1 6 2 8
2 7 1 16
3 X 35
4 9 - 44
5 8 / 64
6 X 87
7 X 105
8 3 5 113
9 7 2 122
10 5 / 140
* 8
Note that you should make use of typical bowling symbols: `X` for a strike,
`/` for a spare, and `-` for zero. Also, if extra balls were thrown at the
end (to supplement a strike or spare in the final frame), list those as
frame `*` like the above, but without a score.
Extra credit: Generate ascii or graphical output that looks more like the
traditional bowling score form, which can be seen on [this page][1].
[1]: http://www.bowling2u.com/trivia/game/scoring.asp
ίΕ
class Array
def sum; self.inject{|a,b| a+b}; end
end
def get_points (shot,next_shots)
return shot.sum + (
# strike
if shot.first 10
next_shots.flatten[0..1].sum
# spare
elsif shot.sum 10
next_shots.first.first
# normal
else
0
end
)
end
def symbols(score,sep)
return "X#{sep}" if score.first 10
return (score.sum 10 ? "#{score[0]}#{sep}/" :
"#{score[0]}#{sep}#{score[1]}").gsub("0","-")
end
name, score, points RGV.first, ARGV[1..-1].join(" "), []
raise "Usage: 'ruby 181_bowling_score.rb [name] [scores]'" if name.nil? or
score.to_a.empty?
score core.scan(/10|\d+\s*\d*/).collect{|e| e.split(" ").map!{|e|
e.to_i}}
score.each_with_index { |s,i| points[i] et_points(s,score[i+1..-1]) }
puts "#{name}'s final score: #{points[0..9].sum}";
puts "Frame\tRoll\tRoll\tScore\t"
score.each_with_index do |s,i|
puts "#{i+1}\t#{symbols(s,"\t")}\t#{(i<10) ? points[0..i].sum : ''}"
end
CODE END (FIX)
On Sun, Oct 26, 2008 at 11:25 PM, Sandro Paganotti <
sandro.paganotti / gmail.com> wrote:
> Here there's my attempt:
>
>
>