
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Program to get final position of moving animals when they stops in Python
Suppose we have a string s that is representing the initial conditions of some animals. Each animal can take one of three values: L, indicates the animal moved to left. R, indicates the animal moved to right. @, indicates the animal is standing still. Animals moving on a direction will pick up other animals unless the animal receives a force from the opposite direction. Then, it will stand still. We have to find the orientation of each animal when the animal stop moving.
So, if the input is like s = "@@L@R@@@@L", then the output will be "LLL@RRRLLL"
To solve this, we will follow these steps −
levels := a list of size same as s and fill with -1
q := a double ended queue
for idx is in range 0 to size of s, do
if s[idx] is same as "R" or s[idx] is same as "L", then
insert (idx, 0, s[idx]) at the end of q
l := a new list of characters of s
while q is not empty, do
(idx, new_level, dir) := left element of q, and delete it from q
if levels[idx] is same as -1, then
levels[idx] := new_level
l[idx] := dir
if dir is same as "R" and idx + 1 < size of l , then
insert (idx + 1, new_level + 1, dir) at the end of q
otherwise when dir is same as "L" and idx - 1 >= 0, then
insert (idx - 1, new_level + 1, dir) at the end of q
otherwise when levels[idx] is same as new_level, then
if l[idx] is not same as dir, then
l[idx] := "@"
return a string by joining elements of l
Example
Let us see the following implementation to get a better understanding −
from collections import deque class Solution: def solve(self, s): levels = [-1 for i in s] q = deque() for idx in range(len(s)): if s[idx] == "R" or s[idx] == "L": q.append((idx, 0, s[idx])) l = list(s) while q: idx, new_level, dir = q.popleft() if levels[idx] == -1: levels[idx] = new_level l[idx] = dir if dir == "R" and idx + 1 < len(l): q.append((idx + 1, new_level + 1, dir)) elif dir == "L" and idx - 1 >= 0: q.append((idx - 1, new_level + 1, dir)) elif levels[idx] == new_level: if l[idx] != dir: l[idx] = "@" return "".join(l) ob = Solution() s = "@@L@R@@@@L" print(ob.solve(s))
Input
"@@L@R@@@@L"
Output
LLL@RRRLLL
- Related Articles
- Does earthworm secrete slimy substance when they are moving?
- Program to get final string after shifting characters with given number of positions in Python
- Program to find final states of rockets after collision in python
- Find the final X and Y when they are Altering under given condition in C++
- Program to find minimum number of busses are required to pass through all stops in Python
- Final cell position in the matrix in C++
- Find the final X and Y when they are Altering under given condition in C++ Programming
- Program to Find Out the Maximum Final Power of a List in Python
- Program to find minimum number of buses required to reach final target in python
- Program to count minimum number of animals which have no predator in Python
- Program to find cost to reach final index of any of given two lists in Python
- C++ code to count steps to reach final position by robot
- C++ Program to get blocks position after right side rotation
- Program to check final answer by performing given stack operations in Python
- Program to find the number of possible position in a line in Python
