Tennis Set
In tennis, the winner of a set is based on how many games each player wins. The first player to win 6 games is declared the winner unless their opponent had already won 5 games, in which case the set continues until one of the players has won 7 games.
Given two integers score1 and score2, your task is to determine if it is possible for a tennis set to be finished with a final score of score1 : score2.
Example
For
score1 = 3andscore2 = 6, the output should betennis_set(score1, score2) = trueSince player 1 hadn't reached
5wins, the set ends once player 2 has won6games.For
score1 = 8andscore2 = 5, the output should betennis_set(score1, score2) = falseSince both players won at least
5games, the set would've ended once one of them won the7thone.For
score1 = 6andscore2 = 5, the output should betennis_set(score1, score2) = falseThis set will continue until one of these players wins their
7thgame, so this can't be the final score.
Solution
def tennis_set(score1, score2):
max_score = max(score1, score2)
min_score = min(score1, score2)
if max_score == 6 and min_score < 5:
return True
if max_score == 7 and (min_score in (5, 6)):
return True
return False
print(tennis_set(6, 3))