Readable and pragmatic over fast and short.
# Get a virtual environment with the correct python version and dependencies
pipenv shell
# Install dependencies
pipenv install
# Run a solution
python -m solutions.problem_xxx
# Benchmark a solution
time python -m solutions.problem_xxx
# Run doctests
python -m doctest -v solutions/problem_xxx.py
# Print more helpful information than the answer
LOG_LEVEL=info python -m solutions.problem_063
# View the documentation for a module
python -m pydoc solutions.problem_xxx
python -m pydoc common.tools
# Run and benchmark all solutions
python -m common.report
"""
Explain the solution in the module docstring
"""
from common.logging import info
def solve(limit):
"""Test the example given in the question in a doctest:
>>> solve(8)
42
"""
info(f"Log other information using the logging functions.")
return 42
if __name__ == "__main__":
print(solve(1_000_000))
Structuring each solution like this is slightly more cumbersome than simply printing the answer, but gives several benefits:
- Each file can still simply be run to output the result
- Enables tools like
pydoc
anddoctest
to run without triggering the often expensivesolve()
-function - Lets you import functions from other solutions
- Lets you keep variables from the examples around to verify the solution
- Lets you keep useful debugging output that often explains the solution really well without having ugly commented out
print()
-statements
Links to relevant Python documentation