Skip to content

Commit c45bf8f

Browse files
Merge branch 'canbula:master' into master
2 parents 2d45101 + 2024d08 commit c45bf8f

9 files changed

Lines changed: 182 additions & 0 deletions

‎Week01/info_jalil_guliyev.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
student_id = "220315102"
2+
full_name = "Jalil Guliyev"

‎Week01/info_ufuk_akkuzu.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
student_id = "230316049"
2+
full_name = "Ufuk Akkuzu"

‎Week02/types_jalil_guliyev.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 = 2 + 3j

‎Week03/pyramid_jalil_guliyev.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
def calculate_pyramid_height(number_of_blocks):
3+
height = 0
4+
5+
while number_of_blocks >= height + 1:
6+
height += 1
7+
number_of_blocks -= height
8+
9+
return height

‎Week03/sequences_jalil_guliyev.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
def remove_duplicates(seq: list) -> list:
3+
result = []
4+
5+
for item in seq:
6+
if item not in result:
7+
result.append(item)
8+
9+
return result
10+
11+
12+
def list_counts(seq: list) -> dict:
13+
counts = {}
14+
15+
for item in seq:
16+
if item in counts:
17+
counts[item] += 1
18+
else:
19+
counts[item] = 1
20+
21+
return counts
22+
23+
24+
def reverse_dict(d: dict) -> dict:
25+
reversed_dict = {}
26+
27+
for key, value in d.items():
28+
reversed_dict[value] = key
29+
30+
return reversed_dict
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
custom_power = lambda x=0,/,e=1: x ** e
2+
3+
def custom_equation(x: int = 0 , y: int = 0, / , a: int = 1, b: int = 1, * , c: int = 1) -> float:
4+
"""
5+
Calculate the custom equation (x**a + y**b) / c.
6+
7+
:param x: first base
8+
:param y: second base
9+
:param a: first exponent
10+
:param b: second exponent
11+
:param c: divisor
12+
:return: the computed float result
13+
"""
14+
return (x**a + y**b) / c
15+
16+
17+
def fn_w_counter() -> (int, dict[str, int]):
18+
19+
if not hasattr(fn_w_counter,'call_counter'):
20+
21+
fn_w_counter.call_counter = 0
22+
fn_w_counter.caller_dict = {}
23+
caller = __name__
24+
fn_w_counter.call_counter += 1
25+
26+
if caller in fn_w_counter.caller_dict:
27+
fn_w_counter.caller_dict[caller] += 1
28+
else:
29+
fn_w_counter.caller_dict[caller] = 1
30+
return fn_w_counter.call_counter,fn_w_counter.caller_dict

‎Week05/emails_egehan_hatipoglu.py‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Emails(list):
2+
3+
def __init__(self, initial_emails=None):
4+
super().__init__()
5+
self.data = self
6+
if initial_emails is not None:
7+
self.validate(initial_emails)
8+
9+
def validate(self, emails_to_check):
10+
11+
for item in emails_to_check:
12+
if not isinstance(item, str):
13+
raise ValueError("List can only contain strings")
14+
15+
if "@" not in item:
16+
raise ValueError("Invalid email format")
17+
18+
domain_part = item.split("@")[1]
19+
if "." not in domain_part:
20+
raise ValueError("Invalid email format")
21+
22+
if item not in self:
23+
self.append(item)
24+
25+
def __repr__(self):
26+
return f"Emails({super().__repr__()})"
27+
28+
def __str__(self):
29+
return super().__str__()

‎Week05/emails_jalil_guliyev.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Week 05: Emails homework."""
2+
3+
import re
4+
5+
6+
class Emails(list):
7+
"""A list of unique, validated email addresses."""
8+
9+
_EMAIL_PATTERN = re.compile(r"^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)+$")
10+
11+
def __init__(self, emails):
12+
validated = self.validate(emails)
13+
super().__init__(validated)
14+
self.data = self
15+
16+
@classmethod
17+
def validate(cls, emails):
18+
"""Validate addresses and return unique values in input order."""
19+
unique = []
20+
for email in emails:
21+
if not isinstance(email, str) or not cls._EMAIL_PATTERN.fullmatch(email):
22+
raise ValueError(f"Invalid email address: {email!r}")
23+
if email not in unique:
24+
unique.append(email)
25+
return unique
26+
27+
def __str__(self):
28+
return str(list(self))
29+
30+
def __repr__(self):
31+
return f"Emails({list(self)!r})"

‎Week06/typeguard.py‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class WrongTypeAnnotationError(TypeError):
2+
pass
3+
4+
5+
class TypeGuard:
6+
def __init__(self, label: str = "TypeGuard"):
7+
self.label = label
8+
9+
def __call__(self, func):
10+
def wrapper(*args, **kwargs):
11+
print(f"{self.label}: Type checking")
12+
print(f"Function: {func.__name__}")
13+
print(f"Type annotations: {func.__annotations__}")
14+
for k, v in func.__annotations__.items():
15+
if k in kwargs:
16+
if not isinstance(kwargs[k], v):
17+
raise WrongTypeAnnotationError(
18+
f"Argument '{k}' must be of type {v.__name__}, "
19+
f"but got {type(kwargs[k]).__name__}"
20+
)
21+
result = func(*args, **kwargs)
22+
return result
23+
24+
return wrapper
25+
26+
27+
@TypeGuard(label="Custom Type Guard")
28+
def add(*, x: int, y: int) -> int:
29+
return x + y
30+
31+
32+
def main():
33+
x = 6
34+
y = "Canbula"
35+
try:
36+
result = add(x=x, y=y)
37+
print(f"Result: {result}")
38+
except TypeError as e:
39+
print(f"TypeError: {e}")
40+
except Exception as e:
41+
print(f"Error ({e.__class__.__name__}): {e}")
42+
43+
44+
if __name__ == "__main__":
45+
main()

0 commit comments

Comments
 (0)