diff --git a/Week03/sequences_cem_ayyildiz.py b/Week03/sequences_cem_ayyildiz.py new file mode 100644 index 00000000..8d4017de --- /dev/null +++ b/Week03/sequences_cem_ayyildiz.py @@ -0,0 +1,15 @@ +def remove_duplicates(seq: list) -> list: + result = [] + for item in seq: + if item not in result: + result.append(item) + return result + +def list_counts(seq: list) -> dict: + counts = {} + for item in seq: + counts[item] = counts.get(item, 0) + 1 + return counts + +def reverse_dict(d: dict) -> dict: + return {v: k for k, v in d.items()} diff --git a/Week04/decorators_cem_ayyildiz.py b/Week04/decorators_cem_ayyildiz.py new file mode 100644 index 00000000..309a0706 --- /dev/null +++ b/Week04/decorators_cem_ayyildiz.py @@ -0,0 +1,30 @@ +import time +import tracemalloc + +def performance(f): + if not hasattr(performance, "counter"): + performance.counter = 0 + if not hasattr(performance, "total_time"): + performance.total_time = 0.0 + if not hasattr(performance, "total_mem"): + performance.total_mem = 0 + + def _performance(*args, **kwargs): + + performance.counter += 1 + + tracemalloc.start() + x = time.time() + + + ret = f(*args, **kwargs) + + + performance.total_time += time.time() - x + + performance.total_mem = tracemalloc.get_traced_memory()[1] + + tracemalloc.stop() + + return ret + return _performance diff --git a/Week04/functions_cem_ayyildiz.py b/Week04/functions_cem_ayyildiz.py new file mode 100644 index 00000000..64c7ae18 --- /dev/null +++ b/Week04/functions_cem_ayyildiz.py @@ -0,0 +1,34 @@ +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 the equation (x**a + y**b) / c. + + :param x: The base of the first term, positional-only + :type x: int + :param y: The base of the second term, positional-only + :type y: int + :param a: The exponent of the first term + :type a: int + :param b: The exponent of the second term + :type b: int + :param c: The divisor, keyword-only + :type c: int + :return: The result of (x**a + y**b) / c + + """ + + return (x**a + y**b) / c + + +_total_calls = 0 +_calls_by_caller = {} + + +def fn_w_counter() -> (int, dict[str, int]): + + global _total_calls + _total_calls += 1 + _calls_by_caller[__name__] = _calls_by_caller.get(__name__, 0) + 1 + return _total_calls, dict(_calls_by_caller) diff --git a/Week05/email_cem_ayyildiz.py b/Week05/email_cem_ayyildiz.py new file mode 100644 index 00000000..b2ec235b --- /dev/null +++ b/Week05/email_cem_ayyildiz.py @@ -0,0 +1,37 @@ + +import re + + +class Emails(list): + + def __init__(self, emails): + self.data = [] + + for email in emails: + if not isinstance(email, str): + raise ValueError("Only strings are allowed") + + self.validate(email) + + if email not in self.data: + self.data.append(email) + + super().__init__(self.data) + + @staticmethod + def validate(email): + if not isinstance(email, str): + raise ValueError("Email must be a string") + + pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' + + if not re.match(pattern, email): + raise ValueError("Invalid email address") + + return True + + def __repr__(self): + return f"Emails({self.data!r})" + + def __str__(self): + return ", ".join(self.data)