From 93abfbbd8e9d23f98f6ae10ec359c6388ed47370 Mon Sep 17 00:00:00 2001 From: Uczelnia Date: Mon, 16 Oct 2023 13:23:32 +0200 Subject: [PATCH] zad 1 i 2 --- TaskA01/run.py | 43 +++++++++++++++++++++++++++++++++++++++++++ TaskA02/run.py | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 TaskA01/run.py create mode 100644 TaskA02/run.py diff --git a/TaskA01/run.py b/TaskA01/run.py new file mode 100644 index 0000000..46a7b7d --- /dev/null +++ b/TaskA01/run.py @@ -0,0 +1,43 @@ +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) diff --git a/TaskA02/run.py b/TaskA02/run.py new file mode 100644 index 0000000..4e63a09 --- /dev/null +++ b/TaskA02/run.py @@ -0,0 +1,41 @@ +import os + + +NUMBER_OF_INVOICES = 10 +DIR = os.path.dirname(__file__) +PHRASE = "pies" + + +def join_path(filename: str) -> str: + return os.path.join(DIR, filename) + + +with open(join_path("simple.in"), "r", newline="", encoding="utf8") as file: + text = file.readlines() + + +def find_me(): + for j in range(len(PHRASE)): + if line[i + j] != PHRASE[j] and line[i + j] != PHRASE[j].capitalize(): + return False + + if line[i - 1] not in [" ", "\t", "\n"]: + return False + if line[i + len(PHRASE)] not in [" ", "\t", "\n", "\r"]: + return False + return True + + +found = True +phrase_array_slow = [] +for ind, line in enumerate(text): + for i in range(len(line)): + found = True + found = find_me() + + if found: + print(ind + 1, ":", line[i:], end="") + phrase_array_slow.append(ind + 1) + break + +# print(*phrase_array_slow, sep="\n")