TheAlgorithms-Python
104 строки · 3.1 Кб
1"""
2Prize Strings
3Problem 191
4
5A particular school offers cash rewards to children with good attendance and
6punctuality. If they are absent for three consecutive days or late on more
7than one occasion then they forfeit their prize.
8
9During an n-day period a trinary string is formed for each child consisting
10of L's (late), O's (on time), and A's (absent).
11
12Although there are eighty-one trinary strings for a 4-day period that can be
13formed, exactly forty-three strings would lead to a prize:
14
15OOOO OOOA OOOL OOAO OOAA OOAL OOLO OOLA OAOO OAOA
16OAOL OAAO OAAL OALO OALA OLOO OLOA OLAO OLAA AOOO
17AOOA AOOL AOAO AOAA AOAL AOLO AOLA AAOO AAOA AAOL
18AALO AALA ALOO ALOA ALAO ALAA LOOO LOOA LOAO LOAA
19LAOO LAOA LAAO
20
21How many "prize" strings exist over a 30-day period?
22
23References:
24- The original Project Euler project page:
25https://projecteuler.net/problem=191
26"""
27
28cache: dict[tuple[int, int, int], int] = {}29
30
31def _calculate(days: int, absent: int, late: int) -> int:32"""33A small helper function for the recursion, mainly to have
34a clean interface for the solution() function below.
35
36It should get called with the number of days (corresponding
37to the desired length of the 'prize strings'), and the
38initial values for the number of consecutive absent days and
39number of total late days.
40
41>>> _calculate(days=4, absent=0, late=0)
4243
43>>> _calculate(days=30, absent=2, late=0)
440
45>>> _calculate(days=30, absent=1, late=0)
4698950096
47"""
48
49# if we are absent twice, or late 3 consecutive days,50# no further prize strings are possible51if late == 3 or absent == 2:52return 053
54# if we have no days left, and have not failed any other rules,55# we have a prize string56if days == 0:57return 158
59# No easy solution, so now we need to do the recursive calculation60
61# First, check if the combination is already in the cache, and62# if yes, return the stored value from there since we already63# know the number of possible prize strings from this point on64key = (days, absent, late)65if key in cache:66return cache[key]67
68# now we calculate the three possible ways that can unfold from69# this point on, depending on our attendance today70
71# 1) if we are late (but not absent), the "absent" counter stays as72# it is, but the "late" counter increases by one73state_late = _calculate(days - 1, absent, late + 1)74
75# 2) if we are absent, the "absent" counter increases by 1, and the76# "late" counter resets to 077state_absent = _calculate(days - 1, absent + 1, 0)78
79# 3) if we are on time, this resets the "late" counter and keeps the80# absent counter81state_ontime = _calculate(days - 1, absent, 0)82
83prizestrings = state_late + state_absent + state_ontime84
85cache[key] = prizestrings86return prizestrings87
88
89def solution(days: int = 30) -> int:90"""91Returns the number of possible prize strings for a particular number
92of days, using a simple recursive function with caching to speed it up.
93
94>>> solution()
951918080160
96>>> solution(4)
9743
98"""
99
100return _calculate(days, absent=0, late=0)101
102
103if __name__ == "__main__":104print(solution())105