diff --git a/Week03/pyramid_mehmet_hamza_ogretmis.py b/Week03/pyramid_mehmet_hamza_ogretmis.py new file mode 100644 index 00000000..2042bfa3 --- /dev/null +++ b/Week03/pyramid_mehmet_hamza_ogretmis.py @@ -0,0 +1,7 @@ +def calculate_pyramid_height(number_of_blocks): + height = 0 + blocks = 0 + while blocks + height < number_of_blocks: + height += 1 + blocks += height + return height \ No newline at end of file diff --git a/Week03/sequences_mehmet_hamza_ogretmis.py b/Week03/sequences_mehmet_hamza_ogretmis.py new file mode 100644 index 00000000..774f54f7 --- /dev/null +++ b/Week03/sequences_mehmet_hamza_ogretmis.py @@ -0,0 +1,34 @@ +def remove_duplicates(seq: list) -> list: + """ + This function removes duplicates from a list + """ + result = [] + for item in seq: + if item not in result: + result.append(item) + return result + + +def list_counts(seq: list) -> dict: + """ + This function counts the number of occurences + of each item in a list + """ + counts = {} + for item in seq: + if item in counts: + counts[item] += 1 + else: + counts[item] = 1 + return counts + + +def reverse_dict(d: dict) -> dict: + """ + This function reverses the keys and values + of a dictionary + """ + new_d = {} + for key, value in d.items(): + new_d[value] = key + return new_d \ No newline at end of file diff --git a/Week04/decorators_mehmet_hamza_ogretmis.py b/Week04/decorators_mehmet_hamza_ogretmis.py new file mode 100644 index 00000000..2d911d8a --- /dev/null +++ b/Week04/decorators_mehmet_hamza_ogretmis.py @@ -0,0 +1,29 @@ +import time +import tracemalloc + + +def performance(func): + """Measure the time and memory each call of func takes and keep running totals.""" + + def wrapper(*args, **kwargs): + tracemalloc.start() + start = time.perf_counter() + + result = func(*args, **kwargs) + + elapsed = time.perf_counter() - start + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.counter += 1 + performance.total_time += elapsed + performance.total_mem += peak + + return result + + return wrapper + + +performance.counter = 0 +performance.total_time = 0 +performance.total_mem = 0 \ No newline at end of file diff --git a/Week04/functions_mehmet_hamza_ogretmis.py b/Week04/functions_mehmet_hamza_ogretmis.py new file mode 100644 index 00000000..1443e58e --- /dev/null +++ b/Week04/functions_mehmet_hamza_ogretmis.py @@ -0,0 +1,32 @@ +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: + """ + Compute (x**a + y**b) / c. + + :param x: First base, positional-only + :type x: int + :param y: Second base, positional-only + :type y: int + :param a: Exponent of x + :type a: int + :param b: Exponent of y + :type b: int + :param c: Divisor, keyword-only + :type c: int + :return: The result of (x**a + y**b) / c + :rtype: float + """ + return (x ** a + y ** b) / c + + +def fn_w_counter() -> (int, dict[str, int]): + fn_w_counter.total += 1 + fn_w_counter.callers[__name__] = fn_w_counter.callers.get(__name__, 0) + 1 + + return fn_w_counter.total, dict(fn_w_counter.callers) + + +fn_w_counter.total = 0 +fn_w_counter.callers = {} \ No newline at end of file