Skip to content

Commit d4fe18f

Browse files
committed
problem runner and both naive and full solution to problem 1
1 parent 0419579 commit d4fe18f

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

p1.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def sum_divisible_by(n):
2+
p = 1000 / n
3+
return n * (p * (p + 1)) / 2
4+
5+
6+
def main():
7+
return sum_divisible_by(3) + sum_divisible_by(5) - sum_divisible_by(15)
8+
9+
if __name__ == '__main__':
10+
print main()

p1naive.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def main():
2+
s = 0
3+
for i in range(1, 1000):
4+
if i % 3 == 0 or i % 5 == 0:
5+
s += i
6+
return s
7+
8+
if __name__ == '__main__':
9+
print main()

run.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#! /usr/bin/env python
2+
3+
from __future__ import division
4+
import sys
5+
from subprocess import call
6+
import timeit
7+
8+
9+
def sh(cmd):
10+
try:
11+
call(cmd)
12+
except:
13+
pass
14+
15+
16+
def clear():
17+
sh('find . -name "__pycache__" -delete')
18+
sh('find . -name "*.pyc" -delete')
19+
sh('find . -name "*~" -delete')
20+
21+
22+
def main():
23+
clear()
24+
25+
if not (len(sys.argv) == 2 or len(sys.argv) == 3):
26+
print 'usage: run.py n <y>'
27+
print 'where:'
28+
print '\tn is the exercise number'
29+
print '\ty the number of times to measure performance (default 1000)'
30+
sys.exit(-1)
31+
32+
m = 1000
33+
if len(sys.argv) == 3:
34+
m = int(sys.argv[2])
35+
36+
cmd_module = __import__('p' + sys.argv[1])
37+
t = timeit.Timer(stmt=cmd_module.main)
38+
try:
39+
print 'avg time: %.5fms' % (1000 * t.timeit(number=m) / m)
40+
print 'result: ' + str(cmd_module.main())
41+
except:
42+
t.print_exc()
43+
44+
clear()
45+
46+
47+
if __name__ == '__main__':
48+
main()

0 commit comments

Comments
 (0)