116 lines
2.3 KiB
Python
116 lines
2.3 KiB
Python
def hello(x=None) -> str:
|
|
s = "Hello"
|
|
if x:
|
|
s += f", {x}"
|
|
|
|
return s + "!"
|
|
|
|
|
|
def int_to_roman(x: int) -> str:
|
|
match = [
|
|
(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"),
|
|
]
|
|
|
|
lst = []
|
|
for num, text in match:
|
|
while x >= num:
|
|
x -= num
|
|
lst.append(text)
|
|
|
|
return "".join(lst)
|
|
|
|
|
|
def longest_common_prefix(x: list[str]) -> str:
|
|
if not x:
|
|
return ""
|
|
|
|
s = x[0].strip()
|
|
ind = 0
|
|
flag = False
|
|
|
|
while ind < len(s):
|
|
for word in x:
|
|
word = word.strip()
|
|
if len(word) <= ind or word[ind] != s[ind]:
|
|
flag = True
|
|
break
|
|
if flag:
|
|
break
|
|
|
|
ind += 1
|
|
|
|
if ind == -1:
|
|
return ""
|
|
|
|
return s[:ind]
|
|
|
|
|
|
class BankCard:
|
|
def __init__(self, total_sum: int, balance_limit: int = -1) -> None:
|
|
self.total_sum = total_sum
|
|
self.balance_limit = balance_limit
|
|
|
|
@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 put(self, sum_put: int):
|
|
self.total_sum += sum_put
|
|
print(f"You put {sum_put} dollars.")
|
|
|
|
return self
|
|
|
|
def __add__(self, other):
|
|
if self.balance_limit == -1 or other.balance_limit == -1:
|
|
new_balance_limit = -1
|
|
else:
|
|
new_balance_limit = max(self.balance_limit, other.balance_limit)
|
|
|
|
return type(self)(self.total_sum + other.total_sum, new_balance_limit)
|
|
|
|
def __call__(self, sum_spent: int) -> None:
|
|
if self.total_sum < sum_spent:
|
|
raise ValueError(f"Not enough money to spend sum_spent dollars.")
|
|
|
|
self.total_sum -= sum_spent
|
|
print(f"You spent {sum_spent} dollars")
|
|
|
|
return
|
|
|
|
def __str__(self) -> str:
|
|
return "To learn the balance call balance."
|
|
|
|
|
|
def primes():
|
|
pr = 2
|
|
|
|
while True:
|
|
flag = False
|
|
|
|
for n in range(2, int(pr**0.5) + 1):
|
|
if pr % n == 0:
|
|
flag = True
|
|
break
|
|
|
|
if not flag:
|
|
yield pr
|
|
|
|
pr += 1
|