diff --git a/README.md b/README.md index d56e54a..10c63df 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ Repository for CDS-2020 Programming and Promt Engineering II + +# Codewars +* Katas in src/codewars +|Title|Source|Link| +|Find the force of gravity between two objects|src/codewars/kata_force_of_gravity.py|https://www.codewars.com/kata/5b609ebc8f47bd595e000627/| diff --git a/src/codewars/kata_force_of_gravity.py b/src/codewars/kata_force_of_gravity.py index 3780d5d..0ed4a06 100644 --- a/src/codewars/kata_force_of_gravity.py +++ b/src/codewars/kata_force_of_gravity.py @@ -1,5 +1,3 @@ -# https://www.codewars.com/kata/5b609ebc8f47bd595e000627/ - units = { "kg": 1, "g": 1e-3, diff --git a/src/tutorial/class/class_introduction.py b/src/tutorial/class/class_introduction.py new file mode 100644 index 0000000..c12f93e --- /dev/null +++ b/src/tutorial/class/class_introduction.py @@ -0,0 +1,10 @@ +class Konto: + pass + + +k1 = Konto() +k2 = Konto() + +print(k1) +print(k2) +print(k1 is k2) diff --git a/src/tutorial/class/dataclass_tutorial.py b/src/tutorial/class/dataclass_tutorial.py new file mode 100644 index 0000000..1a84481 --- /dev/null +++ b/src/tutorial/class/dataclass_tutorial.py @@ -0,0 +1,30 @@ +from dataclasses import dataclass + + +@dataclass +class Student: + name: str + nr: int + sem: int + + +@dataclass +class Rechteck: + breite: float + hoehe: float + + def flaeche(self) -> float: + return self.breite * self.hoehe + + +# Validierung +@dataclass +class Produkt: + name: str + preis: float + + def __post_init__(self): + if not self.name: + raise ValueError("...") + if self.preis < 0: + raise ValueError("...") diff --git a/src/tutorial/class/hund.py b/src/tutorial/class/hund.py new file mode 100644 index 0000000..129deb9 --- /dev/null +++ b/src/tutorial/class/hund.py @@ -0,0 +1,41 @@ +class Hund: + anzahl_hunde = 0 + + def __init__(self, name: str, rasse: str, alter: int, gewicht: float): + self.name = name + self.rasse = rasse + self.alter = alter + self.gewicht = gewicht + Hund.anzahl_hunde += 1 + + def __repr__(self): + return f"Hund(name={self.name!r}, rasse={self.rasse}, alter={self.alter}, gewicht={self.gewicht}" + + def __str__(self): + return f"{self.name} ist ein {self.alter}-jähriger {self.rasse}" + + def bellen(self, n=1) -> int: + print(n * "Woof! ") + + def geburtstag(self): + self.alter += 1 + print( + f"Alles Gute zum Geburtstag, {self.name}! Du bist jetzt {self.alter} Jahre alt." + ) + + def ist_welpe(self): + if self.alter < 2: + print(f"{self.name} ist ein {self.alter}-jähriger Welpe") + else: + print(f"{self.name} ist ein {self.alter}-jähriger erwachsener Hund") + + +hund1 = Hund(name="Bello", rasse="Pudel", alter=99, gewicht=357) +hund2 = Hund(name="Dewy", rasse="Labrador", alter=-6, gewicht=1) + +print(repr(hund1)) +print(hund2) +hund2.bellen(3) +hund1.geburtstag() +hund2.ist_welpe() +hund1.ist_welpe() diff --git a/src/tutorial/class/konto.py b/src/tutorial/class/konto.py new file mode 100644 index 0000000..8b4927e --- /dev/null +++ b/src/tutorial/class/konto.py @@ -0,0 +1,22 @@ +class Konto: + def __init__(self, inhaber, saldo): + if not inhaber: + raise ValueError("Inhaber darf nicht leer sein") + if saldo < 0: + raise ValueError("Saldo darf nicht negativ sein") + self.inhaber = inhaber + self.saldo = saldo + + def einzahlen(self, betrag): + self.saldo += betrag + + def abheben(self, betrag): + self.saldo -= betrag + + +k = Konto("Alice", 100) +print(k.saldo) +k.einzahlen(25) +print(k.saldo) +k.abheben(45) +print(k.saldo) diff --git a/src/tutorial/class/repr_tutorial.py b/src/tutorial/class/repr_tutorial.py new file mode 100644 index 0000000..baf16d0 --- /dev/null +++ b/src/tutorial/class/repr_tutorial.py @@ -0,0 +1,11 @@ +class Konto: + def __init__(self, inhaber, saldo): + self.inhaber = inhaber + self.saldo = saldo + + def __repr__(self): + return f"Konto(inhaber = {self.inhaber!r}, saldo = {self.saldo!r})" + + +k = Konto(" Alice ", 100.0) +print(k) # Konto ( inhaber = Alice , saldo =100.0) diff --git a/src/tutorial/class/str_tutorial.py b/src/tutorial/class/str_tutorial.py new file mode 100644 index 0000000..db26169 --- /dev/null +++ b/src/tutorial/class/str_tutorial.py @@ -0,0 +1,16 @@ +class Konto: + def __init__(self, inhaber, saldo): + self.inhaber = inhaber + self.saldo = saldo + + def __repr__(self): + return f"Konto(inhaber = {self.inhaber!r}, saldo = {self.saldo!r})" + + def __str__(self): + return f"Konto von {self.inhaber}: CHF {self.saldo:.2f}" + + +k = Konto("Alice", 100.0) +print(str(k)) # __str__ +print(repr(k)) # __repr__ +print(k) # bevorzugt __str__ , sonst __repr__