patternpythonMinor
Project Euler module
Viewed 0 times
projectmoduleeuler
Problem
I use Project Euler to teach me programming and not to submit any results. As such I look up the expected return values to double check my solutions.
To organise my files I use the following folder structure:
My
I'll show now two example files to show the source files and how old code can be reused.
```
"""By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below"""
SOLL = 1074
def run(file = "input/18.dat"):
# Parse File
with open(file) as fid:
tri = [[int(num) for num in line.split(' ')] for line in fid.read().split('\n')]
# From bottom's up find the maximal value
for row
To organise my files I use the following folder structure:
main.py
\euler # The problem files
__init__.py # empty
e001.py
e002.py
...
\input # Additional input files
8.dat
11.dat
...My
main.py file is the common entry point. It can either run all the solved examples so far or a specific one. This second option is added that I don't need to add an if __name__ == '__main__' guard in every file. The file looks as follows:TOP_LEVEL = "euler"
def run_module(num):
"""Run specific Problem"""
mod = importlib.import_module('%s.e%0.3i' % (TOP_LEVEL, num))
start = time.time()
ist = mod.run()
print(" %5i | %6.3f | %s | %i" % \
(num, time.time() - start, "ox"[ist == mod.SOLL], ist))
if __name__ == '__main__':
N_MAX = 67
# Pre Header
print('Problem | Time | x/o | Solution')
print("--------+--------+-----+---------")
global_time = time.time()
# Run over all problems
if len(sys.argv) == 2:
run_module(int(sys.argv[1]))
else:
for num in range(1, N_MAX + 1):
run_module(num)
# End Header
print("--------+--------+-----+---------")
print("Total: %.3f s" % (time.time() - global_time))I'll show now two example files to show the source files and how old code can be reused.
e018.py:```
"""By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
3
7 4
2 4 6
8 5 9 3
That is, 3 + 7 + 4 + 9 = 23.
Find the maximum total from top to bottom of the triangle below"""
SOLL = 1074
def run(file = "input/18.dat"):
# Parse File
with open(file) as fid:
tri = [[int(num) for num in line.split(' ')] for line in fid.read().split('\n')]
# From bottom's up find the maximal value
for row
Solution
Use
Using
Be generous with long variables names
For example
Don't abuse Code-Golf techniques
The line:
is a well known Code-Golf trick that relies on implicit boolean to integer conversion that is equivalent to:
please use the latter.
Use
C-Style arguments such as
Don't shadow built-ins
Explode long lines
Divide the following lines in two please.
.formatUsing
% is considered old style, so:print(" %5i | %6.3f | %s | %i".format(
(num, time.time() - start, "ox"[ist == mod.SOLL], ist)))Be generous with long variables names
For example
ist is impossible to understand for me, solution is more naturalDon't abuse Code-Golf techniques
The line:
"ox"[ist == mod.SOLL]is a well known Code-Golf trick that relies on implicit boolean to integer conversion that is equivalent to:
"x" if ist == mod.SOLL else "o"please use the latter.
Use
argparseC-Style arguments such as
sys.argv[1] should be avoided, I suggest argparse (help to get started here: https://stackoverflow.com/questions/7427101/dead-simple-argparse-example-wanted-1-argument-3-results)Don't shadow built-ins
def run(file = "input/18.dat"):
# Parse File
with open(file) as fid:file is a built-in, you should use file_ in your code.Explode long lines
Divide the following lines in two please.
tri = [[int(num) for num in line.split(' ')] for line in fid.read().split('\n')]Code Snippets
print(" %5i | %6.3f | %s | %i".format(
(num, time.time() - start, "ox"[ist == mod.SOLL], ist)))"ox"[ist == mod.SOLL]"x" if ist == mod.SOLL else "o"def run(file = "input/18.dat"):
# Parse File
with open(file) as fid:tri = [[int(num) for num in line.split(' ')] for line in fid.read().split('\n')]Context
StackExchange Code Review Q#78745, answer score: 3
Revisions (0)
No revisions yet.