tutorium 06

This commit is contained in:
Nils Pukropp
2023-11-24 07:06:05 +01:00
parent 4a75aed57d
commit 1ac3e3d4fe
7 changed files with 533 additions and 0 deletions

View File

@ -0,0 +1,131 @@
from dataclasses import dataclass
@dataclass
class Element[T]:
value: T
next: 'Element[T] | None'
@dataclass
class MyList[T]:
head: Element[T] | None = None
last: Element[T] | None = None
length: int = 0
def push_back(self, value: T):
if not self.head:
self.head = Element(value, None)
self.last = self.head
self.length += 1
return
self.last.next = Element(value, None)
self.last = self.last.next
self.length += 1
def remove(self, index: int) -> T | None:
if self.length <= index:
return
if index == 0:
self.head = self.head.next
self.last = self.head
self.length -= 1
i = index
previous = self.head
current = self.head
while current and i > 0:
previous = current
current = current.next
i -= 1
if i != 0 or not current:
return
if not current.next:
self.last = previous
previous.next = current.next
self.length -= 1
return current.value
def __str__(self) -> str:
output = ""
current = self.head
while current:
output += str(current.value)
if current.next:
output += ", "
current = current.next
return f"[{output}]"
def __getitem__(self, index: int) -> T | None:
if self.length <= index:
return
current = self.head
i = index
while current and i > 0:
current = current.next
i -= 1
if i == 0 and current:
return current.value
def __setitem__(self, index: int, new_value: T):
if self.length <= index:
return
current = self.head
i = index
while current and i > 0:
current = current.next
i -= 1
if i == 0 and current:
current.value = new_value
def __len__(self) -> int:
return self.length
def __iter__(self) -> 'MyList.Iter[T]':
return MyList.Iter(self.head)
@dataclass
class Iter[E]:
current: Element[E]
def __next__(self) -> E:
if not self.current:
raise StopIteration
val = self.current.value
self.current = self.current.next
return val
if __name__ == "__main__":
lst: MyList[int] = MyList()
lst.push_back(0)
lst.push_back(3)
lst.push_back(2)
assert lst[0] == 0 and lst[1] == 3 and lst[2] == 2
print(lst)
lst.remove(1)
assert lst[0] == 0 and lst[1] == 2
print(lst)
lst[1] = 3
assert lst[0] == 0 and lst[1] == 3 and len(lst) == 2
print(lst)
assert lst.remove(1)
assert lst.last.value == 0
lst.remove(0)
assert len(lst) == 0
assert not lst.head
assert not lst.last
for i in range(0, 10):
lst.push_back(i)
for it in lst:
print(it)

View File

@ -0,0 +1,20 @@
from union_types import FilteredList, is_even
class PrintableInt(int):
def print(self):
print(self)
if __name__ == "__main__":
lst: FilteredList[PrintableInt] = FilteredList(filter=is_even)
lst.append(PrintableInt(0))
lst.get(0).print()
match lst.get(1):
case None:
pass
case PrintableInt(n):
n.print()
lst.append(PrintableInt(2))
lst.get(1).print()

View File

@ -0,0 +1,39 @@
from dataclasses import dataclass
from typing import Callable
type Optional[T] = T | None
@dataclass
class FilteredList[E]:
lst: list[E]
filter: Callable[[E], bool]
def __init__(self, filter=lambda _: True):
self.lst = []
self.filter = filter
def append(self, item: E):
if self.filter(item):
self.lst += [item]
def get(self, index: int) -> Optional[E]:
if index < len(self.lst):
return self.lst[index]
def __str__(self) -> str:
return str(self.lst)
def is_even(n: int) -> bool:
return n % 2 == 0
if __name__ == "__main__":
filter_list = FilteredList(filter=is_even)
filter_list.append(0)
print(filter_list)
filter_list.append(2)
print(filter_list)
filter_list.append(3)
print(filter_list)

View File

@ -0,0 +1,53 @@
from dataclasses import dataclass
@dataclass
class Point1D[T]:
x: T
@dataclass
class Point2D[T]:
x: T
y: T
@dataclass
class Point3D[T]:
x: T
y: T
z: T
type Point[T] = Point1D[T] | Point2D[T] | Point3D[T]
def print_point[T](pt: Point[T]) -> None:
match pt:
case Point1D(0) | Point2D(0, 0) | Point3D(0, 0, 0):
print("Nullpunkt!")
case Point1D(x):
print(f"Point1D: ({x})")
case Point2D(x, y):
print(f"Point2D: ({x}, {y})")
case Point3D(x, y, z):
print(f"Point3D: ({x}, {y}, {z})")
case _:
print("Not a point!")
def match_list(some_list: list[str]) -> None:
match some_list:
case ["🤡", *other]:
print(f"your list starts with 🤡 and the rest is {other}")
case _:
print("your list doesn't start with 🤡")
if __name__ == "__main__":
print_point(Point1D(1)) # (1)
print_point(Point2D(1, 2)) # (1, 2)
print_point(Point3D(1, 2, 3)) # (1, 2, 3)
print_point(Point3D(0, 0, 0)) # (1, 2, 3)
print_point("not a point") # Not a point!
match_list(["🤡", "ich", "hasse", "python", "manchmal"])

View File

@ -0,0 +1,8 @@
from mylist import MyList
lst: MyList[int] = MyList()
lst.push_back(0)
lst.push_back(1)
print(lst) # [0, 1]
lst.push_back("haha not a number")
print(lst) # [0, 1, haha not a number]