Skip to content

[Project Euler] Fix code style for multiple problems #3094

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions project_euler/problem_13/sol1.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
"""
Problem 13: https://projecteuler.net/problem=13

Problem Statement:
Work out the first ten digits of the sum of the following one-hundred 50-digit
numbers.
"""
import os


def solution(array):
"""Returns the first ten digits of the sum of the array elements.
def solution():
"""
Returns the first ten digits of the sum of the array elements
from the file num.txt

>>> import os
>>> sum = 0
>>> array = []
>>> with open(os.path.dirname(__file__) + "/num.txt","r") as f:
... for line in f:
... array.append(int(line))
...
>>> solution(array)
>>> solution()
'5537376230'
"""
array = []

file_path = os.path.join(os.path.dirname(__file__), "num.txt")
with open(file_path, "r") as file_hand:
for line in file_hand:
array.append(int(line))

return str(sum(array))[:10]


if __name__ == "__main__":
n = int(input().strip())

array = []
for i in range(n):
array.append(int(input().strip()))
print(solution(array))
print(solution())
4 changes: 2 additions & 2 deletions project_euler/problem_17/sol1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Number letter counts
Problem 17
Problem 17: https://projecteuler.net/problem=17

If the numbers 1 to 5 are written out in words: one, two, three, four, five,
then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
Expand All @@ -16,7 +16,7 @@
"""


def solution(n):
def solution(n: int = 1000) -> int:
"""Returns the number of letters used to write all numbers from 1 to n.
where n is lower or equals to 1000.
>>> solution(1000)
Expand Down
7 changes: 3 additions & 4 deletions project_euler/problem_21/sol1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from math import sqrt

"""
Amicable Numbers
Problem 21
Expand All @@ -15,9 +13,10 @@

Evaluate the sum of all the amicable numbers under 10000.
"""
from math import sqrt


def sum_of_divisors(n):
def sum_of_divisors(n: int) -> int:
total = 0
for i in range(1, int(sqrt(n) + 1)):
if n % i == 0 and i != sqrt(n):
Expand All @@ -27,7 +26,7 @@ def sum_of_divisors(n):
return total - n


def solution(n):
def solution(n: int = 10000) -> int:
"""Returns the sum of all the amicable numbers under n.

>>> solution(10000)
Expand Down
23 changes: 12 additions & 11 deletions project_euler/problem_31/sol1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Coin sums
Problem 31
Problem 31: https://projecteuler.net/problem=31

In England the currency is made up of pound, £, and pence, p, and there are
eight coins in general circulation:

Expand All @@ -12,39 +13,39 @@
"""


def one_pence():
def one_pence() -> int:
return 1


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


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


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


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


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


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


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


def solution(n):
def solution(n: int = 200) -> int:
"""Returns the number of different ways can n pence be made using any number of
coins?

Expand All @@ -61,4 +62,4 @@ def solution(n):


if __name__ == "__main__":
print(solution(int(str(input()).strip())))
print(solution(int(input().strip())))
7 changes: 5 additions & 2 deletions project_euler/problem_31/sol2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
"""Coin sums
"""
Problem 31: https://projecteuler.net/problem=31

Coin sums

In England the currency is made up of pound, £, and pence, p, and there are
eight coins in general circulation:
Expand Down Expand Up @@ -29,7 +32,7 @@
"""


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

Expand Down
45 changes: 29 additions & 16 deletions project_euler/problem_33/sol1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""
Problem:
Problem 33: https://projecteuler.net/problem=33

The fraction 49/98 is a curious fraction, as an inexperienced
mathematician in attempting to simplify it may incorrectly believe
Expand All @@ -14,42 +14,55 @@
If the product of these four fractions is given in its lowest common
terms, find the value of the denominator.
"""
from fractions import Fraction
from typing import List


def isDigitCancelling(num, den):
def is_digit_cancelling(num: int, den: int) -> bool:
if num != den:
if num % 10 == den // 10:
if (num // 10) / (den % 10) == num / den:
return True
return False


def solve(digit_len: int) -> str:
def fraction_list(digit_len: int) -> List[str]:
"""
>>> solve(2)
'16/64 , 19/95 , 26/65 , 49/98'
>>> solve(3)
'16/64 , 19/95 , 26/65 , 49/98'
>>> solve(4)
'16/64 , 19/95 , 26/65 , 49/98'
>>> solve(0)
''
>>> solve(5)
'16/64 , 19/95 , 26/65 , 49/98'
>>> fraction_list(2)
['16/64', '19/95', '26/65', '49/98']
>>> fraction_list(3)
['16/64', '19/95', '26/65', '49/98']
>>> fraction_list(4)
['16/64', '19/95', '26/65', '49/98']
>>> fraction_list(0)
[]
>>> fraction_list(5)
['16/64', '19/95', '26/65', '49/98']
"""
solutions = []
den = 11
last_digit = int("1" + "0" * digit_len)
for num in range(den, last_digit):
while den <= 99:
if (num != den) and (num % 10 == den // 10) and (den % 10 != 0):
if isDigitCancelling(num, den):
if is_digit_cancelling(num, den):
solutions.append(f"{num}/{den}")
den += 1
num += 1
den = 10
solutions = " , ".join(solutions)
return solutions


def solution(n: int = 2) -> int:
"""
Return the solution to the problem
"""
result = 1.0
for fraction in fraction_list(n):
frac = Fraction(fraction)
result *= frac.denominator / frac.numerator
return int(result)


if __name__ == "__main__":
print(solve(2))
print(solution())
8 changes: 5 additions & 3 deletions project_euler/problem_43/sol1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Problem 43: https://projecteuler.net/problem=43

The number, 1406357289, is a 0 to 9 pandigital number because it is made up of
each of the digits 0 to 9 in some order, but it also has a rather interesting
sub-string divisibility property.
Expand Down Expand Up @@ -38,11 +40,11 @@ def is_substring_divisible(num: tuple) -> bool:
return True


def compute_sum(n: int = 10) -> int:
def solution(n: int = 10) -> int:
"""
Returns the sum of all pandigital numbers which pass the
divisiility tests.
>>> compute_sum(10)
>>> solution(10)
16695334890
"""
list_nums = [
Expand All @@ -55,4 +57,4 @@ def compute_sum(n: int = 10) -> int:


if __name__ == "__main__":
print(f"{compute_sum(10) = }")
print(f"{solution() = }")
8 changes: 5 additions & 3 deletions project_euler/problem_44/sol1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Problem 44: https://projecteuler.net/problem=44

Pentagonal numbers are generated by the formula, Pn=n(3n−1)/2. The first ten
pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
Expand All @@ -24,11 +26,11 @@ def is_pentagonal(n: int) -> bool:
return ((1 + root) / 6) % 1 == 0


def compute_num(limit: int = 5000) -> int:
def solution(limit: int = 5000) -> int:
"""
Returns the minimum difference of two pentagonal numbers P1 and P2 such that
P1 + P2 is pentagonal and P2 - P1 is pentagonal.
>>> compute_num(5000)
>>> solution(5000)
5482660
"""
pentagonal_nums = [(i * (3 * i - 1)) // 2 for i in range(1, limit)]
Expand All @@ -42,4 +44,4 @@ def compute_num(limit: int = 5000) -> int:


if __name__ == "__main__":
print(f"{compute_num() = }")
print(f"{solution() = }")
9 changes: 8 additions & 1 deletion project_euler/problem_46/sol1.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""
Problem 46: https://projecteuler.net/problem=46

It was proposed by Christian Goldbach that every odd composite number can be
written as the sum of a prime and twice a square.

Expand Down Expand Up @@ -84,5 +86,10 @@ def compute_nums(n: int) -> list[int]:
return list_nums


def solution() -> int:
"""Return the solution to the problem"""
return compute_nums(1)[0]


if __name__ == "__main__":
print(f"{compute_nums(1) = }")
print(f"{solution() = }")
4 changes: 2 additions & 2 deletions project_euler/problem_551/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def add(digits, k, addend):
digits.append(digit)


def solution(n):
def solution(n: int = 10 ** 15) -> int:
"""
returns n-th term of sequence

Expand Down Expand Up @@ -197,4 +197,4 @@ def solution(n):


if __name__ == "__main__":
print(solution(10 ** 15))
print(f"{solution() = }")
12 changes: 6 additions & 6 deletions project_euler/problem_63/sol1.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"""


def compute_nums(max_base: int = 10, max_power: int = 22) -> int:
def solution(max_base: int = 10, max_power: int = 22) -> int:
"""
Returns the count of all n-digit numbers which are nth power
>>> compute_nums(10, 22)
>>> solution(10, 22)
49
>>> compute_nums(0, 0)
>>> solution(0, 0)
0
>>> compute_nums(1, 1)
>>> solution(1, 1)
0
>>> compute_nums(-1, -1)
>>> solution(-1, -1)
0
"""
bases = range(1, max_base)
Expand All @@ -31,4 +31,4 @@ def compute_nums(max_base: int = 10, max_power: int = 22) -> int:


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