Compare commits
4 Commits
1e03bb170f
...
86d0d6582d
| Author | SHA1 | Date | |
|---|---|---|---|
| 86d0d6582d | |||
| 7fc5e59d5b | |||
| 986a26890a | |||
| 9e5606d464 |
@ -1,16 +1,15 @@
|
|||||||
class Dog:
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Dog(ABC):
|
||||||
amount_of_dogs = 0
|
amount_of_dogs = 0
|
||||||
name: str
|
name: str
|
||||||
race: str
|
|
||||||
age: int
|
age: int
|
||||||
weight: float
|
weight: float
|
||||||
bark_sound: str
|
bark_sound: str
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, name: str, age: int, weight: float, bark_sound: str) -> None:
|
||||||
self, name: str, race: str, age: int, weight: float, bark_sound: str
|
|
||||||
) -> None:
|
|
||||||
self.name = name
|
self.name = name
|
||||||
self.race = race
|
|
||||||
self.age = age
|
self.age = age
|
||||||
self.weight = weight
|
self.weight = weight
|
||||||
self.bark_sound = bark_sound
|
self.bark_sound = bark_sound
|
||||||
@ -18,10 +17,11 @@ class Dog:
|
|||||||
Dog.amount_of_dogs += 1
|
Dog.amount_of_dogs += 1
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.name} is a {self.age} year old {self.race}"
|
return f"{self.name} is a {self.age} year old {self.__class__.__name__}"
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
def bark(self, times: int = 1) -> str:
|
def bark(self, times: int = 1) -> str:
|
||||||
return f"{self.name} says:" + (f" {self.bark_sound}" * times)
|
pass
|
||||||
|
|
||||||
def birthday(self) -> str:
|
def birthday(self) -> str:
|
||||||
self.age += 1
|
self.age += 1
|
||||||
@ -30,21 +30,35 @@ class Dog:
|
|||||||
def is_puppy(self) -> bool:
|
def is_puppy(self) -> bool:
|
||||||
return self.age < 2
|
return self.age < 2
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
return self.name == other.name
|
||||||
|
|
||||||
|
|
||||||
class Shepperd(Dog):
|
class Shepperd(Dog):
|
||||||
race: str = "shepperd"
|
|
||||||
bark_sound: str = "Woo!"
|
bark_sound: str = "Woo!"
|
||||||
|
|
||||||
def __init__(self, name: str, age: int, weight: float) -> None:
|
def __init__(self, name: str, age: int, weight: float) -> None:
|
||||||
Dog.__init__(self, name, Shepperd.race, age, weight, Shepperd.bark_sound)
|
Dog.__init__(self, name, age, weight, Shepperd.bark_sound)
|
||||||
|
|
||||||
|
def bark(self, times: int = 1) -> str:
|
||||||
|
return f"{self.name} says:" + (f" {self.bark_sound}" * times)
|
||||||
|
|
||||||
|
|
||||||
class Poodle(Dog):
|
class Poodle(Dog):
|
||||||
race: str = "poodle"
|
|
||||||
bark_sound: str = "Wow!"
|
bark_sound: str = "Wow!"
|
||||||
|
|
||||||
def __init__(self, name: str, age: int, weight: float) -> None:
|
def __init__(self, name: str, age: int, weight: float) -> None:
|
||||||
Dog.__init__(self, name, Poodle.race, age, weight, Poodle.bark_sound)
|
Dog.__init__(self, name, age, weight, Poodle.bark_sound)
|
||||||
|
|
||||||
|
def bark(self, times: int = 1) -> str:
|
||||||
|
return f"{self.name} says:" + (f" {self.bark_sound}" * times)
|
||||||
|
|
||||||
|
|
||||||
|
class Bulldog(Dog):
|
||||||
|
bark_sound: str = "WUUUU!"
|
||||||
|
|
||||||
|
def __init__(self, name: str, age: int, weight: float) -> None:
|
||||||
|
Dog.__init__(self, name, age, weight, Bulldog.bark_sound)
|
||||||
|
|
||||||
|
|
||||||
dog1 = Shepperd("Bello", 5, 22.5)
|
dog1 = Shepperd("Bello", 5, 22.5)
|
||||||
@ -53,7 +67,8 @@ print(dog1.bark())
|
|||||||
print(dog1.birthday())
|
print(dog1.birthday())
|
||||||
print(Dog.amount_of_dogs)
|
print(Dog.amount_of_dogs)
|
||||||
|
|
||||||
dog2 = Poodle("Barky", 10, 20)
|
dog2 = Poodle("Bello", 10, 20)
|
||||||
print(dog2.bark(3))
|
print(dog2.bark(3))
|
||||||
print(dog2.is_puppy())
|
print(dog2.is_puppy())
|
||||||
print(Dog.amount_of_dogs)
|
print(Dog.amount_of_dogs)
|
||||||
|
print(dog2 == dog1)
|
||||||
114
src/class/inheritance.py
Normal file
114
src/class/inheritance.py
Normal file
@ -0,0 +1,114 @@
|
|||||||
|
from abc import ABC, abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Speaker(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def speak(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Sleeper(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def sleep(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class Dog(Speaker, Sleeper):
|
||||||
|
def speak(self):
|
||||||
|
print("Wau")
|
||||||
|
|
||||||
|
def sleep(self):
|
||||||
|
print("Zzzzz")
|
||||||
|
|
||||||
|
|
||||||
|
dog = Dog()
|
||||||
|
dog.speak()
|
||||||
|
dog.sleep()
|
||||||
|
|
||||||
|
|
||||||
|
class Storage(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def save(self, key: str, value: str) -> None:
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def load(self, key: str) -> str:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryStorage(Storage):
|
||||||
|
def __init__(self):
|
||||||
|
self.storage = {}
|
||||||
|
|
||||||
|
def save(self, key: str, value: str) -> None:
|
||||||
|
self.storage.update({key: value})
|
||||||
|
|
||||||
|
def load(self, key: str) -> str:
|
||||||
|
return self.storage.get(key)
|
||||||
|
|
||||||
|
|
||||||
|
storage: Storage = MemoryStorage()
|
||||||
|
storage.save("key", "value")
|
||||||
|
print(storage.load("key"))
|
||||||
|
|
||||||
|
|
||||||
|
class Person:
|
||||||
|
def __init__(self, name: str):
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
|
||||||
|
class Student(Person):
|
||||||
|
def __init__(self, name: str, semester: int):
|
||||||
|
super().__init__(name)
|
||||||
|
self.semester = semester
|
||||||
|
|
||||||
|
|
||||||
|
s = Student("Mein Name", 3)
|
||||||
|
print(s.name)
|
||||||
|
print(s.semester)
|
||||||
|
|
||||||
|
|
||||||
|
class A:
|
||||||
|
def ping(self) -> None:
|
||||||
|
print(" A ")
|
||||||
|
|
||||||
|
|
||||||
|
class B(A):
|
||||||
|
def ping(self) -> None:
|
||||||
|
print(" B ")
|
||||||
|
super().ping()
|
||||||
|
|
||||||
|
|
||||||
|
class C(A):
|
||||||
|
def ping(self) -> None:
|
||||||
|
print(" C ")
|
||||||
|
super().ping()
|
||||||
|
|
||||||
|
|
||||||
|
class D(B, C):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
D().ping()
|
||||||
|
|
||||||
|
|
||||||
|
class Animal(ABC):
|
||||||
|
@abstractmethod
|
||||||
|
def speak(self):
|
||||||
|
return "..."
|
||||||
|
|
||||||
|
def chorus(self, n: int):
|
||||||
|
if n <= 0:
|
||||||
|
raise ValueError(" n muss positiv sein ")
|
||||||
|
return " ".join(self.speak() for _ in range(n))
|
||||||
|
|
||||||
|
|
||||||
|
class Cat(Animal):
|
||||||
|
def speak(self):
|
||||||
|
return super().speak()
|
||||||
|
|
||||||
|
def chorus(self, n: int):
|
||||||
|
return super().chorus(n)
|
||||||
|
|
||||||
|
|
||||||
|
print(Cat().chorus(3))
|
||||||
37
src/codewars/MenuDisplay.py
Normal file
37
src/codewars/MenuDisplay.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
class Menu:
|
||||||
|
def __init__(self, selects: list):
|
||||||
|
self.selects = selects
|
||||||
|
self.cursor = 0
|
||||||
|
|
||||||
|
def to_the_right(self):
|
||||||
|
self.cursor = (self.cursor + 1) % len(self.selects)
|
||||||
|
|
||||||
|
def to_the_left(self):
|
||||||
|
self.cursor = (self.cursor - 1) % len(self.selects)
|
||||||
|
|
||||||
|
def display(self):
|
||||||
|
return (
|
||||||
|
"["
|
||||||
|
+ ", ".join(
|
||||||
|
[
|
||||||
|
f"'{s}'" if i != self.cursor else f"['{s}']"
|
||||||
|
for i, s in enumerate(self.selects)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
+ "]"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
menu = Menu(["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"])
|
||||||
|
|
||||||
|
print(menu.display())
|
||||||
|
menu.to_the_right()
|
||||||
|
print(menu.display())
|
||||||
|
menu.to_the_left()
|
||||||
|
menu.to_the_left()
|
||||||
|
menu.to_the_left()
|
||||||
|
print(menu.display())
|
||||||
|
menu.to_the_left()
|
||||||
|
menu.to_the_left()
|
||||||
|
print(menu.display())
|
||||||
4
src/codewars/__main__.py
Normal file
4
src/codewars/__main__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from codewars.lastDigitOfHugeNumber import last_digit
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(last_digit([1, 2, 3, 4, 5]))
|
||||||
Loading…
x
Reference in New Issue
Block a user