113 lines
2.3 KiB
Python
113 lines
2.3 KiB
Python
def hello(x=None) -> str:
|
|
return f"Hello{', ' + str(x) if x else ''}!"
|
|
|
|
|
|
def int_to_roman(x: int) -> str:
|
|
int_roman = [
|
|
(1000, "M"),
|
|
(900, "CM"),
|
|
(500, "D"),
|
|
(400, "CD"),
|
|
(100, "C"),
|
|
(90, "XC"),
|
|
(50, "L"),
|
|
(40, "XL"),
|
|
(10, "X"),
|
|
(9, "IX"),
|
|
(5, "V"),
|
|
(4, "IV"),
|
|
(1, "I"),
|
|
]
|
|
|
|
res = ""
|
|
for n, rom in int_roman:
|
|
while x >= n:
|
|
res += rom
|
|
x -= n
|
|
|
|
return res
|
|
|
|
|
|
def longest_common_prefix(x: list[str]) -> str:
|
|
if not x or not x[0]:
|
|
return ""
|
|
|
|
prefix = x[0].strip()
|
|
break_flag = False
|
|
|
|
i = 0
|
|
while i < len(prefix):
|
|
for s in x:
|
|
s = s.strip()
|
|
|
|
if i >= len(s) or s[i] != prefix[i]:
|
|
break_flag = True
|
|
|
|
break
|
|
|
|
if break_flag:
|
|
break
|
|
|
|
i += 1
|
|
|
|
return prefix[:i]
|
|
|
|
|
|
class BankCard:
|
|
def __init__(self, total_sum: int, balance_limit: int = -1) -> None:
|
|
self.total_sum = total_sum
|
|
self.balance_limit = balance_limit
|
|
|
|
def put(self, sum_put: int):
|
|
self.total_sum += sum_put
|
|
print(f"You put {sum_put} dollars.")
|
|
return self
|
|
|
|
@property
|
|
def balance(self) -> int:
|
|
if self.balance_limit == 0:
|
|
raise ValueError("Balance check limits exceeded.")
|
|
|
|
self.balance_limit -= 1
|
|
return self.total_sum
|
|
|
|
def __add__(self, other):
|
|
return type(self)(
|
|
self.total_sum + other.total_sum,
|
|
(
|
|
max(self.balance_limit, other.balance_limit)
|
|
if self.balance_limit != -1 and other.balance_limit != -1
|
|
else -1
|
|
),
|
|
)
|
|
|
|
def __str__(self) -> str:
|
|
return "To learn the balance call balance."
|
|
|
|
def __call__(self, sum_spent: int) -> None:
|
|
if self.total_sum < sum_spent:
|
|
raise ValueError(f"Not enough money to spend {sum_spent} dollars.")
|
|
|
|
print(f"You spent {sum_spent} dollars")
|
|
self.total_sum -= sum_spent
|
|
|
|
return
|
|
|
|
|
|
def primes():
|
|
num = 2
|
|
|
|
while True:
|
|
prime = True
|
|
|
|
for i in range(2, int(num**0.5) + 1):
|
|
if not num % i:
|
|
prime = False
|
|
|
|
break
|
|
|
|
if prime:
|
|
yield num
|
|
|
|
num += 1
|