Using Python without print At the beginning of a game of snooker, the red balls
ID: 3742825 • Letter: U
Question
Using Python without print
At the beginning of a game of snooker, the red balls are placed in a triangular pattern, with 1 ball in the first row and 1 more ball in each subsequent row (see the snooker setup here). This is also the setup at the beginning of a game of eight-ball pool (image here). For example, the first 3 rows contain 6 balls (1+2+3) and the first 5 rows contain 15 balls (1+2+3+4+5).
This is related to a concept in computer science and mathematics called triangular numbers, which you should research here. Look for a formula for computing a triangular number 1 + · · · + n directly (that is, without adding them explicitly using n 1 additions, which is slower and would require iteration with a for loop, a future topic).
Write the function nSnookerBall(nRow) that takes a nonnegative integer nRow, representing the number of rows of balls, and returns an integer, the number of balls in the associated rack. Notice that we are generalizing from a rack of 5 rows to a rack with any number of rows.
Explanation / Answer
def nSnookerBall(nRow): return (nRow * (nRow+1)) // 2 print(nSnookerBall(3)) print(nSnookerBall(5)) print(nSnookerBall(10))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.