

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to check robot can reach target position or not in Python
Suppose we have a robot, that is currently sitting in at position (0, 0) (Cartesian plane). If we have list of its moves that it can make, containing N(North), S(South), W(West), and E(East). We have to check whether it can reach at destination coordinate (x, y).
So, if the input is like moves = ['N','N','E','E','S'], (x,y) = (2,1), then the output will be True,
To solve this, we will follow these steps −
- temp_coord := [0,0]
- for each move in moves, do
- if move is same as "N", then
- temp_coord[1] := temp_coord[1] + 1
- otherwise when move is same as "S", then
- temp_coord[1] := temp_coord[1] - 1
- otherwise when move is same as "E", then
- temp_coord[0] := temp_coord[0] + 1
- otherwise when move is same as "W", then
- temp_coord[0] := temp_coord[0] - 1
- if move is same as "N", then
- return True when temp_coord[0] is same as coord[0] and temp_coord[1] is same as coord[1] otherwise false.
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, moves, coord): temp_coord = [0,0] for move in moves: if move == "N": temp_coord[1] += 1 elif move == "S": temp_coord[1] -= 1 elif move == "E": temp_coord[0] += 1 elif move == "W": temp_coord[0] -= 1 return temp_coord[0] == coord[0] and temp_coord[1] == coord[1] ob = Solution() moves = ['N','N','E','E','S'] coord = [2,1] print(ob.solve(moves, coord))
Input
['N','N','E','E','S'], [2,1]
Output
True
- Related Questions & Answers
- Program to check we can reach leftmost or rightmost position or not in Python
- C++ code to check grasshopper can reach target or not
- Program to check robot can reach target by keep moving on visited spots in Python
- Program to check we can reach at position n by jumping or not in Python
- Program to check we can update a list index by its current sum to reach target or not in python
- Program to check person can reach top-left or bottomright cell avoiding fire or not in Python
- Program to check whether we can reach last position from index 0 in Python
- Program to check whether robot is moving inside a bounded box or not in Python
- C++ code to count steps to reach final position by robot
- Program to find minimum steps to reach target position by a chess knight in Python
- Program to check we can spell out the target by a list of words or not in Python
- Program to check robbers can rob the vault or not in Python
- C++ Program to check k rupees are enough to reach final cell or not
- Python program to check whether we can pile up cubes or not
- C++ Program to check player position is dangerous or not in football match
Advertisements