diff --git a/Week01/info_Ibrahim_Ozel.py b/Week01/info_Ibrahim_Ozel.py new file mode 100644 index 00000000..f45a339c --- /dev/null +++ b/Week01/info_Ibrahim_Ozel.py @@ -0,0 +1,2 @@ +student_id = "220316056" +full_name = "İbrahim Özel" diff --git a/Week02/types_Ibrahim_Ozel.py b/Week02/types_Ibrahim_Ozel.py new file mode 100644 index 00000000..36c1facd --- /dev/null +++ b/Week02/types_Ibrahim_Ozel.py @@ -0,0 +1,4 @@ +my_int = 32 +my_float = 32.32 +my_bool = True +my_complex = 3j2 diff --git a/Week03/pyramid_Ibrahim_Ozel.py b/Week03/pyramid_Ibrahim_Ozel.py new file mode 100644 index 00000000..7322de28 --- /dev/null +++ b/Week03/pyramid_Ibrahim_Ozel.py @@ -0,0 +1,6 @@ +def calculate_pyramid_height(number_of_blocks): + height = 0 + while number_of_blocks >= height + 1: + height += 1 + number_of_blocks -= height + return height diff --git a/Week03/sequences_Ibrahim_Ozel.py b/Week03/sequences_Ibrahim_Ozel.py new file mode 100644 index 00000000..e68a82f4 --- /dev/null +++ b/Week03/sequences_Ibrahim_Ozel.py @@ -0,0 +1,29 @@ +def remove_duplicates(seq: list) -> list: + """ + Removes duplicate elements from a list while preserving order. + """ + unique_items = [] + for item in seq: + if item not in unique_items: + unique_items.append(item) + return unique_items + + +def list_counts(seq: list) -> dict: + """ + Counts the number of occurrences of each item in a list. + """ + counts = {} + for item in seq: + counts[item] = counts.get(item, 0) + 1 + return counts + + +def reverse_dict(d: dict) -> dict: + """ + Reverses the keys and values of a dictionary. + """ + reversed_dict = {} + for key, value in d.items(): + reversed_dict[value] = key + return reversed_dict diff --git a/Week04/decorators_Ibrahim_Ozel.py b/Week04/decorators_Ibrahim_Ozel.py new file mode 100644 index 00000000..a67d7b97 --- /dev/null +++ b/Week04/decorators_Ibrahim_Ozel.py @@ -0,0 +1,38 @@ +import tracemalloc +import time +from functools import wraps + + +def performance(func): + """ + Decorator to track function performance and record statistics. + + :param func: Function to decorate. + :type func: callable + :return: Wrapped function. + :rtype: callable + + :cvar counter: Number of calls. + :cvar total_time: Total execution time in seconds. + :cvar total_mem: Total peak memory in bytes. + """ + @wraps(func) + def wrapper(*args, **kwargs): + tracemalloc.start() + start_time = time.perf_counter() + try: + result = func(*args, **kwargs) + return result + finally: + end_time = time.perf_counter() + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + wrapper.counter += 1 + wrapper.total_time += (end_time - start_time) + wrapper.total_mem += peak + + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + return wrapper diff --git a/Week04/functions_Ibrahim_Ozel.py b/Week04/functions_Ibrahim_Ozel.py new file mode 100644 index 00000000..04cdb310 --- /dev/null +++ b/Week04/functions_Ibrahim_Ozel.py @@ -0,0 +1,37 @@ +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: + """ + This function raises x to the power of a, y to the power of b, + sums them, and then divides the result by c. + + :param x: The positional-only integer base parameter + :param y: The positional-only integer base parameter + :param a: The exponent for x + :param b: The exponent for y + :param c: The divisor (keyword-only) + :return: (x**a + y**b) / c + :rtype: float + """ + return (x ** a + y ** b) / c + + +def fn_w_counter() -> tuple[int, dict[str, int]]: + """ + Counts how many times this function is called and records + how many calls came from each module. + """ + if not hasattr(fn_w_counter, '_call_counter'): + fn_w_counter._call_counter = 0 + fn_w_counter._caller_dict = {} + + caller = __name__ + fn_w_counter._call_counter += 1 + + if caller in fn_w_counter._caller_dict: + fn_w_counter._caller_dict[caller] += 1 + else: + fn_w_counter._caller_dict[caller] = 1 + + return fn_w_counter._call_counter, fn_w_counter._caller_dict diff --git a/Week05/awaitme_Ibrahim_Ozel.py b/Week05/awaitme_Ibrahim_Ozel.py new file mode 100644 index 00000000..b62d8872 --- /dev/null +++ b/Week05/awaitme_Ibrahim_Ozel.py @@ -0,0 +1,11 @@ +import asyncio +from functools import wraps + +def awaitme(function): + @wraps(function) + async def wrapper(*args, **kwargs): + result = function(*args, **kwargs) + if asyncio.iscoroutine(result): + return await result + return result + return wrapper diff --git a/Week06/timer_Ibrahim_Ozel.py b/Week06/timer_Ibrahim_Ozel.py new file mode 100644 index 00000000..a27ca2e0 --- /dev/null +++ b/Week06/timer_Ibrahim_Ozel.py @@ -0,0 +1,21 @@ +import time + + +class Timer: + """ + A context manager class that measures the time taken + by the block of code it manages. + """ + + def __init__(self): + self.start_time = None + self.end_time = None + + def __enter__(self): + self.start_time = time.perf_counter() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end_time = time.perf_counter() + print(f"Elapsed time: {self.end_time - self.start_time:.6f} seconds") + return False