diff --git a/Week03/pyramid_omer_kayaalp.py b/Week03/pyramid_omer_kayaalp.py new file mode 100644 index 00000000..cc0e189c --- /dev/null +++ b/Week03/pyramid_omer_kayaalp.py @@ -0,0 +1,8 @@ +def calculate_pyramid_height(number_of_blocks): + height = 0 + needed = 1 + while number_of_blocks >= needed: + number_of_blocks -= needed + height += 1 + needed += 1 + return height diff --git a/Week03/sequences_omer_kayaalp.py b/Week03/sequences_omer_kayaalp.py new file mode 100644 index 00000000..fb242979 --- /dev/null +++ b/Week03/sequences_omer_kayaalp.py @@ -0,0 +1,26 @@ +def remove_duplicates(seq: list) -> list: + result = [] + + for item in seq: + if item not in result: + result.append(item) + return result + +#calculate the repeated ones +def list_counts(seq: list) -> dict: + counts = {} + + for item in seq: + if item in counts: + counts[item] += 1 + else: + counts[item] = 1 + return counts + +#Swap the keys and values of a dictionary +def reverse_dict(d: dict) -> dict: + reversed_dict = {} + + for key, value in d.items(): + reversed_dict[value] = key + return reversed_dict diff --git a/Week04/decorators_omer_kayaalp.py b/Week04/decorators_omer_kayaalp.py new file mode 100644 index 00000000..9d4514b8 --- /dev/null +++ b/Week04/decorators_omer_kayaalp.py @@ -0,0 +1,58 @@ +import functools +import time +import tracemalloc + + +def performance(fn): + """ + Decorator that measures execution time and memory of the wrapped function. + + Statistics are stored as attributes on the decorator itself: + + - ``performance.counter``: number of decorated-function calls + - ``performance.total_time``: total time spent in seconds + - ``performance.total_mem``: total memory consumed in bytes + + :param fn: the function to decorate + :type fn: callable + :return: the wrapped function + :rtype: callable + """ + + @functools.wraps(fn) + def wrapper(*args, **kwargs): + started_here = not tracemalloc.is_tracing() + if started_here: + tracemalloc.start() + tracemalloc.reset_peak() + mem_before = tracemalloc.get_traced_memory()[0] + start = time.perf_counter() + + try: + return fn(*args, **kwargs) + finally: + elapsed = time.perf_counter() - start + peak = tracemalloc.get_traced_memory()[1] + if started_here: + tracemalloc.stop() + performance.counter += 1 + performance.total_time += elapsed + performance.total_mem += max(peak - mem_before, 0) + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 + + +if __name__ == "__main__": + + @performance + def build_list(n: int) -> list[int]: + return [i * i for i in range(n)] + + build_list(100_000) + build_list(50_000) + print(performance.counter, performance.total_time, performance.total_mem) diff --git a/Week04/functions_omer_kayaalp.py b/Week04/functions_omer_kayaalp.py new file mode 100644 index 00000000..16a9a3d9 --- /dev/null +++ b/Week04/functions_omer_kayaalp.py @@ -0,0 +1,33 @@ +custom_power = lambda x=0, /, e=1: x**e + +def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: + """ + Calculates a custom equation. + + :param x: Positional-only integer, defaults to 0. + :param y: Positional-only integer, defaults to 0. + :param a: Positional-or-keyword integer, defaults to 1. + :param b: Positional-or-keyword integer, defaults to 1. + :param c: Keyword-only integer, defaults to 1. + :return: The result of (x**a + y**b) / c as float. + """ + if not all(isinstance(val, int) for val in (x, y, a, b, c)): + raise TypeError("Arguments must be integers.") + + return float((x**a + y**b) / c) + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, "total_calls"): + fn_w_counter.total_calls = 0 + fn_w_counter.caller_counts = {} + + caller_name = __name__ + + fn_w_counter.total_calls += 1 + + if caller_name in fn_w_counter.caller_counts: + fn_w_counter.caller_counts[caller_name] += 1 + else: + fn_w_counter.caller_counts[caller_name] = 1 + + return (fn_w_counter.total_calls, fn_w_counter.caller_counts) diff --git a/Week05/emails_omer_kayaalp.py b/Week05/emails_omer_kayaalp.py new file mode 100644 index 00000000..37bbcc49 --- /dev/null +++ b/Week05/emails_omer_kayaalp.py @@ -0,0 +1,30 @@ +class Emails(list): + def __init__(self, email_list): + checked = self.validate(email_list) + super().__init__(checked) + self.data = checked + + def validate(self, email_list): + result = [] + for item in email_list: + if type(item) is not str: + raise ValueError("Only strings are allowed") + if item.count("@") != 1: + raise ValueError("Invalid email: " + item) + user, domain = item.split("@") + if user == "" or "." not in domain: + raise ValueError("Invalid email: " + item) + if domain.startswith(".") or domain.endswith("."): + raise ValueError("Invalid email: " + item) + if item not in result: + result.append(item) + return result + + def __repr__(self): + return "Emails(" + str(list(self)) + ")" + + def __str__(self): + text = "Emails:\n" + for i, mail in enumerate(self, start=1): + text += f"{i}. {mail}\n" + return text