simple classes, add codewars - last digit of huge number
This commit is contained in:
parent
d73e6cb2ec
commit
ad72160139
32
src/classes.py
Normal file
32
src/classes.py
Normal file
@ -0,0 +1,32 @@
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Konto:
|
||||
inhaber: str
|
||||
saldo: float
|
||||
|
||||
def __init__(self, inhaber: str, saldo: float):
|
||||
if not inhaber:
|
||||
raise ValueError("inhaber is required")
|
||||
if saldo < 0:
|
||||
raise ValueError("saldo must be positive")
|
||||
object.__setattr__(self, "inhaber", inhaber)
|
||||
object.__setattr__(self, "saldo", saldo)
|
||||
|
||||
def einzahlen(self, betrag: float):
|
||||
object.__setattr__(self, "saldo", self.saldo + betrag)
|
||||
|
||||
def abheben(self, betrag: float):
|
||||
object.__setattr__(self, "saldo", self.saldo - betrag)
|
||||
return betrag
|
||||
|
||||
|
||||
k1 = Konto("Alice", 100.0)
|
||||
k2 = Konto("Housi", 10)
|
||||
k2.einzahlen(100)
|
||||
|
||||
print(k1.inhaber)
|
||||
print(k2.saldo)
|
||||
print(k1 is k2)
|
||||
print(k1)
|
||||
0
src/codewars/__init__.py
Normal file
0
src/codewars/__init__.py
Normal file
9
src/codewars/lastDigitOfHugeNumber.py
Normal file
9
src/codewars/lastDigitOfHugeNumber.py
Normal file
@ -0,0 +1,9 @@
|
||||
def last_digit(lst):
|
||||
rev_lst = list(reversed(lst))
|
||||
|
||||
n = 1
|
||||
|
||||
for number in rev_lst:
|
||||
n = number ** (n if n < 4 else n % 4 + 4)
|
||||
|
||||
return n % 10
|
||||
20
tests/codewars/test_lastDigitOfHugeNumber.py
Normal file
20
tests/codewars/test_lastDigitOfHugeNumber.py
Normal file
@ -0,0 +1,20 @@
|
||||
from codewars.lastDigitOfHugeNumber import last_digit
|
||||
|
||||
|
||||
def test_last_digit():
|
||||
test_data = [
|
||||
([], 1),
|
||||
([0, 0], 1),
|
||||
([0, 0, 0], 0),
|
||||
([1, 2], 1),
|
||||
([3, 4, 5], 1),
|
||||
([4, 3, 6], 4),
|
||||
([7, 6, 21], 1),
|
||||
([12, 30, 21], 6),
|
||||
([2, 2, 2, 0], 4),
|
||||
([937640, 767456, 981242], 0),
|
||||
([123232, 694022, 140249], 6),
|
||||
([499942, 898102, 846073], 6),
|
||||
]
|
||||
for test_input, test_output in test_data:
|
||||
assert last_digit(test_input) == test_output
|
||||
Loading…
x
Reference in New Issue
Block a user