diff --git a/Week03/... b/Week03/... new file mode 100644 index 00000000..2c8c6f25 --- /dev/null +++ b/Week03/... @@ -0,0 +1,52 @@ +import os + + +files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("pyramid")] +for f in files: + exec("import " + f[:-3] + " as " + f[:-3]) + print(f"The module {f[:-3]} has been imported.") + + +def test_names(): + for f in files: + assert "calculate_pyramid_height" in dir(eval(f[:-3])), ( + "calculate_pyramid_height is not defined in " + f[:-3] + ) + + +def test_types(): + for f in files: + assert callable(eval(f[:-3]).calculate_pyramid_height), ( + "calculate_pyramid_height is not callable in " + f[:-3] + ) + assert isinstance(eval(f[:-3]).calculate_pyramid_height(1), int), ( + "calculate_pyramid_height is not returning an int in " + f[:-3] + ) + + +def test_calculate_pyramid_height(): + for f in files: + assert eval(f[:-3]).calculate_pyramid_height(1) == 1, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(2) == 1, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(6) == 3, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(20) == 5, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(100) == 13, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(1000) == 44, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(10000) == 140, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) + assert eval(f[:-3]).calculate_pyramid_height(100000) == 446, ( + "calculate_pyramid_height is not working in " + f[:-3] + ) diff --git a/Week03/pyramid_tarik_bozgan.py b/Week03/pyramid_tarik_bozgan.py new file mode 100644 index 00000000..a742a203 --- /dev/null +++ b/Week03/pyramid_tarik_bozgan.py @@ -0,0 +1,6 @@ +def calculate_pyramid_height(number_of_blocks): + height = 0 + while(number_of_blocks >= 0): + height += 1 + number_of_blocks -= height + return height - 1 \ No newline at end of file diff --git a/Week03/sequences_tarik_bozgan.py b/Week03/sequences_tarik_bozgan.py new file mode 100644 index 00000000..1ba02067 --- /dev/null +++ b/Week03/sequences_tarik_bozgan.py @@ -0,0 +1,23 @@ +def remove_duplicates(seq): + result = [] + for item in seq: + if item not in result: + result.append(item) + return result + + +def list_counts(seq): + counts = {} + for item in seq: + if item in counts: + counts[item] += 1 + else: + counts[item] = 1 + return counts + + +def reverse_dict(d): + reversed_dict = {} + for key, value in d.items(): + reversed_dict[value] = key + return reversed_dict \ No newline at end of file diff --git a/Week04/decorators_tarik_bozgan.py b/Week04/decorators_tarik_bozgan.py new file mode 100644 index 00000000..718e0a3b --- /dev/null +++ b/Week04/decorators_tarik_bozgan.py @@ -0,0 +1,26 @@ +import time +import tracemalloc + +def performance(func): + def wrapper(*args, **kwargs): + tracemalloc.start() + t1 = time.perf_counter() + + result = func(*args, **kwargs) + + t2 = time.perf_counter() + _, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + # Hata buradaydı: Sayaçları wrapper'a değil, performance fonksiyonuna ekliyoruz + performance.counter += 1 + performance.total_time += (t2 - t1) + performance.total_mem += peak + + return result + return wrapper + +# Testin beklediği özellikler (Attributes) ana fonksiyona atanmalı +performance.counter = 0 +performance.total_time = 0 +performance.total_mem = 0 diff --git a/Week04/functions_tarik_bozgan.py b/Week04/functions_tarik_bozgan.py new file mode 100644 index 00000000..60492966 --- /dev/null +++ b/Week04/functions_tarik_bozgan.py @@ -0,0 +1,24 @@ +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: + """ + :param x: + :param y: + :param a: + :param b: + :param c: + :return: + """ + if not isinstance(x, int): raise TypeError("x must be int") + if not isinstance(y, int): raise TypeError("y must be int") + if not isinstance(a, int): raise TypeError("a must be int") + if not isinstance(b, int): raise TypeError("b must be int") + if not isinstance(c, int): raise TypeError("c must be int") + + return float((x ** a + y ** b) / c) + +_count = 0 +def fn_w_counter() -> (int, dict[str, int]): + global _count + _count += 1 + return _count, {__name__: _count}