Skip to content

Commit 501a2ff

Browse files
authored
[Project Euler] Fix code style for multiple problems (TheAlgorithms#3094)
* Fix code style for Project Euler problems: - 13, 17, 21 - Default args - Type hints - File path * Fix code style for multiple problems * Made suggested changes
1 parent c961d55 commit 501a2ff

File tree

11 files changed

+89
-67
lines changed

11 files changed

+89
-67
lines changed

project_euler/problem_13/sol1.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
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
"""
21-
return str(sum(array))[:10]
19+
file_path = os.path.join(os.path.dirname(__file__), "num.txt")
20+
with open(file_path, "r") as file_hand:
21+
return str(sum([int(line) for line in file_hand]))[:10]
2222

2323

2424
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))
25+
print(solution())

project_euler/problem_17/sol1.py

+2-2
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

+3-4
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)

project_euler/problem_31/sol1.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Coin sums
3-
Problem 31
3+
Problem 31: https://projecteuler.net/problem=31
4+
45
In England the currency is made up of pound, £, and pence, p, and there are
56
eight coins in general circulation:
67
@@ -12,39 +13,39 @@
1213
"""
1314

1415

15-
def one_pence():
16+
def one_pence() -> int:
1617
return 1
1718

1819

19-
def two_pence(x):
20+
def two_pence(x: int) -> int:
2021
return 0 if x < 0 else two_pence(x - 2) + one_pence()
2122

2223

23-
def five_pence(x):
24+
def five_pence(x: int) -> int:
2425
return 0 if x < 0 else five_pence(x - 5) + two_pence(x)
2526

2627

27-
def ten_pence(x):
28+
def ten_pence(x: int) -> int:
2829
return 0 if x < 0 else ten_pence(x - 10) + five_pence(x)
2930

3031

31-
def twenty_pence(x):
32+
def twenty_pence(x: int) -> int:
3233
return 0 if x < 0 else twenty_pence(x - 20) + ten_pence(x)
3334

3435

35-
def fifty_pence(x):
36+
def fifty_pence(x: int) -> int:
3637
return 0 if x < 0 else fifty_pence(x - 50) + twenty_pence(x)
3738

3839

39-
def one_pound(x):
40+
def one_pound(x: int) -> int:
4041
return 0 if x < 0 else one_pound(x - 100) + fifty_pence(x)
4142

4243

43-
def two_pound(x):
44+
def two_pound(x: int) -> int:
4445
return 0 if x < 0 else two_pound(x - 200) + one_pound(x)
4546

4647

47-
def solution(n):
48+
def solution(n: int = 200) -> int:
4849
"""Returns the number of different ways can n pence be made using any number of
4950
coins?
5051
@@ -61,4 +62,4 @@ def solution(n):
6162

6263

6364
if __name__ == "__main__":
64-
print(solution(int(str(input()).strip())))
65+
print(solution(int(input().strip())))

project_euler/problem_31/sol2.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
"""Coin sums
1+
"""
2+
Problem 31: https://projecteuler.net/problem=31
3+
4+
Coin sums
25
36
In England the currency is made up of pound, £, and pence, p, and there are
47
eight coins in general circulation:
@@ -29,7 +32,7 @@
2932
"""
3033

3134

32-
def solution(pence: int) -> int:
35+
def solution(pence: int = 200) -> int:
3336
"""Returns the number of different ways to make X pence using any number of coins.
3437
The solution is based on dynamic programming paradigm in a bottom-up fashion.
3538

project_euler/problem_33/sol1.py

+29-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Problem:
2+
Problem 33: https://projecteuler.net/problem=33
33
44
The fraction 49/98 is a curious fraction, as an inexperienced
55
mathematician in attempting to simplify it may incorrectly believe
@@ -14,42 +14,55 @@
1414
If the product of these four fractions is given in its lowest common
1515
terms, find the value of the denominator.
1616
"""
17+
from fractions import Fraction
18+
from typing import List
1719

1820

19-
def isDigitCancelling(num, den):
21+
def is_digit_cancelling(num: int, den: int) -> bool:
2022
if num != den:
2123
if num % 10 == den // 10:
2224
if (num // 10) / (den % 10) == num / den:
2325
return True
26+
return False
2427

2528

26-
def solve(digit_len: int) -> str:
29+
def fraction_list(digit_len: int) -> List[str]:
2730
"""
28-
>>> solve(2)
29-
'16/64 , 19/95 , 26/65 , 49/98'
30-
>>> solve(3)
31-
'16/64 , 19/95 , 26/65 , 49/98'
32-
>>> solve(4)
33-
'16/64 , 19/95 , 26/65 , 49/98'
34-
>>> solve(0)
35-
''
36-
>>> solve(5)
37-
'16/64 , 19/95 , 26/65 , 49/98'
31+
>>> fraction_list(2)
32+
['16/64', '19/95', '26/65', '49/98']
33+
>>> fraction_list(3)
34+
['16/64', '19/95', '26/65', '49/98']
35+
>>> fraction_list(4)
36+
['16/64', '19/95', '26/65', '49/98']
37+
>>> fraction_list(0)
38+
[]
39+
>>> fraction_list(5)
40+
['16/64', '19/95', '26/65', '49/98']
3841
"""
3942
solutions = []
4043
den = 11
4144
last_digit = int("1" + "0" * digit_len)
4245
for num in range(den, last_digit):
4346
while den <= 99:
4447
if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
45-
if isDigitCancelling(num, den):
48+
if is_digit_cancelling(num, den):
4649
solutions.append(f"{num}/{den}")
4750
den += 1
4851
num += 1
4952
den = 10
50-
solutions = " , ".join(solutions)
5153
return solutions
5254

5355

56+
def solution(n: int = 2) -> int:
57+
"""
58+
Return the solution to the problem
59+
"""
60+
result = 1.0
61+
for fraction in fraction_list(n):
62+
frac = Fraction(fraction)
63+
result *= frac.denominator / frac.numerator
64+
return int(result)
65+
66+
5467
if __name__ == "__main__":
55-
print(solve(2))
68+
print(solution())

project_euler/problem_43/sol1.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 43: https://projecteuler.net/problem=43
3+
24
The number, 1406357289, is a 0 to 9 pandigital number because it is made up of
35
each of the digits 0 to 9 in some order, but it also has a rather interesting
46
sub-string divisibility property.
@@ -38,11 +40,11 @@ def is_substring_divisible(num: tuple) -> bool:
3840
return True
3941

4042

41-
def compute_sum(n: int = 10) -> int:
43+
def solution(n: int = 10) -> int:
4244
"""
4345
Returns the sum of all pandigital numbers which pass the
4446
divisiility tests.
45-
>>> compute_sum(10)
47+
>>> solution(10)
4648
16695334890
4749
"""
4850
list_nums = [
@@ -55,4 +57,4 @@ def compute_sum(n: int = 10) -> int:
5557

5658

5759
if __name__ == "__main__":
58-
print(f"{compute_sum(10) = }")
60+
print(f"{solution() = }")

project_euler/problem_44/sol1.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 44: https://projecteuler.net/problem=44
3+
24
Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten
35
pentagonal numbers are:
46
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
@@ -24,11 +26,11 @@ def is_pentagonal(n: int) -> bool:
2426
return ((1 + root) / 6) % 1 == 0
2527

2628

27-
def compute_num(limit: int = 5000) -> int:
29+
def solution(limit: int = 5000) -> int:
2830
"""
2931
Returns the minimum difference of two pentagonal numbers P1 and P2 such that
3032
P1 + P2 is pentagonal and P2 - P1 is pentagonal.
31-
>>> compute_num(5000)
33+
>>> solution(5000)
3234
5482660
3335
"""
3436
pentagonal_nums = [(i * (3 * i - 1)) // 2 for i in range(1, limit)]
@@ -42,4 +44,4 @@ def compute_num(limit: int = 5000) -> int:
4244

4345

4446
if __name__ == "__main__":
45-
print(f"{compute_num() = }")
47+
print(f"{solution() = }")

project_euler/problem_46/sol1.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 46: https://projecteuler.net/problem=46
3+
24
It was proposed by Christian Goldbach that every odd composite number can be
35
written as the sum of a prime and twice a square.
46
@@ -84,5 +86,10 @@ def compute_nums(n: int) -> list[int]:
8486
return list_nums
8587

8688

89+
def solution() -> int:
90+
"""Return the solution to the problem"""
91+
return compute_nums(1)[0]
92+
93+
8794
if __name__ == "__main__":
88-
print(f"{compute_nums(1) = }")
95+
print(f"{solution() = }")

project_euler/problem_551/sol1.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def add(digits, k, addend):
167167
digits.append(digit)
168168

169169

170-
def solution(n):
170+
def solution(n: int = 10 ** 15) -> int:
171171
"""
172172
returns n-th term of sequence
173173
@@ -197,4 +197,4 @@ def solution(n):
197197

198198

199199
if __name__ == "__main__":
200-
print(solution(10 ** 15))
200+
print(f"{solution() = }")

project_euler/problem_63/sol1.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
"""
1212

1313

14-
def compute_nums(max_base: int = 10, max_power: int = 22) -> int:
14+
def solution(max_base: int = 10, max_power: int = 22) -> int:
1515
"""
1616
Returns the count of all n-digit numbers which are nth power
17-
>>> compute_nums(10, 22)
17+
>>> solution(10, 22)
1818
49
19-
>>> compute_nums(0, 0)
19+
>>> solution(0, 0)
2020
0
21-
>>> compute_nums(1, 1)
21+
>>> solution(1, 1)
2222
0
23-
>>> compute_nums(-1, -1)
23+
>>> solution(-1, -1)
2424
0
2525
"""
2626
bases = range(1, max_base)
@@ -31,4 +31,4 @@ def compute_nums(max_base: int = 10, max_power: int = 22) -> int:
3131

3232

3333
if __name__ == "__main__":
34-
print(f"{compute_nums(10, 22) = }")
34+
print(f"{solution(10, 22) = }")

0 commit comments

Comments
 (0)