Skip to content

Instantly share code, notes, and snippets.

@safehammad
Created December 23, 2012 16:06
Show Gist options
  • Save safehammad/4364078 to your computer and use it in GitHub Desktop.
Save safehammad/4364078 to your computer and use it in GitHub Desktop.
Tennis scorer.
#!/usr/bin/env python3
"""Tennis scorer developed during Python Northwest coding meeting Dec 2012.
Given a current score and the winner of the next point, the script
will return the next score.
Usage: python3 tennis.py <p1_score> <p2_score> <scorer>
Output: <p1_score> <p2_score>
Where <pn_score> is any of 0, 15, 30, 40, ADV, WIN.
And <next_scorer> is any of 1, 2.
Examples:
$ python3 tennis.py 15 30 1
30 30
$ python3 tennis.py 40 ADV 1
40 40
$ python3 tennis.py 40 30 1
WIN 30
"""
import sys
points = ['0', '15', '30', '40', 'WIN']
points_deuce = ['40', 'ADV', 'WIN']
def next_score(p1_score, p2_score, scorer):
score = [p1_score, p2_score]
scorer = int(scorer) - 1
loser = abs(scorer - 1)
if score < ['40', '40']:
score[scorer] = points[points.index(score[scorer]) + 1]
elif score[loser] == '40':
score[scorer] = points_deuce[points_deuce.index(score[scorer]) + 1]
else:
score = ['40', '40']
return score
if __name__ == '__main__':
print(' '.join(next_score(*sys.argv[1:])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment