diff --git a/Week02/types_hilmican_karadag.py b/Week02/types_hilmican_karadag.py new file mode 100644 index 000000000..2241fa306 --- /dev/null +++ b/Week02/types_hilmican_karadag.py @@ -0,0 +1,4 @@ +my_int = 10 +my_float = 3.14 +my_bool = True +my_complex = 3 + 9j \ No newline at end of file diff --git a/Week03/pyramid_hilmican_karadag.py b/Week03/pyramid_hilmican_karadag.py new file mode 100644 index 000000000..ac844f9b0 --- /dev/null +++ b/Week03/pyramid_hilmican_karadag.py @@ -0,0 +1,10 @@ +def calculate_pyramid_height(number_of_blocks): + height = 0 + blocks_needed = 1 + + while number_of_blocks >= blocks_needed: + number_of_blocks -= blocks_needed + height += 1 + blocks_needed += 1 + + return height diff --git a/Week03/sequences_hilmican_karadag.py b/Week03/sequences_hilmican_karadag.py new file mode 100644 index 000000000..13d3168bf --- /dev/null +++ b/Week03/sequences_hilmican_karadag.py @@ -0,0 +1,26 @@ +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 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: + """ + This function reverses the keys and values of a dictionary. + """ + return {value: key for key, value in d.items()} \ No newline at end of file diff --git a/Week04/decorators_hilmican_karadag.py b/Week04/decorators_hilmican_karadag.py new file mode 100644 index 000000000..9d4514b8e --- /dev/null +++ b/Week04/decorators_hilmican_karadag.py @@ -0,0 +1,58 @@ +import functools +import time +import tracemalloc + + +def performance(fn): + """ + Decorator that measures execution time and memory of the wrapped function. + + Statistics are stored as attributes on the decorator itself: + + - ``performance.counter``: number of decorated-function calls + - ``performance.total_time``: total time spent in seconds + - ``performance.total_mem``: total memory consumed in bytes + + :param fn: the function to decorate + :type fn: callable + :return: the wrapped function + :rtype: callable + """ + + @functools.wraps(fn) + def wrapper(*args, **kwargs): + started_here = not tracemalloc.is_tracing() + if started_here: + tracemalloc.start() + tracemalloc.reset_peak() + mem_before = tracemalloc.get_traced_memory()[0] + start = time.perf_counter() + + try: + return fn(*args, **kwargs) + finally: + elapsed = time.perf_counter() - start + peak = tracemalloc.get_traced_memory()[1] + if started_here: + tracemalloc.stop() + performance.counter += 1 + performance.total_time += elapsed + performance.total_mem += max(peak - mem_before, 0) + + return wrapper + + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 + + +if __name__ == "__main__": + + @performance + def build_list(n: int) -> list[int]: + return [i * i for i in range(n)] + + build_list(100_000) + build_list(50_000) + print(performance.counter, performance.total_time, performance.total_mem)