diff --git a/Week04/decorators_cemil_koca.py b/Week04/decorators_cemil_koca.py new file mode 100644 index 00000000..bbb1fa96 --- /dev/null +++ b/Week04/decorators_cemil_koca.py @@ -0,0 +1,25 @@ +import time +import tracemalloc + +def performance(fn): + def _performance(*args, **kwargs): # wraps yok, düz wrapper + performance.counter += 1 + + tracemalloc.start() + start = time.perf_counter() + + result = fn(*args, **kwargs) + + elapsed = time.perf_counter() - start + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.total_time += elapsed + performance.total_mem += peak + + return result + return _performance + +performance.counter = 0 +performance.total_time = 0 +performance.total_mem = 0 \ No newline at end of file diff --git a/Week04/functions_cemil_koca.py b/Week04/functions_cemil_koca.py new file mode 100644 index 00000000..065e06ad --- /dev/null +++ b/Week04/functions_cemil_koca.py @@ -0,0 +1,35 @@ +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: + """Calculate a custom equation. + + :param x: Base value 1. + :type x: int + :param y: Base value 2. + :type y: int + :param a: Exponent for x. + :type a: int + :param b: Exponent for y. + :type b: int + :param c: Divisor. + :type c: int + :return: Result of (x**a + y**b) / c. + :rtype: float + """ + for param in (x, y, a, b, c): + if not isinstance(param, int) or isinstance(param, bool): + raise TypeError("All parameters must be integers.") + return (x**a + y**b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + """Count function calls with caller information.""" + fn_w_counter.total += 1 + caller_name = __name__ + fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 + return (fn_w_counter.total, dict(fn_w_counter.callers)) + + +fn_w_counter.total = 0 +fn_w_counter.callers = {} diff --git a/Week05/awaitme_cemil_koca.py b/Week05/awaitme_cemil_koca.py new file mode 100644 index 00000000..6a2ecb10 --- /dev/null +++ b/Week05/awaitme_cemil_koca.py @@ -0,0 +1,7 @@ +def awaitme(fn): + async def _wrapper(*args, **kwargs): + return fn(*args, **kwargs) + _wrapper.__name__ = fn.__name__ + _wrapper.__doc__ = fn.__doc__ + _wrapper.__annotations__ = fn.__annotations__ + return _wrapper \ No newline at end of file diff --git a/Week06/timer_cemil_koca.py b/Week06/timer_cemil_koca.py new file mode 100644 index 00000000..3f13f670 --- /dev/null +++ b/Week06/timer_cemil_koca.py @@ -0,0 +1,13 @@ +import time + +class Timer: + def __init__(self): + self.start_time = None + self.end_time = None + + def __enter__(self): + self.start_time = time.time() + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.end_time = time.time() \ No newline at end of file diff --git a/Week07/threaded_cemil_koca.py b/Week07/threaded_cemil_koca.py new file mode 100644 index 00000000..c51f0b41 --- /dev/null +++ b/Week07/threaded_cemil_koca.py @@ -0,0 +1,44 @@ +import threading +import random + +def threaded(n): + def decorator(func): + def wrapper(*args, **kwargs): + threads = [threading.Thread(target=func, args=args, kwargs=kwargs) for _ in range(n)] + for t in threads: + t.start() + for t in threads: + t.join() + return wrapper + return decorator + +total_points = 0 +inside_circle = 0 +lock = threading.Lock() + +NUM_POINTS_PER_THREAD = 100000 +NUM_THREADS = 4 + +@threaded(NUM_THREADS) +def estimate_pi(): + global total_points, inside_circle + local_total = 0 + local_inside = 0 + + for _ in range(NUM_POINTS_PER_THREAD): + x = random.uniform(-1, 1) + y = random.uniform(-1, 1) + if x * x + y * y <= 1: + local_inside += 1 + local_total += 1 + + with lock: + total_points += local_total + inside_circle += local_inside + +if __name__ == "__main__": + estimate_pi() + pi_estimate = 4 * inside_circle / total_points + print(f"Total points: {total_points}") + print(f"Points inside circle: {inside_circle}") + print(f"Estimated Pi: {pi_estimate}") \ No newline at end of file