44 lines
1.0 KiB
Python
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)
|