Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Week04/functions_hilmican_karadag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# x is positional-only (default 0), e is positional-or-keyword (default 1)
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:
"""
Calculate (x**a + y**b) / c.

:param x: Positional-only base of the first term.
:type x: int
:param y: Positional-only base of the second term.
:type y: int
:param a: Exponent applied to x.
:type a: int
:param b: Exponent applied to y.
:type b: int
:param c: Keyword-only divisor.
:type c: int
:return: The value of (x**a + y**b) / c.
:rtype: float
"""
return (x**a + y**b) / c


_total_calls: int = 0
_calls_by_caller: dict[str, int] = {}


def fn_w_counter() -> (int, dict[str, int]):
"""
Count how many times this function has been called, and by whom.

:return: A tuple of the total number of calls and a dictionary mapping
each caller's ``__name__`` to the number of calls it made.
:rtype: tuple[int, dict[str, int]]
"""
global _total_calls
_total_calls += 1
_calls_by_caller[__name__] = _calls_by_caller.get(__name__, 0) + 1
return _total_calls, dict(_calls_by_caller)


if __name__ == "__main__":
assert custom_power(2) == 2
assert custom_power(2, 3) == 8
assert custom_power(2, e=2) == 4

assert custom_equation(2, 3) == 5.0
assert custom_equation(2, 3, 2) == 7.0
assert custom_equation(2, 3, 2, 3) == 31.0
assert custom_equation(3, 5, a=2, b=3, c=4) == 33.5
assert custom_equation(3, 5, 2, b=3, c=4) == 33.5
assert custom_equation(3, 5, 2, 3, c=4) == 33.5

for i in range(10):
fn_w_counter()
assert fn_w_counter() == (11, {"__main__": 11})
print("All examples pass.")
30 changes: 30 additions & 0 deletions Week05/emails_hilmican_karadag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re


class Emails(list):

EMAIL_PATTERN = re.compile(r"[^@\s]+@[^@\s]+\.[^@\s]+")

def __init__(self, emails):
super().__init__()
self.validate(emails)
for email in emails:
if email not in self: # tekrar edenleri ekleme
self.append(email)

@property
def data(self):
return list(self)

def validate(self, emails):
for email in emails:
if not isinstance(email, str):
raise ValueError(f"{email!r} is not a string")
if not self.EMAIL_PATTERN.fullmatch(email):
raise ValueError(f"{email!r} is not a valid email address")

def __repr__(self):
return f"Emails({list.__repr__(self)})"

def __str__(self):
return ", ".join(self)
Loading