Skip to content

Commit 2080191

Browse files
committed
Fix code style for Project Euler problems:
- 13, 17, 21 - Default args - Type hints - File path
1 parent 216a194 commit 2080191

File tree

3 files changed

+21
-22
lines changed

3 files changed

+21
-22
lines changed

project_euler/problem_13/sol1.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
"""
2+
Problem 13: https://projecteuler.net/problem=13
3+
24
Problem Statement:
35
Work out the first ten digits of the sum of the following one-hundred 50-digit
46
numbers.
57
"""
8+
import os
69

710

8-
def solution(array):
9-
"""Returns the first ten digits of the sum of the array elements.
11+
def solution():
12+
"""
13+
Returns the first ten digits of the sum of the array elements
14+
from the file num.txt
1015
11-
>>> import os
12-
>>> sum = 0
13-
>>> array = []
14-
>>> with open(os.path.dirname(__file__) + "/num.txt","r") as f:
15-
... for line in f:
16-
... array.append(int(line))
17-
...
18-
>>> solution(array)
16+
>>> solution()
1917
'5537376230'
2018
"""
19+
array = []
20+
21+
file_path = os.path.join(os.path.dirname(__file__), "num.txt")
22+
with open(file_path, "r") as file_hand:
23+
for line in file_hand:
24+
array.append(int(line))
25+
2126
return str(sum(array))[:10]
2227

2328

2429
if __name__ == "__main__":
25-
n = int(input().strip())
26-
27-
array = []
28-
for i in range(n):
29-
array.append(int(input().strip()))
30-
print(solution(array))
30+
print(solution())

project_euler/problem_17/sol1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""
22
Number letter counts
3-
Problem 17
3+
Problem 17: https://projecteuler.net/problem=17
44
55
If the numbers 1 to 5 are written out in words: one, two, three, four, five,
66
then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
@@ -16,7 +16,7 @@
1616
"""
1717

1818

19-
def solution(n):
19+
def solution(n: int = 1000) -> int:
2020
"""Returns the number of letters used to write all numbers from 1 to n.
2121
where n is lower or equals to 1000.
2222
>>> solution(1000)

project_euler/problem_21/sol1.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from math import sqrt
2-
31
"""
42
Amicable Numbers
53
Problem 21
@@ -15,9 +13,10 @@
1513
1614
Evaluate the sum of all the amicable numbers under 10000.
1715
"""
16+
from math import sqrt
1817

1918

20-
def sum_of_divisors(n):
19+
def sum_of_divisors(n: int) -> int:
2120
total = 0
2221
for i in range(1, int(sqrt(n) + 1)):
2322
if n % i == 0 and i != sqrt(n):
@@ -27,7 +26,7 @@ def sum_of_divisors(n):
2726
return total - n
2827

2928

30-
def solution(n):
29+
def solution(n: int = 10000) -> int:
3130
"""Returns the sum of all the amicable numbers under n.
3231
3332
>>> solution(10000)

0 commit comments

Comments
 (0)