Fix problem number
This commit is contained in:
parent
a311ea75f8
commit
7190721c5f
@ -32,17 +32,28 @@ def compute():
|
||||
"""
|
||||
|
||||
primes = sorted(set(list_primes(1_000_000)) - set(list_primes(57_000)))
|
||||
digits = {'0':[], '1':[], '2':[],'3':[], '4':[], '5':[],'6':[], '7':[], '8':[], '9':[]}
|
||||
digits = {
|
||||
"0": [],
|
||||
"1": [],
|
||||
"2": [],
|
||||
"3": [],
|
||||
"4": [],
|
||||
"5": [],
|
||||
"6": [],
|
||||
"7": [],
|
||||
"8": [],
|
||||
"9": [],
|
||||
}
|
||||
for d in digits.keys():
|
||||
for p in primes:
|
||||
p = str(p)
|
||||
if p.count(d) == 3 and p[-1] != d:
|
||||
digits[d].append(p)
|
||||
for d in {'0', '1', '2'}:
|
||||
for d in {"0", "1", "2"}:
|
||||
for p in digits[d]:
|
||||
res = 0
|
||||
i = 10
|
||||
for D in {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}-{d}:
|
||||
for D in {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"} - {d}:
|
||||
i -= 1
|
||||
q = int(p.replace(d, D))
|
||||
if is_prime(q) and q > 57_000:
|
||||
@ -55,4 +66,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(51):003d}: {compute()}")
|
||||
print(f"Result for Problem 51: {compute()}")
|
||||
|
@ -23,14 +23,17 @@ def compute():
|
||||
"""
|
||||
|
||||
for number in range(123456, 1_000_000):
|
||||
if sorted(str(number)) \
|
||||
== sorted(str(2*number)) \
|
||||
== sorted(str(3*number)) \
|
||||
== sorted(str(4*number)) \
|
||||
== sorted(str(5*number)) == sorted(str(6*number)):
|
||||
if (
|
||||
sorted(str(number))
|
||||
== sorted(str(2 * number))
|
||||
== sorted(str(3 * number))
|
||||
== sorted(str(4 * number))
|
||||
== sorted(str(5 * number))
|
||||
== sorted(str(6 * number))
|
||||
):
|
||||
return number
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(52):003d}: {compute()}")
|
||||
print(f"Result for Problem 52: {compute()}")
|
||||
|
@ -41,4 +41,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(53):003d}: {compute()}")
|
||||
print(f"Result for Problem 53: {compute()}")
|
||||
|
@ -79,7 +79,6 @@ def compute():
|
||||
def to_numerical(hand: list) -> list:
|
||||
return sorted([int(x[:-1]) for x in hand], reverse=True)
|
||||
|
||||
|
||||
# 10 Ranks functions.
|
||||
def high_card(str_hand: list) -> list:
|
||||
return to_numerical(str_hand)
|
||||
@ -113,7 +112,6 @@ def compute():
|
||||
def royal_flush(str_hand: list) -> bool:
|
||||
return flush(str_hand) and list(range(14, 9, -1)) == to_numerical(str_hand)
|
||||
|
||||
|
||||
replace_map = {"T": 10, "J": 11, "Q": 12, "K": 13, "A": 14}
|
||||
score = [0, 0]
|
||||
|
||||
@ -121,10 +119,25 @@ def compute():
|
||||
for line in open(file, "r").read().splitlines():
|
||||
line = replace_values_in_string(line, replace_map).split()
|
||||
hands = line[:5], line[5:]
|
||||
for rank in (royal_flush, straight_flush, four_of_a_kind, full_house, flush,
|
||||
straight, three_of_a_kind, two_pair, one_pair, high_card):
|
||||
should_convert_hand = "str" not in rank.__code__.co_varnames[0] # Checks parameter name.
|
||||
result = [rank(to_numerical(hand) if should_convert_hand else hand) for hand in hands]
|
||||
for rank in (
|
||||
royal_flush,
|
||||
straight_flush,
|
||||
four_of_a_kind,
|
||||
full_house,
|
||||
flush,
|
||||
straight,
|
||||
three_of_a_kind,
|
||||
two_pair,
|
||||
one_pair,
|
||||
high_card,
|
||||
):
|
||||
should_convert_hand = (
|
||||
"str" not in rank.__code__.co_varnames[0]
|
||||
) # Checks parameter name.
|
||||
result = [
|
||||
rank(to_numerical(hand) if should_convert_hand else hand)
|
||||
for hand in hands
|
||||
]
|
||||
if result[0] != result[1]:
|
||||
score[0 if result[0] > result[1] else 1] += 1
|
||||
break
|
||||
@ -134,4 +147,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(54):003d}: {compute()}")
|
||||
print(f"Result for Problem 54: {compute()}")
|
||||
|
@ -63,4 +63,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(55):003d}: {compute()}")
|
||||
print(f"Result for Problem 55: {compute()}")
|
||||
|
@ -35,4 +35,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(56):003d}: {compute()}")
|
||||
print(f"Result for Problem 56: {compute()}")
|
||||
|
@ -47,4 +47,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(57):003d}: {compute()}")
|
||||
print(f"Result for Problem 57: {compute()}")
|
||||
|
@ -53,4 +53,4 @@ def compute():
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(58):003d}: {compute()}")
|
||||
print(f"Result for Problem 58: {compute()}")
|
||||
|
@ -45,9 +45,9 @@ def compute():
|
||||
values in the original text.
|
||||
"""
|
||||
|
||||
with open('../files/Problem59.txt', 'r') as f:
|
||||
with open("../files/Problem59.txt", "r") as f:
|
||||
# encrypted = list(map(int, f.read().split(',')))
|
||||
encrypted = [int(char) for char in f.read().split(',')]
|
||||
encrypted = [int(char) for char in f.read().split(",")]
|
||||
# print(encrypted)
|
||||
# print(test)
|
||||
plain_text = len(encrypted) // 3
|
||||
@ -57,10 +57,10 @@ def compute():
|
||||
decrypted += chr(ord(k) ^ i)
|
||||
|
||||
# assuming Euler will be in the text
|
||||
if 'Euler' in decrypted:
|
||||
if "Euler" in decrypted:
|
||||
return sum([ord(c) for c in decrypted])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
print(f"Result for Problem {int(59):003d}: {compute()}")
|
||||
print(f"Result for Problem 59: {compute()}")
|
||||
|
48
src/Python/Problems001-050/utils.py
Normal file
48
src/Python/Problems001-050/utils.py
Normal file
@ -0,0 +1,48 @@
|
||||
import math
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def timeit(name):
|
||||
def profile(original):
|
||||
import time
|
||||
@wraps(original)
|
||||
def wrapper(*args, **kwargs):
|
||||
t0 = time.perf_counter()
|
||||
result = original(*args, **kwargs)
|
||||
t1 = time.perf_counter()
|
||||
print(f"Time to evaluate problem {int(name[7:]):003d}: {(t1 - t0)*1000:.3f} ms\n")
|
||||
return result
|
||||
return wrapper
|
||||
return profile
|
||||
|
||||
|
||||
def is_prime(n):
|
||||
if n <2:
|
||||
return False
|
||||
for i in range(2, int(math.sqrt(n)) + 1):
|
||||
if n % i == 0:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
# Returns a list of True and False indicating whether each number is prime.
|
||||
# For 0 <= i <= n, result[i] is True if i is a prime number, False otherwise.
|
||||
def list_primality(n):
|
||||
# Sieve of Eratosthenes
|
||||
result = [True] * (n + 1)
|
||||
result[0] = result[1] = False
|
||||
for i in range(int(math.sqrt(n) + 1)):
|
||||
if result[i]:
|
||||
for j in range(i * i, len(result), i):
|
||||
result[j] = False
|
||||
return result
|
||||
|
||||
|
||||
# Returns all the prime numbers less than or equal to n, in ascending order
|
||||
# For example: list_primes(97) = [2, 3, 5, 7, 11, ..., 83, 89, 97].
|
||||
def list_primes(n):
|
||||
return [i for (i, is_prime) in enumerate(list_primality(n)) if is_prime]
|
||||
|
||||
|
||||
def is_palindrome(num):
|
||||
return str(num) == str(num)[::-1]
|
Loading…
x
Reference in New Issue
Block a user