From 84f0f9e8fbba5ad38db2f21bd68fcbbe09c89007 Mon Sep 17 00:00:00 2001 From: Vivien Engling Date: Thu, 29 Oct 2026 13:42:50 +0100 Subject: [PATCH] modified --- 07_kloetzchen.py | 61 ++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/07_kloetzchen.py b/07_kloetzchen.py index 6bd1704..d05ddcd 100644 --- a/07_kloetzchen.py +++ b/07_kloetzchen.py @@ -1,41 +1,52 @@ -# Ausgangs- und Zielzustand -state = {'A': 'B', 'B': '', 'C': ''} -goal = {'A': 'C', 'B': 'A', 'C': ''} - +from functools import reduce +# Gegebene Funktionen aus der Aufgabe def is_clear(state, block): - """Prüft, ob ein Block frei ist (nichts liegt darauf).""" - return all(v != block for v in state.values()) - + 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): + def action(state): + if not is_clear(state, block): raise ValueError(f"Auf Block {block} liegt etwas!") - if target != '' and not is_clear(current_state, target): + if target != "_" and not is_clear(state, target): raise ValueError(f"Block {target} ist nicht frei!") - - new_state = current_state.copy() + new_state = 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)) + return lambda state: g(f(state)) if __name__ == '__main__': - # Erstellen des Ausführungsplans - plan = chain(move('A', 'C'), move('B', 'A')) + # Ausgangs- und Zielzustand der Aufgabe + start = {"A": "B", "B": "_", "C": "_", "D": "C"} + goal = {"A": "_", "B": "C", "C": "_", "D": "B"} - # Ausführen des Plans auf dem Ausgangszustand - final = plan(state) + # Liste der Aktionen in korrekter Reihenfolge + actions = [ + move("A", "_"), + move("D", "_"), + move("B", "C"), + move("D", "B") + ] - print("Final state:", final) - assert final == goal \ No newline at end of file + # Originalzustand für den späteren Unverändertbarkeits-Check sichern + start_copy = start.copy() + + # 1. Plan aus der Liste mit reduce erzeugen (Startwert ist die neutrale Funktion) + plan = reduce(chain, actions, lambda state: state) + + # 2. Plan auf den Startzustand anwenden + final_state = plan(start) + + print("Final state:", final_state) + + # Prüfen, ob das Ziel erreicht wurde + assert final_state == goal, "Das Ziel wurde nicht erreicht!" + + # 3. Prüfen, ob der Startzustand durch die rein funktionale Arbeitsweise unverändert geblieben ist + assert start == start_copy, "Der Startzustand wurde modifiziert (Seiteneffekt)!" + + print("Erfolgreich! Zielzustand erreicht und Startzustand blieb intakt.") \ No newline at end of file