SStackSundial
Workflows

Coding workflows · Bounded postfix arithmetic

03
Built around your work

A concrete starting point

Evaluate one through 100 postfix tokens using bounded integers and the operators +, -, * and //. Check negative floor division and reject division by zero, an incomplete stack or any intermediate magnitude above 10**18.

01

Open the context

Choose the project folder and name the files or behavior involved.

02

Define the change

Describe the input, expected output and boundaries that must stay intact.

03

Inspect the work

Review the proposed edits and any commands before relying on their result.

04

Check the outcome

Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.

Bounded postfix arithmetic

A concrete starting point
def postfix_value(tokens):
    tokens = tuple(tokens)
    if not 1 <= len(tokens) <= 100:
        raise ValueError("tokens.limit")
    stack = []
    for token in tokens:
        if type(token) is int and abs(token) <= 1000000000:
            stack.append(token)
            continue
        if type(token) is not str or token not in ("+", "-", "*", "//") or len(stack) < 2:
            raise ValueError("token.invalid")
        right, left = stack.pop(), stack.pop()
        if token == "//" and right == 0:
            raise ValueError("division.zero")
        value = (left + right if token == "+" else left - right if token == "-" else left * right if token == "*" else left // right)
        if abs(value) > 10**18:
            raise ValueError("result.range")
        stack.append(value)
    if len(stack) != 1:
        raise ValueError("stack.remainder")
    return stack[0]

The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.

AI coding workspace

Your next change starts here.

Get the Windows app ↗