Displacement from origin after N moves of given distances in specified directions | Python3 program for the above approach ; Function to find the displacement from the origin and direction after performing the given set of moves ; Stores the distances travelled in the directions North , South , East , and West respectively ; Store the initial position of robot ; Traverse the array B [ ] ; If the current direction is North ; If the current direction is South ; If the current direction is East ; If the current direction is West ; Stores the total vertical displacement ; Stores the total horizontal displacement ; Find the displacement ; Print the displacement and direction after N moves ; Driver Code
from math import sqrt , floor NEW_LINE def finalPosition ( a , b , M ) : NEW_LINE INDENT n = 0 NEW_LINE s = 0 NEW_LINE e = 0 NEW_LINE w = 0 NEW_LINE p = ' N ' NEW_LINE for i in range ( M ) : NEW_LINE INDENT if ( p == ' N ' ) : NEW_LINE INDENT if ( a [ i ] == ' U ' ) : NEW_LINE INDENT p = ' N ' NEW_LINE n = n + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' D ' ) : NEW_LINE INDENT p = ' S ' NEW_LINE s = s + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' R ' ) : NEW_LINE INDENT p = ' E ' NEW_LINE e = e + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' L ' ) : NEW_LINE INDENT p = ' W ' NEW_LINE w = w + b [ i ] NEW_LINE DEDENT DEDENT elif ( p == ' S ' ) : NEW_LINE INDENT if ( a [ i ] == ' U ' ) : NEW_LINE INDENT p = ' S ' NEW_LINE s = s + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' D ' ) : NEW_LINE INDENT p = ' N ' NEW_LINE n = n + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' R ' ) : NEW_LINE INDENT p = ' W ' NEW_LINE w = w + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' L ' ) : NEW_LINE INDENT p = ' E ' NEW_LINE e = e + b [ i ] NEW_LINE DEDENT DEDENT elif ( p == ' E ' ) : NEW_LINE INDENT if ( a [ i ] == ' U ' ) : NEW_LINE INDENT p = ' E ' NEW_LINE e = e + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' D ' ) : NEW_LINE INDENT p = ' W ' NEW_LINE w = w + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' R ' ) : NEW_LINE INDENT p = ' S ' NEW_LINE s = s + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' L ' ) : NEW_LINE INDENT p = ' N ' NEW_LINE n = n + b [ i ] NEW_LINE DEDENT DEDENT elif ( p == ' W ' ) : NEW_LINE INDENT if ( a [ i ] == ' U ' ) : NEW_LINE INDENT p = ' W ' NEW_LINE w = w + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' D ' ) : NEW_LINE INDENT p = ' E ' NEW_LINE e = e + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' R ' ) : NEW_LINE INDENT p = ' N ' NEW_LINE n = n + b [ i ] NEW_LINE DEDENT elif ( a [ i ] == ' L ' ) : NEW_LINE INDENT p = ' S ' NEW_LINE s = s + b [ i ] NEW_LINE DEDENT DEDENT DEDENT ver_disp = n - s NEW_LINE hor_disp = e - w NEW_LINE displacement = floor ( sqrt ( ( ver_disp * ver_disp ) + ( hor_disp * hor_disp ) ) + 1 ) NEW_LINE print ( displacement , p ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ ' U ' , ' R ' , ' R ' , ' R ' , ' R ' ] NEW_LINE B = [ 1 , 1 , 1 , 1 , 0 ] NEW_LINE N = len ( A ) NEW_LINE finalPosition ( A , B , N ) NEW_LINE DEDENT