jfz-2023-s473555/TaskA01/run.py
2023-10-16 13:23:32 +02:00

44 lines
1.0 KiB
Python

import os
import time
NUMBER_OF_INVOICES = 10
DIR = os.path.dirname(__file__)
HAMLET = "Hamlet"
def join_path(filename: str) -> str:
return os.path.join(DIR, filename)
with open(join_path("shakespeare.in"), "r", newline="", encoding="utf8") as file:
text = file.readlines()
start_time = time.time()
found = True
hamlet_array_slow = []
for ind, line in enumerate(text):
for i in range(len(line)):
found = True
for j in range(len(HAMLET)):
if line[i + j] != HAMLET[j] and line[i + j] != HAMLET[j].capitalize():
found = False
break
if found:
# print(ind + 1, ":", line[i:], end="")
hamlet_array_slow.append(ind + 1)
break
# print(*hamlet_array_slow, sep="\n")
print(time.time() - start_time)
# time this function
start_time = time.time()
hamlet_array_fast = []
for ind, line in enumerate(text):
if HAMLET in line:
hamlet_array_fast.append(ind + 1)
print(time.time() - start_time)