Skip to content

Commit aaaec6e

Browse files
committed
adv python
1 parent dbd08f5 commit aaaec6e

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import time
2+
3+
def sumOfN2(n):
4+
start = time.time()
5+
theSum = 0
6+
for i in range(1,n+1):
7+
theSum = theSum + i
8+
end = time.time()
9+
return theSum, end-start
10+
11+
if __name__ == '__main__':
12+
n = 5
13+
print("총 합계: %d\t 시간: %10.7f초" % sumOfN2(n))
14+
n = 200
15+
print("총 합계: %d\t 시간: %10.7f초" % sumOfN2(n))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
This is the "example" module.
3+
4+
The example module supplies one function, factorial(). For example,
5+
6+
>>> factorial(5)
7+
120
8+
"""
9+
10+
def factorial(n):
11+
"""Return the factorial of n, an exact integer >= 0.
12+
13+
>>> [factorial(n) for n in range(6)]
14+
[1, 1, 2, 6, 24, 120]
15+
>>> factorial(30)
16+
265252859812191058636308480000000
17+
>>> factorial(-1)
18+
Traceback (most recent call last):
19+
...
20+
ValueError: n must be >= 0
21+
22+
Factorials of floats are OK, but the float must be an exact integer:
23+
>>> factorial(30.1)
24+
Traceback (most recent call last):
25+
...
26+
ValueError: n must be exact integer
27+
>>> factorial(30.0)
28+
265252859812191058636308480000000
29+
30+
It must also not be ridiculously large:
31+
>>> factorial(1e100)
32+
Traceback (most recent call last):
33+
...
34+
OverflowError: n too large
35+
"""
36+
37+
import math
38+
if not n >= 0:
39+
raise ValueError("n must be >= 0")
40+
if math.floor(n) != n:
41+
raise ValueError("n must be exact integer")
42+
if n+1 == n: # catch a value like 1e300
43+
raise OverflowError("n too large")
44+
result = 1
45+
factor = 2
46+
while factor <= n:
47+
result *= factor
48+
factor += 1
49+
return result
50+
51+
52+
if __name__ == "__main__":
53+
import doctest
54+
doctest.testmod()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
def func(x):
2+
return x + 1
3+
4+
def test_answer():
5+
assert func(3) == 51

0 commit comments

Comments
 (0)