Add type hints in problems 55-60
This commit is contained in:
parent
ac15f7ddf2
commit
f9aa2edbd2
@ -26,7 +26,7 @@
|
|||||||
from projecteuler import is_palindrome, timing
|
from projecteuler import is_palindrome, timing
|
||||||
|
|
||||||
|
|
||||||
def is_lychrel(n):
|
def is_lychrel(n: int) -> bool:
|
||||||
tmp = n
|
tmp = n
|
||||||
|
|
||||||
# Run for 50 iterations
|
# Run for 50 iterations
|
||||||
@ -52,7 +52,7 @@ def is_lychrel(n):
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p055():
|
def p055() -> None:
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
# For each number, use the is_lychrel function to check if the number
|
# For each number, use the is_lychrel function to check if the number
|
||||||
|
@ -9,7 +9,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p056():
|
def p056() -> None:
|
||||||
max_ = 0
|
max_ = 0
|
||||||
|
|
||||||
# Straightforward brute force approach
|
# Straightforward brute force approach
|
||||||
|
@ -20,7 +20,7 @@ from projecteuler import timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p057():
|
def p057() -> None:
|
||||||
n = 1
|
n = 1
|
||||||
d = 1
|
d = 1
|
||||||
count = 0
|
count = 0
|
||||||
|
@ -20,7 +20,7 @@ from projecteuler import is_prime, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p058():
|
def p058() -> None:
|
||||||
# Starting with 1, the next four numbers in the diagonal are 3 (1+2), 5 (1+2+2), 7 (1+2+2+2)
|
# Starting with 1, the next four numbers in the diagonal are 3 (1+2), 5 (1+2+2), 7 (1+2+2+2)
|
||||||
# and 9 (1+2+2+2+2). Check which are prime, increment the counter every time a new prime is
|
# and 9 (1+2+2+2+2). Check which are prime, increment the counter every time a new prime is
|
||||||
# found, and divide by the number of elements of the diagonal, which are increase by 4 at
|
# found, and divide by the number of elements of the diagonal, which are increase by 4 at
|
||||||
|
@ -19,19 +19,19 @@
|
|||||||
# ASCII values in the original text.
|
# ASCII values in the original text.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from projecteuler import timing
|
from projecteuler import timing
|
||||||
|
|
||||||
|
|
||||||
class EncryptedText():
|
class EncryptedText():
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self) -> None:
|
||||||
super().__init__(*args, **kwargs)
|
self.text: List[int]
|
||||||
|
self.text = []
|
||||||
self.text = None
|
|
||||||
self.len = 0
|
self.len = 0
|
||||||
|
|
||||||
def read_text(self, filename):
|
def read_text(self, filename: str) -> int:
|
||||||
try:
|
try:
|
||||||
with open(filename, 'r', encoding='utf-8') as fp:
|
with open(filename, 'r', encoding='utf-8') as fp:
|
||||||
self.text = list(map(int, list(fp.readline().split(','))))
|
self.text = list(map(int, list(fp.readline().split(','))))
|
||||||
@ -44,7 +44,7 @@ class EncryptedText():
|
|||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def decrypt(self):
|
def decrypt(self) -> str:
|
||||||
found = 0
|
found = 0
|
||||||
|
|
||||||
for c1 in range(ord('a'), ord('z')+1):
|
for c1 in range(ord('a'), ord('z')+1):
|
||||||
@ -72,17 +72,17 @@ class EncryptedText():
|
|||||||
if i == self.len - 1:
|
if i == self.len - 1:
|
||||||
plain_text[i] = str(chr(self.text[i] ^ c1))
|
plain_text[i] = str(chr(self.text[i] ^ c1))
|
||||||
|
|
||||||
plain_text = ''.join(plain_text)
|
plain_text_str = ''.join(plain_text)
|
||||||
|
|
||||||
if 'the' in plain_text and 'be' in plain_text and 'to' in plain_text and 'of' in plain_text and\
|
if 'the' in plain_text_str and 'be' in plain_text_str and 'to' in plain_text_str and 'of' in plain_text_str and\
|
||||||
'and' in plain_text and 'in' in plain_text and 'that' in plain_text and 'have' in plain_text:
|
'and' in plain_text_str and 'in' in plain_text_str and 'that' in plain_text_str and 'have' in plain_text_str:
|
||||||
found = 1
|
found = 1
|
||||||
|
|
||||||
return plain_text
|
return plain_text_str
|
||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p059():
|
def p059() -> None:
|
||||||
enc_text = EncryptedText()
|
enc_text = EncryptedText()
|
||||||
|
|
||||||
if enc_text.read_text('p059_cipher.txt') == -1:
|
if enc_text.read_text('p059_cipher.txt') == -1:
|
||||||
|
@ -10,7 +10,7 @@ from projecteuler import is_prime, sieve, timing
|
|||||||
|
|
||||||
|
|
||||||
@timing
|
@timing
|
||||||
def p060():
|
def p060() -> None:
|
||||||
N = 10000
|
N = 10000
|
||||||
|
|
||||||
primes = sieve(N)
|
primes = sieve(N)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user