(Python)
def number_of_tieless_rugby_games(n):
"""
Returns the number of tieless rugby games with n scoring events.
A scoring event is a number in (-7, -5, -3, 3, 5, 7) and a game is tieless
if the score is never zero, apart from at the start.
Negative points represent points for the away team, positive points
represent points for the home team
"""
dictionary_of_scores = {0:1}
# The keys of this dictionary represent possible scores.
# The values represent the number of ways this score can be reached.
scoring_events = (-7, -5, -3, 3, 5, 7)
for i in range(n):
# At each stage, we have the nonzero scores with i scoring events in
# dictionary_of_scores. To find nonzero scores with i+1 scoring events
# consider each nonzero score, and each possibility for the next
# scoring event.
old_dictionary = dictionary_of_scores
dictionary_of_scores = {}
for score, number_of_ways in old_dictionary.items():
for scoring_event in scoring_events:
new_score = score + scoring_event
if new_score != 0:
dictionary_of_scores[new_score] =\
dictionary_of_scores.get(new_score, 0) + number_of_ways
return sum(dictionary_of_scores.values())
|