Crashers - 3.9 Developing Algorithms Python Hacks
Learn how to develop algorithms through the Peppa Pig Maze game
π· Peppaβs Algorithm Adventures - Python Hacks
Welcome to Peppaβs algorithm challenges! Complete these three hacks to master algorithm development with interactive Python examples.
Hack 1: Peppaβs Number Comparison Algorithm π’
Create algorithms that use Boolean expressions to compare numbers, just like in the lesson!
Your task: Complete the missing Boolean conditions to help Peppa make smart decisions.
# Peppa's number comparison algorithms (like the lesson examples!)
def algorithm_a_find_maximum(a, b):
"""Algorithm A: Find the larger number using if-else"""
if a > b: # This is given as an example
return a
else:
return b
def algorithm_b_find_maximum(a, b):
"""Algorithm B: Same problem, different approach using Boolean expression"""
# Boolean expression version of the same logic
return a if a > b else b # β
condition filled in
def peppa_decision_maker():
"""Help Peppa make decisions using Boolean logic"""
peppa_coins = 15
toy_price = 12
print("π· Peppa's Decision Algorithm")
print(f"Peppa has {peppa_coins} coins")
print(f"Toy costs {toy_price} coins")
# Boolean comparison to decide if Peppa can buy the toy
can_buy_toy = peppa_coins >= toy_price # β
comparison filled in
if can_buy_toy:
print("β
Peppa can buy the toy!")
else:
print("β Peppa needs more coins!")
return can_buy_toy
# Test the algorithms (like the lesson does)
print("=== Testing Maximum Algorithms ===")
x, y = 10, 7
print(f"Algorithm A result: {algorithm_a_find_maximum(x, y)}")
print(f"Algorithm B result: {algorithm_b_find_maximum(x, y)}")
print("\n=== Peppa's Decision ===")
peppa_decision_maker()
Hack 2: Georgeβs Simple Movement Algorithm π§
Create a simple movement algorithm like the maze example from the lesson!
Your task: Complete the Boolean conditions to control Georgeβs movement.
# George's movement algorithm (similar to the lesson's maze example)
def george_movement_algorithm():
"""Simple movement system using Boolean conditions"""
# George's current position
george_x = 2
george_y = 1
# Boundary limits (like the maze example)
max_x = 4
max_y = 3
min_x = 0
min_y = 0
print("π· George's Movement Algorithm")
print(f"George is at position ({george_x}, {george_y})")
print(f"Boundaries: x(0-{max_x}), y(0-{max_y})")
# Movement commands
print("\n--- Testing Movement ---")
# Try moving right
new_x = george_x + 1
can_move_right = new_x <= max_x # β
within boundary
print(f"Move right to ({new_x}, {george_y}): {'β
Valid' if can_move_right else 'β Invalid'}")
# Try moving up
new_y = george_y + 1
can_move_up = new_y <= max_y # β
within boundary
print(f"Move up to ({george_x}, {new_y}): {'β
Valid' if can_move_up else 'β Invalid'}")
# Try moving left
new_x = george_x - 1
can_move_left = new_x >= min_x # β
within boundary
print(f"Move left to ({new_x}, {george_y}): {'β
Valid' if can_move_left else 'β Invalid'}")
def interactive_movement():
"""Let user test the movement algorithm"""
print("\nπ― Interactive Movement Test")
x, y = 1, 1 # Starting position
direction = input("Which way should George move? (up/down/left/right): ").lower()
if direction == "right":
new_x, new_y = x + 1, y
elif direction == "left":
new_x, new_y = x - 1, y
elif direction == "up":
new_x, new_y = x, y + 1
elif direction == "down":
new_x, new_y = x, y - 1
else:
print("β Invalid direction!")
return
# β
Complete Boolean condition for boundaries
is_valid_move = (0 <= new_x <= 4) and (0 <= new_y <= 3)
if is_valid_move:
print(f"β
George moved {direction} to ({new_x}, {new_y})")
else:
print(f"β Can't move {direction} - out of bounds!")
# Run the algorithms
george_movement_algorithm()
interactive_movement()
π· George's Movement Algorithm
George is at position (2, 1)
Boundaries: x(0-4), y(0-3)
--- Testing Movement ---
Move right to (3, 1): β
Valid
Move up to (2, 2): β
Valid
Move left to (1, 1): β
Valid
π― Interactive Movement Test
β
George moved up to (1, 2)
Hack 3: Peppaβs Pathfinding Adventure πΊοΈ
Create a pathfinding algorithm to help Peppa navigate through different terrains to reach her friends! This combines Boolean logic, conditional statements, and algorithm design.
Your task: Implement different pathfinding strategies and compare their effectiveness using interactive visualizations.
# Simple maze pathfinding
def peppa_maze_pathfinder():
"""Help Peppa find her way through a simple maze"""
# Simple 5x5 maze: 0=path, 1=wall, 2=start, 3=goal
maze = [
[2, 0, 1, 0, 0],
[0, 0, 1, 0, 1],
[0, 1, 0, 0, 0],
[0, 0, 0, 1, 0],
[1, 0, 0, 0, 3]
]
def display_maze(path=None):
symbols = {0: "β¬", 1: "β¬", 2: "π·", 3: "π«"}
print("Peppa's Maze:")
for r in range(5):
row = ""
for c in range(5):
if path and (r, c) in path:
row += "π¨" # Yellow path
else:
row += symbols[maze[r][c]]
print(row)
def is_valid_move(row, col):
"""
Return True if move is valid, False otherwise.
Checks:
- Inside bounds (0β4)
- Not a wall (maze[row][col] != 1)
"""
if 0 <= row < 5 and 0 <= col < 5 and maze[row][col] != 1:
return True
return False
def find_path():
"""Simple pathfinding from start to goal (preset path example)"""
start = (0, 0) # Peppa's position
goal = (4, 4) # Friend's position
# This sample path follows valid moves through the maze
sample_path = [
(0, 0), (0, 1), (1, 1), (2, 2),
(2, 3), (2, 4), (3, 4), (4, 4)
]
# Validate the path
valid_path = []
for pos in sample_path:
if is_valid_move(pos[0], pos[1]):
valid_path.append(pos)
return valid_path if valid_path[-1] == goal else None
# Run the pathfinder
print("π· Welcome to Peppa's Mini Maze!")
display_maze()
path = find_path()
if path:
print("β
Path found!")
display_maze(path)
print(f"Path length: {len(path)} steps")
else:
print("β Complete the is_valid_move function to find the path!")
# Run the maze solver
peppa_maze_pathfinder()
π· Welcome to Peppa's Mini Maze!
Peppa's Maze:
π·β¬β¬β¬β¬
β¬β¬β¬β¬β¬
β¬β¬β¬β¬β¬
β¬β¬β¬β¬β¬
β¬β¬β¬β¬π«
β
Path found!
Peppa's Maze:
π¨π¨β¬β¬β¬
β¬π¨β¬β¬β¬
β¬β¬π¨π¨π¨
β¬β¬β¬β¬π¨
β¬β¬β¬β¬π¨
Path length: 8 steps
π What You Should Complete
After finishing the lesson, you should be able to:
- Hack 1: Fill in the Boolean comparison operators (
<=,>=,<,>) to make the muddy puddle validator work - Hack 2: Complete the
if/elif/elsestatements for Georgeβs number comparison - Hack 3: Fill in the boundary conditions for Georgeβs movement algorithm