
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find minimum jump needed to return from a folder to home in Python
Suppose we have a logs where we have path to enter into folders, there may be different symbols like −
"../" : Move to the parent folder from current one. (If we are at main folder, do not change location).
"./" : Remain in the current folder.
"x/" : Move to the child folder named x.
From the logs we have to find minimum number of operations needed to come back from last folder where we stop to the main folder.
So, if the input is like logs = ["Dir1/","Dir2/","../","Dir2/","Dir3/","./"], then the output will be 3
from the image we can see we have to step back thrice to reach home.
To solve this, we will follow these steps −
stk := a new list
for each item i in logs, do
if i is same as "../" and size of stk > 0, then
delete last element from stk
otherwise when i is not same as "./" and i is not same as "../", then
insert i at the end of stk
otherwise,
go for next iteration
return number of items in the stk
Example (Python)
Let us see the following implementation to get better understanding −
def solve(logs): stk = [] for i in logs: if i == "../" and len(stk) > 0: stk.pop() elif i != "./" and i != "../": stk.append(i) else: continue return len(stk) logs = ["Dir1/","Dir2/","../","Dir2/","Dir3/","./"] print(solve(logs))
Input
["Dir1/","Dir2/","../","Dir2/","Dir3/","./"]
Output
3
- Related Articles
- C++ code to find minimum jump to reach home by frog
- Program to find minimum jumps to reach home in Python
- Program to find out the minimum rotations needed to maximize the profit from a Ferris wheel in Python
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find minimum element addition needed to get target sum in Python
- Program to find minimum costs needed to fill fruits in optimized way in Python
- Program to find minimum number of rocketships needed for rescue in Python
- Program to find minimum amount needed to be paid all good performers in Python
- Program to find minimum operations needed to make two arrays sum equal in Python
- C++ program to find minimum number of steps needed to move from start to end
- Program to count minimum deletions needed to make character frequencies unique in Python
- Program to check minimum number of characters needed to make string palindrome in Python
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- C++ program to find minimum how many operations needed to make number 0
- C++ program to find minimum how many coins needed to buy binary string
