From d327f5d7e6d5b3cae9c89456e07991734203892e Mon Sep 17 00:00:00 2001 From: Vivien Engling Date: Thu, 29 Oct 2026 13:17:57 +0100 Subject: [PATCH] added --- 01_verdoppeln.py | 7 +++++++ 02_trump_style.py | 7 +++++++ 03_gerade_zahlen.py | 7 +++++++ 04_k_laenge.py | 7 +++++++ 05_produkt.py | 16 +++++++++++++++ 06_undo.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 07_kloetzchen.py | 41 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 132 insertions(+) create mode 100644 01_verdoppeln.py create mode 100644 02_trump_style.py create mode 100644 03_gerade_zahlen.py create mode 100644 04_k_laenge.py create mode 100644 05_produkt.py create mode 100644 06_undo.py create mode 100644 07_kloetzchen.py diff --git a/01_verdoppeln.py b/01_verdoppeln.py new file mode 100644 index 0000000..3f1742f --- /dev/null +++ b/01_verdoppeln.py @@ -0,0 +1,7 @@ +numbers = [1, 2, 3, 4, 5] + +# Verdopplung aller Zahlen mittels map +result = list(map(lambda x: x * 2, numbers)) + +print("Ergebnis:", result) +#assert result == [2, 4, 6, 8, 10] \ No newline at end of file diff --git a/02_trump_style.py b/02_trump_style.py new file mode 100644 index 0000000..87776af --- /dev/null +++ b/02_trump_style.py @@ -0,0 +1,7 @@ +words = ["huge", "great", "tremendous", "fake news"] + +# Strings in Großbuchstaben umwandeln und "!" anhängen +result = list(map(lambda word: word.upper() + "!", words)) + +print("Ergebnis:", result) +#assert result == ["HUGE!", "GREAT!", "TREMENDOUS!", "FAKE NEWS!"] \ No newline at end of file diff --git a/03_gerade_zahlen.py b/03_gerade_zahlen.py new file mode 100644 index 0000000..2638ad7 --- /dev/null +++ b/03_gerade_zahlen.py @@ -0,0 +1,7 @@ +numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +# Nur gerade Zahlen (x % 2 == 0) herausfiltern +result = list(filter(lambda x: x % 2 == 0, numbers)) + +print("Ergebnis:", result) +assert result == [2, 4, 6, 8, 10] \ No newline at end of file diff --git a/04_k_laenge.py b/04_k_laenge.py new file mode 100644 index 0000000..3cc6c9a --- /dev/null +++ b/04_k_laenge.py @@ -0,0 +1,7 @@ +words = ["Kuchen", "Katze", "Apfel", "Kaffee", "Baum", "Kino"] + +# Einzeiler kombiniert filter, map und sum +total_length = sum(map(len, filter(lambda w: w.lower().startswith('k'), words))) + +print("Gesamtlänge aller K-Wörter:", total_length) +assert total_length == 6 + 5 + 6 + 4 # Kuchen (6) + Katze (5) + Kaffee (6) + Kino (4) = 21 \ No newline at end of file diff --git a/05_produkt.py b/05_produkt.py new file mode 100644 index 0000000..06a3dfa --- /dev/null +++ b/05_produkt.py @@ -0,0 +1,16 @@ +from functools import reduce + +numbers = [2, 3, 4, 5] + +# Produkt berechnen +result = reduce(lambda x, y: x * y, numbers, 1) + +print("Produkt:", result) +assert result == 120 + +# Verhalten bei einer leeren Liste: +# Ohne Startwert (initializer) wirft reduce() einen TypeError: "reduce() of empty sequence with no initial value". +# Behebung: Der optional paramter initializer=1 sorgt dafür, dass bei einer leeren Liste das neutrale Element 1 zurückgegeben wird. +empty_list = [] +result_empty = reduce(lambda x, y: x * y, empty_list, 1) +assert result_empty == 1 \ No newline at end of file diff --git a/06_undo.py b/06_undo.py new file mode 100644 index 0000000..73653e4 --- /dev/null +++ b/06_undo.py @@ -0,0 +1,47 @@ +class Particle: + def __init__(self, position, velocity=(0, 0), acceleration=(0, 0)): + self.position = position + self.velocity = velocity + self.acceleration = acceleration + + def update(self): + """Erstellt ein neues Particle-Objekt mit aktualisiertem Zustand.""" + new_velocity = ( + self.velocity[0] + self.acceleration[0], + self.velocity[1] + self.acceleration[1] + ) + new_position = ( + self.position[0] + new_velocity[0], + self.position[1] + new_velocity[1] + ) + return Particle(new_position, new_velocity, self.acceleration) + + +# Simulation mit Historie (Immutable State erlaubt Undo) +history = [] + +# Initialzustand (Start bei (0, 0) mit Beschleunigung (1, 2)) +p0 = Particle(position=(0, 0), velocity=(0, 0), acceleration=(1, 2)) +history.append(p0) + +# 1. Update +p1 = history[-1].update() +history.append(p1) + +# 2. Update +p2 = history[-1].update() +history.append(p2) + +# Überprüfen der Zwischenzustände +assert history[0].position == (0, 0) +assert history[1].position == (1, 2) +assert history[2].position == (3, 6) + +# UNDO-Schritte ausführen (Einfach das jeweils letzte Element entfernen) +history.pop() # Undo von Schritt 2 +assert history[-1].position == (1, 2) + +history.pop() # Undo von Schritt 1 +assert history[-1].position == (0, 0) + +print("Undo-Tests für Particle erfolgreich bestanden!") \ No newline at end of file diff --git a/07_kloetzchen.py b/07_kloetzchen.py new file mode 100644 index 0000000..6bd1704 --- /dev/null +++ b/07_kloetzchen.py @@ -0,0 +1,41 @@ +# Ausgangs- und Zielzustand +state = {'A': 'B', 'B': '', 'C': ''} +goal = {'A': 'C', 'B': 'A', 'C': ''} + + +def is_clear(state, block): + """Prüft, ob ein Block frei ist (nichts liegt darauf).""" + return all(v != block for v in state.values()) + + +def move(block, target): + """Gibt eine Action-Funktion zurück (Closure).""" + + def action(current_state): + if not is_clear(current_state, block): + raise ValueError(f"Auf Block {block} liegt etwas!") + if target != '' and not is_clear(current_state, target): + raise ValueError(f"Block {target} ist nicht frei!") + + new_state = current_state.copy() + new_state[block] = target + print(f"Moved {block} to {target}.") + return new_state + + return action + + +def chain(f, g): + """Funktionale Komposition: Erst f, dann g ausführen.""" + return lambda current_state: g(f(current_state)) + + +if __name__ == '__main__': + # Erstellen des Ausführungsplans + plan = chain(move('A', 'C'), move('B', 'A')) + + # Ausführen des Plans auf dem Ausgangszustand + final = plan(state) + + print("Final state:", final) + assert final == goal \ No newline at end of file