r/dailyprogrammer • u/Garth5689 • Jan 24 '18
[2018-01-24] Challenge #348 [Intermediate] Bowling Frames Display
Description
Today's challenge will be a variation on a popular introductory programming task, scoring a game of bowling. However, in this challenge, we won't even actually have to calculate the score. Today's challenge is to produce the display for the individual frames, given a list of the number of pins knocked down on each frame.
The basic rules are as follows:
- The game of bowling consists of 10 frames, where a player gets 2 attempts to knock down 10 pins.
- If the player knocks down all 10 pins on the first roll, that should be displayed as
X, and the next number will be the first roll of the next frame. - If the player doesn't knock down any pins, that should be displayed as
- - If the player gets a spare (knocks down the remaining pins on the second roll of the frame, that should be displayed as
/
If you want more details about the rules, see: Challenge #235 [Intermediate] Scoring a Bowling Game
Input Description
You will be given a list of integers that represent the number of pins knocked down on each roll. Not that this list is not a fixed size, as bowling a perfect game requires only 12 rolls, while most games would use more rolls.
Example:
6 4 5 3 10 10 8 1 8 0 10 6 3 7 3 5 3
Output Description
Your program should output the bowling frames including strikes and spares. The total score is not necessary.
Example:
6/ 53 X X 81 8- X 63 7/ 53
Challenge Inputs
9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0 9 0
10 10 10 10 10 10 10 10 10 10 10 10
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
10 3 7 6 1 10 10 10 2 8 9 0 7 3 10 10 10
9 0 3 7 6 1 3 7 8 1 5 5 0 10 8 0 7 3 8 2 8
Challenge Outputs
9- 9- 9- 9- 9- 9- 9- 9- 9- 9-
X X X X X X X X X XXX
5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/5
X 3/ 61 X X X 2/ 9- 7/ XXX
9- 3/ 61 3/ 81 5/ -/ 8- 7/ 8/8
8
u/OldNewbee Jan 24 '18 edited Jan 24 '18
I see that many of the solvers fail to notice that, in the sample output, the columns are neatly aligned -- every frame in every game occupies exactly three characters (including trailing blanks). Some of our submitted outputs don't look like that.
(I must admit, my solution had the same failing, and I went back and edited it when I realized my mistake.)
2
u/OldNewbee Jan 24 '18 edited Jan 24 '18
It looks like that "54" is a typo... also, why in the last game is "0 10" scored as "0/" rather than "-/"?
PYTHON 3.6
Output:
4
u/henrikenggaard Jan 24 '18
Neat! Albeit not "programming" I realized this could be easily done using a Mealy-type finite state machine:
Image
4
u/chunes 1 2 Jan 25 '18
Factor
Output (including example):
4
u/zookeeper_zeke Jan 25 '18 edited Jan 25 '18
Ah, this brings back memories of my Saturday morning bowling league at Frontier Lanes in New York state back when I was a kid. I'll never forget bowling my personal best of 196. We used pencil and paper to score, computerized scoring had yet to be installed :-)
Funny, I went to see if I could provide a link to their homepage and they don't have one aside from a Facecrook account. No wonder we were scoring with pen and paper!
Here's the solution in C using table lookup, single space between each frame:
1
u/[deleted] Jan 24 '18
Sorry if this is a stupid question, as I have bowled maybe once in my entire life. Why does
5 3translate to54? Or is that a typo?3
u/FrankRuben27 0 1 Jan 25 '18
Again in Ada. The challenge is probably more on the easy side if the solution takes << 100 lines even in Ada. I still like both ;)
2
u/skeeto -9 8 Jan 24 '18
C. Any time I write code to handle bowling scores I'm never satisfied with the result. Scoring that last frame is a mess of exceptions.
2
u/DouglasMeyer Jan 25 '18
Spreadsheet: try it out, I think I even got the formatting correct. This one was a pain 😑.
2
u/supersonicsonarradar Jan 25 '18
Rust
2
u/allywilson Jan 25 '18
Powershell
The '10' in the last input really threw this off for me, I had to end up manipulating the input to get that one to work :-(
I spent far too long on this.
2
u/DouglasMeyer Jan 26 '18
I may have overdone it: custom state machine language
In the end, the code for this problem looks like (excuse the prefix notation):
1
u/sklpo1234 Jan 24 '18 edited Jan 25 '18
This is one of my first participations here and feedback would be greatly appreciated.
Python 3.6:
1
u/CraftersLP Jan 24 '18
Tried adding a few comments so it's easier to understand, although it shouldn't be too complicated
PHP
1
u/tomekanco Jan 24 '18 edited Jan 24 '18
Python
1
u/vault_dweller_707 Jan 24 '18
Python 3
Feedback very welcome.
Code:
Output:
9- 9- 9- 9- 9- 9- 9- 9- 9- 9-
X X X X X X X X X XXX
5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/ 5/5
X 3/ 61 X X X 2/ 9- 7/ XXX
9- 3/ 61 3/ 81 5/ -/ 8- 7/ 8/8
1
u/zqvt Jan 24 '18
Clojure
1
u/LegendK95 Jan 24 '18 edited Jan 24 '18
Haskell solution
1
u/octolanceae Jan 24 '18
Python3.6
Output:
1
u/raderberg Jan 24 '18
pretty ugly clojure:
1
u/zatoichi49 Jan 25 '18 edited Jan 25 '18
Method:
Create two lists, one for the first nine games and another for the tenth. Check the attempts for each frame, adding '/' for a spare and remembering to skip to the next frame if attempt one is a strike. Repeat for the tenth game, adding the additional bonus attempt if the player has a spare/strike in their first two attempts. Join each list into a single string, and replace any remaining 10's and 0's with 'X' and '-'. Join both strings to get the final scoreboard.
Python 3:
Output:
1
u/[deleted] Jan 25 '18 edited Jan 25 '18
Python 3.
Once the test framework was up and running solving the problem was a breeze.
output
1
u/Lksaar Jan 25 '18
C++
I recently started with C++, this is the first "real" challenge I have tackled. I would appreciate criticism/advice. I opted for 2 classes (which I seem to be the only one here, should I have opted for procedural based program?) Initially started with
std::vectorinstead ofstd::dequebut decided to use the latter since it allows to pop elements from the front. I initially tried to nest astd::vectorin anotherstd::vectorto break the frames apart and keep them in a vectory array (i.e.{ {6, 4} {5, 3} ... }), but I ran into issues and decided to create the Frame class which made the code more easily to read (atleast for me).I'm somewhat unhappy with the output function (and I spent the most time fixing the output),
toChar()only outputting one char tripped me up for a quite a while, using a char array there might have been a better choice.In the end I'm pretty happy how it turned out
Output:
1
u/wakaboo Jan 25 '18
Javascript
1
u/[deleted] Jan 25 '18
Haskell
OutPut:
Can anyone help me here? I just realized I cant use the values from
letwithwhere. My last line isBut i wanted to write something more like:
1
u/engageant Jan 25 '18
PowerShell
I found this one easier than a lot of the easy ones.
1
u/do_hickey Jan 25 '18
Python 3.6
I'm sure this is horribly inefficient, but at least it works? Any input is greatly appreciated.
Source:
Output:
1
u/g00glen00b Jan 25 '18 edited Jan 25 '18
JavaScript:
Probably not the most readable example, but concise I guess:
1
u/popillol Jan 25 '18
Go / Golang Playground Link. Nothing fancy. Assumes the input is the correct length.
1
u/DouglasMeyer Jan 25 '18
JavaScript/RegExp: jsfiddle. I was wondering if this could be done relying heavily on RegExp, I think this is close to what I was hoping for:
1
u/TiltChamberlain Jan 25 '18
Javascript
This challenge got pretty tricky for me and I feel as if there has to be an easier way to write this. I relied on multiple if-else statements to get through this thing, including a 50 line scoreTenthFrame function that is made entirely of if-else statements. With that being said, it does work with all challenge inputs including getting the spacing (three characters per frame) correct.
I just started with Javascript, so I'd love to hear some feedback about how to make this a bit cleaner or a new way to approach a problem like this.
Thanks all!
1
u/BlasphemousJoshua Jan 25 '18
Swift 4 Playground with neatly formatted output.
Output:
1
u/[deleted] Jan 25 '18
Ruby
This is a pretty simple solution. Not much to say here. Just runs frame by frame, skipping the second roll if the first was a strike. The 10th frame, of course, has to be handled specially.
1
u/[deleted] Jan 26 '18 edited Jan 26 '18
Python 3.6. Feedback welcome.
+/u/CompileBot Python
1
u/whywhynotorelsewhat Jan 26 '18
https://jsfiddle.net/ilearn/jq7m7xuj/
1
u/juanchi35 Jan 27 '18
JavaScript (feedback welcome):
1
u/DEElekgolo Jan 27 '18
Python 3
1
u/Picklegunner Jan 27 '18
Java
Decided to try this as a state machine because I thought it would be fun. Turned out to be longer than it needed to be
1
u/[deleted] Jan 27 '18
Python 3.6 First submission, could use optimization and more thought around edge cases
Output
1
u/Daanvdk 1 0 Jan 28 '18
Haskell
1
u/konaraddio Jan 28 '18
JavaScript
1
u/Legionof1 Jan 28 '18 edited Jan 28 '18
Absolute trash code...
Python 2.7
Output
1
u/Badaking Jan 29 '18
Python 3: I would really appreciate feedback
1
u/amdopt Jan 29 '18
C#
It works but it is hideous. I got frustrated with this. Comments/help/criticisms always welcome. I added in the 2 games from challenge #235 too.
Output
1
u/john0514 Jan 31 '18 edited Feb 01 '18
Javascript
1
u/Areumdaun Feb 04 '18 edited Feb 04 '18
Python 3
1
u/Imoa Feb 13 '18 edited Feb 13 '18
Python 3
Outputs
Not a very fancy solution but I managed to do it along with all of the challenge inputs. Worth mentioning too my solution doesn't address a gutter ball on the 10th frame. It's poor form but I just addressed the challenge inputs directly, so I currently don't handle XX- or X-.
1
u/ribenaboy15 Feb 28 '18
Recursive in F#:
1
u/2kofawsome Jul 03 '18
python3.6
With proper formatting, yay
0
u/[deleted] Jan 24 '18
[deleted] — view deleted comment