Skip to content

Commit 11a5afd

Browse files
authored
Add type hints and default args for problem 20 (#2962)
- Improved variable names - Added type hints - Added default argument values for validate_solutions script
1 parent e41d041 commit 11a5afd

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

Diff for: project_euler/problem_20/sol1.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 20: https://projecteuler.net/problem=20
3+
24
n! means n × (n − 1) × ... × 3 × 2 × 1
35
46
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
@@ -8,14 +10,15 @@
810
"""
911

1012

11-
def factorial(n):
13+
def factorial(num: int) -> int:
14+
"""Find the factorial of a given number n"""
1215
fact = 1
13-
for i in range(1, n + 1):
16+
for i in range(1, num + 1):
1417
fact *= i
1518
return fact
1619

1720

18-
def split_and_add(number):
21+
def split_and_add(number: int) -> int:
1922
"""Split number digits and add them."""
2023
sum_of_digits = 0
2124
while number > 0:
@@ -25,8 +28,8 @@ def split_and_add(number):
2528
return sum_of_digits
2629

2730

28-
def solution(n):
29-
"""Returns the sum of the digits in the number 100!
31+
def solution(num: int = 100) -> int:
32+
"""Returns the sum of the digits in the factorial of num
3033
>>> solution(100)
3134
648
3235
>>> solution(50)
@@ -42,8 +45,8 @@ def solution(n):
4245
>>> solution(1)
4346
1
4447
"""
45-
f = factorial(n)
46-
result = split_and_add(f)
48+
nfact = factorial(num)
49+
result = split_and_add(nfact)
4750
return result
4851

4952

Diff for: project_euler/problem_20/sol2.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 20: https://projecteuler.net/problem=20
3+
24
n! means n × (n − 1) × ... × 3 × 2 × 1
35
46
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
@@ -9,8 +11,8 @@
911
from math import factorial
1012

1113

12-
def solution(n):
13-
"""Returns the sum of the digits in the number 100!
14+
def solution(num: int = 100) -> int:
15+
"""Returns the sum of the digits in the factorial of num
1416
>>> solution(100)
1517
648
1618
>>> solution(50)
@@ -26,7 +28,7 @@ def solution(n):
2628
>>> solution(1)
2729
1
2830
"""
29-
return sum([int(x) for x in str(factorial(n))])
31+
return sum([int(x) for x in str(factorial(num))])
3032

3133

3234
if __name__ == "__main__":

Diff for: project_euler/problem_20/sol3.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 20: https://projecteuler.net/problem=20
3+
24
n! means n × (n − 1) × ... × 3 × 2 × 1
35
46
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
@@ -9,8 +11,8 @@
911
from math import factorial
1012

1113

12-
def solution(n):
13-
"""Returns the sum of the digits in the number 100!
14+
def solution(num: int = 100) -> int:
15+
"""Returns the sum of the digits in the factorial of num
1416
>>> solution(1000)
1517
10539
1618
>>> solution(200)
@@ -32,7 +34,7 @@ def solution(n):
3234
>>> solution(0)
3335
1
3436
"""
35-
return sum(map(int, str(factorial(n))))
37+
return sum(map(int, str(factorial(num))))
3638

3739

3840
if __name__ == "__main__":

Diff for: project_euler/problem_20/sol4.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 20: https://projecteuler.net/problem=20
3+
24
n! means n × (n − 1) × ... × 3 × 2 × 1
35
46
For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,
@@ -8,8 +10,8 @@
810
"""
911

1012

11-
def solution(n):
12-
"""Returns the sum of the digits in the number 100!
13+
def solution(num: int = 100) -> int:
14+
"""Returns the sum of the digits in the factorial of num
1315
>>> solution(100)
1416
648
1517
>>> solution(50)
@@ -27,7 +29,7 @@ def solution(n):
2729
"""
2830
fact = 1
2931
result = 0
30-
for i in range(1, n + 1):
32+
for i in range(1, num + 1):
3133
fact *= i
3234

3335
for j in str(fact):

0 commit comments

Comments
 (0)