Skip to content

Commit 4727024

Browse files
committed
Apply review suggestions
1 parent 0abd5db commit 4727024

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Diff for: project_euler/problem_51/sol1.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from collections import Counter
2-
31
"""
42
https://projecteuler.net/problem=51
53
Prime digit replacements
@@ -18,17 +16,20 @@
1816
adjacent digits) with the same digit, is part of an eight prime value family.
1917
"""
2018

19+
from collections import Counter
20+
from typing import List
2121

22-
def sieve(n: int) -> list:
22+
23+
def prime_sieve(n: int) -> List[int]:
2324
"""
2425
Sieve of Erotosthenes
2526
Function to return all the prime numbers up to a certain number
2627
https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
2728
28-
>>> sieve(3)
29+
>>> prime_sieve(3)
2930
[2]
3031
31-
>>> sieve(50)
32+
>>> prime_sieve(50)
3233
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
3334
"""
3435
is_prime = [True] * n
@@ -51,7 +52,7 @@ def sieve(n: int) -> list:
5152
return primes
5253

5354

54-
def digit_replacements(number: int) -> list:
55+
def digit_replacements(number: int) -> List[List[int]]:
5556
"""
5657
Returns all the possible families of digit replacements in a number which
5758
contains at least one repeating digit
@@ -73,18 +74,20 @@ def digit_replacements(number: int) -> list:
7374
return replacements
7475

7576

76-
def solution() -> int:
77+
def solution(family_length: int = 8) -> int:
7778
"""
7879
Returns the solution of the problem
7980
80-
>>> solution()
81-
121313
81+
>>> solution(2)
82+
229399
83+
84+
>>> solution(3)
85+
221311
8286
"""
8387
numbers_checked = set()
84-
primes = set(sieve(1_000_000))
8588

8689
# Filter primes with less than 3 replaceable digits
87-
primes = {x for x in primes if len(str(x)) - len(set(str(x))) >= 3}
90+
primes = {x for x in set(prime_sieve(1_000_000)) if len(str(x)) - len(set(str(x))) >= 3}
8891

8992
for prime in primes:
9093
if prime in numbers_checked:
@@ -96,7 +99,7 @@ def solution() -> int:
9699
numbers_checked.update(family)
97100
primes_in_family = primes.intersection(family)
98101

99-
if len(primes_in_family) != 8:
102+
if len(primes_in_family) != family_length:
100103
continue
101104

102105
return min(primes_in_family)

0 commit comments

Comments
 (0)