From f9aa2edbd289ebad8d403ff1be016e317c6b5d27 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sun, 10 Nov 2024 11:06:21 +0100 Subject: [PATCH] Add type hints in problems 55-60 --- Python/p055.py | 4 ++-- Python/p056.py | 2 +- Python/p057.py | 2 +- Python/p058.py | 2 +- Python/p059.py | 22 +++++++++++----------- Python/p060.py | 2 +- 6 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Python/p055.py b/Python/p055.py index c7f6953..1c5a000 100644 --- a/Python/p055.py +++ b/Python/p055.py @@ -26,7 +26,7 @@ from projecteuler import is_palindrome, timing -def is_lychrel(n): +def is_lychrel(n: int) -> bool: tmp = n # Run for 50 iterations @@ -52,7 +52,7 @@ def is_lychrel(n): @timing -def p055(): +def p055() -> None: count = 0 # For each number, use the is_lychrel function to check if the number diff --git a/Python/p056.py b/Python/p056.py index 706b4f7..5e482ee 100644 --- a/Python/p056.py +++ b/Python/p056.py @@ -9,7 +9,7 @@ from projecteuler import timing @timing -def p056(): +def p056() -> None: max_ = 0 # Straightforward brute force approach diff --git a/Python/p057.py b/Python/p057.py index 2338860..48a1006 100644 --- a/Python/p057.py +++ b/Python/p057.py @@ -20,7 +20,7 @@ from projecteuler import timing @timing -def p057(): +def p057() -> None: n = 1 d = 1 count = 0 diff --git a/Python/p058.py b/Python/p058.py index 2d6b6c7..0700b9e 100644 --- a/Python/p058.py +++ b/Python/p058.py @@ -20,7 +20,7 @@ from projecteuler import is_prime, 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) # 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 diff --git a/Python/p059.py b/Python/p059.py index 908cc33..d2d92e4 100644 --- a/Python/p059.py +++ b/Python/p059.py @@ -19,19 +19,19 @@ # ASCII values in the original text. import sys +from typing import List from projecteuler import timing class EncryptedText(): - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - - self.text = None + def __init__(self) -> None: + self.text: List[int] + self.text = [] self.len = 0 - def read_text(self, filename): + def read_text(self, filename: str) -> int: try: with open(filename, 'r', encoding='utf-8') as fp: self.text = list(map(int, list(fp.readline().split(',')))) @@ -44,7 +44,7 @@ class EncryptedText(): return 0 - def decrypt(self): + def decrypt(self) -> str: found = 0 for c1 in range(ord('a'), ord('z')+1): @@ -72,17 +72,17 @@ class EncryptedText(): if i == self.len - 1: 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\ - 'and' in plain_text and 'in' in plain_text and 'that' in plain_text and 'have' in plain_text: + 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_str and 'in' in plain_text_str and 'that' in plain_text_str and 'have' in plain_text_str: found = 1 - return plain_text + return plain_text_str @timing -def p059(): +def p059() -> None: enc_text = EncryptedText() if enc_text.read_text('p059_cipher.txt') == -1: diff --git a/Python/p060.py b/Python/p060.py index 8a1ae92..e9c6dac 100644 --- a/Python/p060.py +++ b/Python/p060.py @@ -10,7 +10,7 @@ from projecteuler import is_prime, sieve, timing @timing -def p060(): +def p060() -> None: N = 10000 primes = sieve(N)