Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Week04/decorators_cemil_koca.py
Original file line number Diff line number Diff line change
@@ -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
35 changes: 35 additions & 0 deletions Week04/functions_cemil_koca.py
Original file line number Diff line number Diff line change
@@ -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 = {}
7 changes: 7 additions & 0 deletions Week05/awaitme_cemil_koca.py
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions Week06/timer_cemil_koca.py
Original file line number Diff line number Diff line change
@@ -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()
44 changes: 44 additions & 0 deletions Week07/threaded_cemil_koca.py
Original file line number Diff line number Diff line change
@@ -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}")
Loading