Position of robot after given movements
Last Updated :
27 Jul, 2022
Given a robot which can only move in four directions, UP(U), DOWN(D), LEFT(L), RIGHT(R). Given a string consisting of instructions to move. Output the coordinates of a robot after executing the instructions. Initial position of robot is at origin(0, 0).
Examples:
Input : move = "UDDLRL"
Output : (-1, -1)
Explanation:
Move U : (0, 0)--(0, 1)
Move D : (0, 1)--(0, 0)
Move D : (0, 0)--(0, -1)
Move L : (0, -1)--(-1, -1)
Move R : (-1, -1)--(0, -1)
Move L : (0, -1)--(-1, -1)
Therefore final position after the complete
movement is: (-1, -1)
Input : move = "UDDLLRUUUDUURUDDUULLDRRRR"
Output : (2, 3)
Source: Goldman Sachs Interview Experience | Set 36 .
Approach: Count number of up movements (U), down movements (D), left movements (L) and right movements (R) as countUp, countDown, countLeft and countRight respectively. Final x-coordinate will be
(countRight – countLeft) and y-coordinate will be (countUp – countDown).
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
void finalPosition(string move)
{
int l = move.size();
int countUp = 0, countDown = 0;
int countLeft = 0, countRight = 0;
for ( int i = 0; i < l; i++)
{
if (move[i] == 'U' )
countUp++;
else if (move[i] == 'D' )
countDown++;
else if (move[i] == 'L' )
countLeft++;
else if (move[i] == 'R' )
countRight++;
}
cout << "Final Position: ("
<< (countRight - countLeft)
<< ", " << (countUp - countDown)
<< ")" << endl;
}
int main()
{
string move = "UDDLLRUUUDUURUDDUULLDRRRR" ;
finalPosition(move);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void finalPosition(String move)
{
int l = move.length();
int countUp = 0 , countDown = 0 ;
int countLeft = 0 , countRight = 0 ;
for ( int i = 0 ; i < l; i++)
{
if (move.charAt(i) == 'U' )
countUp++;
else if (move.charAt(i) == 'D' )
countDown++;
else if (move.charAt(i) == 'L' )
countLeft++;
else if (move.charAt(i) == 'R' )
countRight++;
}
System.out.println( "Final Position: ("
+ (countRight - countLeft) + ", "
+ (countUp - countDown) + ")" );
}
public static void main(String[] args)
{
String move = "UDDLLRUUUDUURUDDUULLDRRRR" ;
finalPosition(move);
}
}
|
Python3
def finalPosition(move):
l = len (move)
countUp, countDown = 0 , 0
countLeft, countRight = 0 , 0
for i in range (l):
if (move[i] = = 'U' ):
countUp + = 1
elif (move[i] = = 'D' ):
countDown + = 1
elif (move[i] = = 'L' ):
countLeft + = 1
elif (move[i] = = 'R' ):
countRight + = 1
print ( "Final Position: (" , (countRight - countLeft),
", " , (countUp - countDown), ")" )
if __name__ = = '__main__' :
move = "UDDLLRUUUDUURUDDUULLDRRRR"
finalPosition(move)
|
C#
using System;
class GFG {
static void finalPosition(String move)
{
int l = move.Length;
int countUp = 0, countDown = 0;
int countLeft = 0, countRight = 0;
for ( int i = 0; i < l; i++)
{
if (move[i] == 'U' )
countUp++;
else if (move[i] == 'D' )
countDown++;
else if (move[i] == 'L' )
countLeft++;
else if (move[i] == 'R' )
countRight++;
}
Console.WriteLine( "Final Position: ("
+ (countRight - countLeft) + ", "
+ (countUp - countDown) + ")" );
}
public static void Main()
{
String move = "UDDLLRUUUDUURUDDUULLDRRRR" ;
finalPosition(move);
}
}
|
PHP
<?php
function finalPosition( $move )
{
$l = strlen ( $move );
$countUp = 0;
$countDown = 0;
$countLeft = 0;
$countRight = 0;
for ( $i = 0; $i < $l ; $i ++) {
if ( $move [ $i ] == 'U' )
$countUp ++;
else if ( $move [ $i ] == 'D' )
$countDown ++;
else if ( $move [ $i ] == 'L' )
$countLeft ++;
else if ( $move [ $i ] == 'R' )
$countRight ++;
}
echo "Final Position: ("
. ( $countRight - $countLeft )
. ", " , ( $countUp - $countDown )
. ")" . "\n" ;
}
$move = "UDDLLRUUUDUURUDDUULLDRRRR" ;
finalPosition( $move );
?>
|
Javascript
<script>
function finalPosition(move)
{
let l = move.length;
let countUp = 0, countDown = 0;
let countLeft = 0, countRight = 0;
for (let i = 0; i < l; i++)
{
if (move[i] == 'U' )
countUp++;
else if (move[i] == 'D' )
countDown++;
else if (move[i] == 'L' )
countLeft++;
else if (move[i] == 'R' )
countRight++;
}
document.write( "Final Position: ("
+ (countRight - countLeft) + ", "
+ (countUp - countDown) + ")" );
}
let move = "UDDLLRUUUDUURUDDUULLDRRRR" ;
finalPosition(move);
</script>
|
Output
Final Position: (2, 3)
Share your thoughts in the comments
Please Login to comment...