2025-09-10
Maze Puzzle
# Recursive function to solve a maze
def solve_maze(maze, x, y, path):
# Base case: reached the end
if maze[x][y] == 'E':
path.append((x, y))
return True
# Mark current cell as visited
maze[x][y] = '#'
# Possible moves: right, down, left, up
moves = [(0,1), (1,0), (0,-1), (-1,0)]
for dx, dy in moves:
nx, ny = x + dx, y + dy
if 0 <= nx < len(maze) and 0 <= ny < len(maze[0]) and maze[nx][ny] in (' ', 'E'):
if solve_maze(maze, nx, ny, path):
path.append((x, y))
return True
return False
Tic-Tac_Toe
X
and O
).Minimax Algorithm
function minimax(board, player):
if winner(board) or board full:
return score of board (1 for win, -1 for loss, 0 for draw)
if player is maximizing:
bestScore = -∞
for each valid move:
make move
score = minimax(board, opponent)
undo move
bestScore = max(score, bestScore)
return bestScore
else: # minimizing player
bestScore = +∞
for each valid move:
make move
score = minimax(board, opponent)
undo move
bestScore = min(score, bestScore)
return bestScore
Tower of Hanoi
Classic puzzle involving three rods/spindles/columns, and a number of disks of different sizes.
Also known as the Tower of Brahma or Lucas’s Tower for Édouard Lucas
All the disks start out on one rod, stacked in order of diameter, which gives the appearance of a tower
# Pythonic function-based ToH
def create_tower_of_hanoi(n_disks=3):
stacks = [[] for _ in range(3)]
labels = ['L', 'M', 'R']
# Initialize first spindle with disks in descending order
stacks[0] = list(range(n_disks, 0, -1))
return stacks, labels
def reset_tower(stacks, n_disks):
for spindle in range(3):
stacks[spindle] = []
stacks[0] = list(range(n_disks, 0, -1))
def label(labels, spindle):
return labels[spindle]
# Lenght of each rod = number of disk on a spindle
def height(stacks, spindle):
return len(stacks[spindle])
def top_disk(stacks, spindle):
if stacks[spindle]:
return stacks[spindle][-1]
return None
#Create the tower
def tower_str(stacks, labels):
result = ""
for spindle in range(3):
if result:
result += "\n"
result += f"{labels[spindle]}: {stacks[spindle]}"
return result
## ToH movement
def move(stacks, labels, source, to, show=False):
if not stacks[source]:
raise Exception("Cannot move from empty spindle " + labels[source])
if stacks[to] and stacks[source][-1] > stacks[to][-1]:
raise Exception(
"Cannot move disk " + str(stacks[source][-1]) +
" on top of disk " + str(stacks[to][-1])
)
disk = stacks[source].pop()
stacks[to].append(disk)
if show:
print('Move disk', disk, 'from spindle', labels[source], 'to', labels[to])
## Recursive function 'Solve'
def solve(stacks, labels, n_disks=None, start=0, goal=2, spare=1, show=False, total_disks=None):
if n_disks is None:
n_disks = len(stacks[start])
if total_disks is None:
total_disks = n_disks
if n_disks <= 0:
return
if len(stacks[start]) < n_disks:
raise Exception(
"Not enough disks (" + str(n_disks) +
") on starting spindle " + labels[start]
)
## Recursive call
solve(stacks, labels, n_disks - 1, start, spare, goal, show, total_disks)
move(stacks, labels, start, goal, show)
if show:
print(tower_str(stacks, labels))
solve(stacks, labels, n_disks - 1, spare, goal, start, show, total_disks)
if n_disks == total_disks and show:
print("Puzzle complete")
# Example usage:
n_disks = 3
stacks, labels = create_tower_of_hanoi(n_disks)
print(tower_str(stacks, labels))
solve(stacks, labels, show=True)
Car
object could have attributes like color
and speed
, and methods like drive()
or brake()
.Student
object might store a name and grade, and have methods to enroll in courses or update grades.class Student:
def __init__(self, name, grade):
self.name = name
self.grade = grade
def enroll(self, course):
print(f"{self.name} enrolled in {course}")
def update_grade(self, new_grade):
self.grade = new_grade
print(f"{self.name}'s grade updated to {self.grade}")
def display_info(self):
print(f"Name: {self.name}, Grade: {self.grade}")
# Example usage:
student1 = Student("Alice", "A")
student1.enroll("Math")
student1.update_grade("A+")
student1.display_info()
Arrays
: