Skip to content

Commit a2544ae

Browse files
2 , 3 , 4 weeks hw
1 parent 8b82b00 commit a2544ae

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎Week02/types_hilmican_karadag.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
my_int = 10
2+
my_float = 3.14
3+
my_bool = True
4+
my_complex = 3 + 9j

‎Week03/pyramid_hilmican_karadag.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def calculate_pyramid_height(number_of_blocks):
2+
3+
height = 1
4+
5+
while number_of_blocks >= x:
6+
7+
number_of_blocks = number_of_blocks - x
8+
9+
height = height + 1
10+
11+
return height
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def remove_duplicates(seq: list) -> list:
2+
"""
3+
This function removes duplicates from a list.
4+
"""
5+
result = []
6+
for item in seq:
7+
if item not in result:
8+
result.append(item)
9+
return result
10+
11+
12+
def list_counts(seq: list) -> dict:
13+
"""
14+
This function counts the number of occurrences of each item in a list.
15+
"""
16+
counts = {}
17+
for item in seq:
18+
counts[item] = counts.get(item, 0) + 1
19+
return counts
20+
21+
22+
def reverse_dict(d: dict) -> dict:
23+
"""
24+
This function reverses the keys and values of a dictionary.
25+
"""
26+
return {value: key for key, value in d.items()}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import functools
2+
import time
3+
import tracemalloc
4+
5+
6+
def performance(fn):
7+
"""
8+
Decorator that measures execution time and memory of the wrapped function.
9+
10+
Statistics are stored as attributes on the decorator itself:
11+
12+
- ``performance.counter``: number of decorated-function calls
13+
- ``performance.total_time``: total time spent in seconds
14+
- ``performance.total_mem``: total memory consumed in bytes
15+
16+
:param fn: the function to decorate
17+
:type fn: callable
18+
:return: the wrapped function
19+
:rtype: callable
20+
"""
21+
22+
@functools.wraps(fn)
23+
def wrapper(*args, **kwargs):
24+
started_here = not tracemalloc.is_tracing()
25+
if started_here:
26+
tracemalloc.start()
27+
tracemalloc.reset_peak()
28+
mem_before = tracemalloc.get_traced_memory()[0]
29+
start = time.perf_counter()
30+
31+
try:
32+
return fn(*args, **kwargs)
33+
finally:
34+
elapsed = time.perf_counter() - start
35+
peak = tracemalloc.get_traced_memory()[1]
36+
if started_here:
37+
tracemalloc.stop()
38+
performance.counter += 1
39+
performance.total_time += elapsed
40+
performance.total_mem += max(peak - mem_before, 0)
41+
42+
return wrapper
43+
44+
45+
performance.counter = 0
46+
performance.total_time = 0.0
47+
performance.total_mem = 0
48+
49+
50+
if __name__ == "__main__":
51+
52+
@performance
53+
def build_list(n: int) -> list[int]:
54+
return [i * i for i in range(n)]
55+
56+
build_list(100_000)
57+
build_list(50_000)
58+
print(performance.counter, performance.total_time, performance.total_mem)

0 commit comments

Comments
 (0)