Skip to content

Commit de4e540

Browse files
authored
Merge pull request #1033 from hamza-jpg/master
Week03 pyramid homework
2 parents bce4c52 + 67313b7 commit de4e540

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def calculate_pyramid_height(number_of_blocks):
2+
height = 0
3+
blocks = 0
4+
while blocks + height < number_of_blocks:
5+
height += 1
6+
blocks += height
7+
return height
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 occurences
15+
of each item in a list
16+
"""
17+
counts = {}
18+
for item in seq:
19+
if item in counts:
20+
counts[item] += 1
21+
else:
22+
counts[item] = 1
23+
return counts
24+
25+
26+
def reverse_dict(d: dict) -> dict:
27+
"""
28+
This function reverses the keys and values
29+
of a dictionary
30+
"""
31+
new_d = {}
32+
for key, value in d.items():
33+
new_d[value] = key
34+
return new_d
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import time
2+
import tracemalloc
3+
4+
5+
def performance(func):
6+
"""Measure the time and memory each call of func takes and keep running totals."""
7+
8+
def wrapper(*args, **kwargs):
9+
tracemalloc.start()
10+
start = time.perf_counter()
11+
12+
result = func(*args, **kwargs)
13+
14+
elapsed = time.perf_counter() - start
15+
_, peak = tracemalloc.get_traced_memory()
16+
tracemalloc.stop()
17+
18+
performance.counter += 1
19+
performance.total_time += elapsed
20+
performance.total_mem += peak
21+
22+
return result
23+
24+
return wrapper
25+
26+
27+
performance.counter = 0
28+
performance.total_time = 0
29+
performance.total_mem = 0
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
custom_power = lambda x=0, /, e=1: x ** e
2+
3+
4+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
5+
"""
6+
Compute (x**a + y**b) / c.
7+
8+
:param x: First base, positional-only
9+
:type x: int
10+
:param y: Second base, positional-only
11+
:type y: int
12+
:param a: Exponent of x
13+
:type a: int
14+
:param b: Exponent of y
15+
:type b: int
16+
:param c: Divisor, keyword-only
17+
:type c: int
18+
:return: The result of (x**a + y**b) / c
19+
:rtype: float
20+
"""
21+
return (x ** a + y ** b) / c
22+
23+
24+
def fn_w_counter() -> (int, dict[str, int]):
25+
fn_w_counter.total += 1
26+
fn_w_counter.callers[__name__] = fn_w_counter.callers.get(__name__, 0) + 1
27+
28+
return fn_w_counter.total, dict(fn_w_counter.callers)
29+
30+
31+
fn_w_counter.total = 0
32+
fn_w_counter.callers = {}

0 commit comments

Comments
 (0)