Game to eleven
- O(n) · Medium
- Full plan
- Python
- JavaScript
- strings
- loops
- conditionals
Problem
"To eleven, win by two," the umpire says before every ping-pong game at the club. The first player to reach 11 points wins, but only with a lead of 2 or more: at 10 all, play goes on until someone pulls ahead by 2. Write a function that takes the points in order, as a string where each letter is a point for player A or player B, and returns "A" or "B", whoever won. If the game isn't over yet, or the string is empty, return "". Eleven A in a row: A wins 11 to 0. At 11 to 10 there is no winner yet; at 12 to 10 there is.
As soon as someone wins, the game is over: if the string has more letters after that, ignore them.
Examples
Eleven to nothing
"AAAAAAAAAAA" → "A"
B wins 11 to 5
"ABABABABABBBBBBB" → "B"
Not over yet
"AABBA" → ""
11 to 10 is not enough
"ABABABABABABABABABABA" → ""
Long deuce, B wins 14 to 12
"ABABABABABABABABABABABBABB" → "B"
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def winner(points):
passJavaScript
function winner(points) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.