[Python] Python stack implementation for maze navigation with scoring (fixed capacity)

Discussão em 'Python' iniciado por Stack, Novembro 29, 2025.

  1. Stack

    Stack Membro Participativo

    I am a university student working on a Data Structures lab about simulating maze navigation using a stack with limited memory.

    The stack must have a fixed capacity of 25 moves, and the movement rules are:


    • Move forward → push(direction) → +10 points


    • Dead-end → pop() → –5 points


    • Current position → peek/top


    • Try to push when stack is full → overflow (–20 points)


    • Try to pop when stack is empty → underflow


    • Exit reached → +50 points

    Example movement sequence:

    push("Left")
    push("Right")
    pop() # dead-end
    push("Forward")
    push("Left")
    push(...) # beyond stack capacity → test overflow
    pop repeatedly # return to start


    What I need help with:


    • Stack implementation using a Python list (no built-in stack libraries)


    • Handling overflow, underflow, and scoring


    • Printing stack content and score after each move

    Missing Code:
    I started defining a stack class but I am unsure how to apply scoring and full/empty conditions properly.

    class Stack:
    def __init__(self, capacity=25):
    self.items = []
    self.capacity = capacity
    self.score = 0

    def push(self, direction):
    # I am not sure how to handle scoring and overflow here
    if len(self.items) < self.capacity:
    self.items.append(direction)
    # self.score += 10 → not sure if this is correct
    else:
    # Should I use return, print, or raise error here?
    # self.score -= 20
    pass

    def pop(self):
    # Confused about underflow and scoring
    if len(self.items) > 0:
    direction = self.items.pop()
    # self.score -= 5
    return direction
    else:
    # Stack is empty → underflow
    return None

    def peek(self):
    if len(self.items) > 0:
    return self.items[-1]
    return None # not sure if this is correct

    # Not sure how to simulate the movement sequence correctly
    maze_moves = ["Left", "Right", "dead-end", "Forward", "Left"]

    # I want to simulate push/pop based on this list, but confused on logic
    stack = Stack()
    for move in maze_moves:
    if move == "dead-end":
    stack.pop()
    else:
    stack.push(move)

    print(stack.items, stack.score)


    I’m not sure if I’m managing scoring and overflow/underflow correctly inside the methods. I want to properly apply the rules (+10 push, –5 pop, –20 overflow). Can someone help me correct this implementation and show the right way to simulate the maze example step-by-step?

    Preferred language: Python
    Thanks!

    Continue reading...

Compartilhe esta Página