Skip to content

Add solution for Project Euler problem 191 #2875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
105 changes: 105 additions & 0 deletions project_euler/problem_191/sol1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
"""
Prize Strings
Problem 191
A particular school offers cash rewards to children with good attendance and
punctuality. If they are absent for three consecutive days or late on more
than one occasion then they forfeit their prize.
During an n-day period a trinary string is formed for each child consisting
of L's (late), O's (on time), and A's (absent).
Although there are eighty-one trinary strings for a 4-day period that can be
formed, exactly forty-three strings would lead to a prize:
OOOO OOOA OOOL OOAO OOAA OOAL OOLO OOLA OAOO OAOA
OAOL OAAO OAAL OALO OALA OLOO OLOA OLAO OLAA AOOO
AOOA AOOL AOAO AOAA AOAL AOLO AOLA AAOO AAOA AAOL
AALO AALA ALOO ALOA ALAO ALAA LOOO LOOA LOAO LOAA
LAOO LAOA LAAO
How many "prize" strings exist over a 30-day period?
References:
- The original Project Euler project page:
https://projecteuler.net/problem=191
"""


cache = {}


def _calculate(days: int, absent: int, late: int) -> int:
"""
A small helper function for the recursion, mainly to have
a clean interface for the solution() function below.
It should get called with the number of days (corresponding
to the desired length of the 'prize strings'), and the
initial values for the number of consecutive absent days and
number of total late days.
>>> _calculate(days=4, absent=0, late=0)
43
>>> _calculate(days=30, absent=2, late=0)
0
>>> _calculate(days=30, absent=1, late=0)
98950096
"""

# if we are absent twice, or late 3 consecutive days,
# no further prize strings are possible
if late == 3 or absent == 2:
return 0
Comment on lines +50 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot hold your hand throughout your life :)

I will help you out here with this code as an example. The code here says if the late variable is 3 or the absent variable is 2 then we will return 0 and the comment says the same thing. What I want you to see is that comments are meant to explain the code ONLY if it's necessary but here the code is pretty self-explanatory. The comment here says the same thing in English.

What I basically mean here is that your code is perfect, you shouldn't need a ton of comments to explain what you have written. You don't want to keep referring to a dictionary when you're reading a book otherwise it breaks your flow.


# if we have no days left, and have not failed any other rules,
# we have a prize string
if days == 0:
return 1

# No easy solution, so now we need to do the recursive calculation

# First, check if the combination is already in the cache, and
# if yes, return the stored value from there since we already
# know the number of possible prize strings from this point on
key = (days, absent, late)
if key in cache:
return cache[key]

# now we calculate the three possible ways that can unfold from
# this point on, depending on our attendance today

# 1) if we are late (but not absent), the "absent" counter stays as
# it is, but the "late" counter increases by one
state_late = _calculate(days - 1, absent, late + 1)

# 2) if we are absent, the "absent" counter increases by 1, and the
# "late" counter resets to 0
state_absent = _calculate(days - 1, absent + 1, 0)

# 3) if we are on time, this resets the "late" counter and keeps the
# absent counter
state_ontime = _calculate(days - 1, absent, 0)

prizestrings = state_late + state_absent + state_ontime

cache[key] = prizestrings
return prizestrings


def solution(days: int = 30) -> int:
"""
Returns the number of possible prize strings for a particular number
of days, using a simple recursive function with caching to speed it up.
>>> solution()
1918080160
>>> solution(4)
43
"""

return _calculate(days, absent=0, late=0)


if __name__ == "__main__":
print(solution())