This commit is contained in:
englingvi103466 committed 2026-10-29 13:17:57 +01:00
1 parent b17db7bc39
commit d327f5d7e6
7 files changed
+132

No files matched your search

+7
View File
@@ -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]
+7
View File
@@ -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!"]
+7
View File
@@ -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]
+7
View File
@@ -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
+16
View File
@@ -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
+47
View File
@@ -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!")
+41
View File
@@ -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