Skip to content

Commit efc3581

Browse files
fa1ldhruvmanila
authored andcommitted
Fix coding style for Project Euler problem 39 (TheAlgorithms#3023)
* improvements for project euler task 39 * add tests for solution() * fixed a typo * Update sol1.py Co-authored-by: Dhruv <[email protected]>
1 parent 8fc16e5 commit efc3581

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Diff for: project_euler/problem_39/sol1.py

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
Problem 39: https://projecteuler.net/problem=39
3+
24
If p is the perimeter of a right angle triangle with integral length sides,
35
{a,b,c}, there are exactly three solutions for p = 120.
46
{20,48,52}, {24,45,51}, {30,40,50}
@@ -8,10 +10,11 @@
810

911
from __future__ import annotations
1012

13+
import typing
1114
from collections import Counter
1215

1316

14-
def pythagorean_triple(max_perimeter: int) -> dict:
17+
def pythagorean_triple(max_perimeter: int) -> typing.Counter[int]:
1518
"""
1619
Returns a dictionary with keys as the perimeter of a right angled triangle
1720
and value as the number of corresponding triplets.
@@ -22,19 +25,31 @@ def pythagorean_triple(max_perimeter: int) -> dict:
2225
>>> pythagorean_triple(50)
2326
Counter({12: 1, 30: 1, 24: 1, 40: 1, 36: 1, 48: 1})
2427
"""
25-
triplets = Counter()
28+
triplets: typing.Counter[int] = Counter()
2629
for base in range(1, max_perimeter + 1):
2730
for perpendicular in range(base, max_perimeter + 1):
2831
hypotenuse = (base * base + perpendicular * perpendicular) ** 0.5
29-
if hypotenuse == int((hypotenuse)):
32+
if hypotenuse == int(hypotenuse):
3033
perimeter = int(base + perpendicular + hypotenuse)
3134
if perimeter > max_perimeter:
3235
continue
33-
else:
34-
triplets[perimeter] += 1
36+
triplets[perimeter] += 1
3537
return triplets
3638

3739

40+
def solution(n: int = 1000) -> int:
41+
"""
42+
Returns perimeter with maximum solutions.
43+
>>> solution(100)
44+
90
45+
>>> solution(200)
46+
180
47+
>>> solution(1000)
48+
840
49+
"""
50+
triplets = pythagorean_triple(n)
51+
return triplets.most_common(1)[0][0]
52+
53+
3854
if __name__ == "__main__":
39-
triplets = pythagorean_triple(1000)
40-
print(f"{triplets.most_common()[0][0] = }")
55+
print(f"Perimeter {solution()} has maximum solutions")

0 commit comments

Comments
 (0)