அல்கோரிதம்stack simulation

Stack simulation — process model

TT
Testlaa Team
May 14, 20261 min read

Reversible/nested process — push/pop simulation.

Try this in Python

def reduce_path(path: str) -> str:
    st: list[str] = []
    for part in path.split("/"):
        if part in ("", "."):
            continue
        if part == "..":
            if st:
                st.pop()
        else:
            st.append(part)
    return "/" + "/".join(st)


print(reduce_path("/a/./b/../../c/"))

Key takeaways

  • Directory — frame; .. — pop.
  • Browser history / undo.
  • Invalid pop behavior வரையறு.

Tags:

Stacks & queuesPythonமாணவர்கள்