Distance calculator

0 votes

A robot moves in a plane starting from the origin point[0,0].. The robot can move toward UP, DOWN, LEFT, RIGHT. The trace of robot movements is as given following:

UP 5
DOWN 3
LEFT 3
RIGHT 2
The numbers after directions are steps. Write a program to compute the distance current position after sequence of movements.
Nov 13, 2020 in Python by Raju
• 120 points
294 views

1 answer to this question.

+1 vote

Hello @

Try this code:

import math

x, y = 0, 0

while True:
    step = input("Type in UP/DOWN/LEFT/RIGHT #step number: ")

    if step == "":
        break

    else:
        step = step.split(" ")

        if step[0] == "UP":
            y = y + int(step[1])
        elif step[0] == "DOWN":
            y = y - int(step[1])
        elif step[0] == "LEFT":
            x = x - int(step[1])
        elif step[0] == "RIGHT":
            x = x + int(step[1])

c = math.sqrt(x**2 + y**2)

print("Distance:", c)
answered Nov 13, 2020 by Niroj
• 82,720 points

Related Questions In Python

+1 vote
2 answers

Measuring the distance between pixels on OpenCv with Python

You can try this: Mat pts1(nPts, 1, CV_8UC2), ...READ MORE

answered Aug 24, 2018 in Python by Omkar
• 69,130 points
7,800 views
0 votes
1 answer

Plot a k-distance graph in python

You probably want to use the matrix ...READ MORE

answered Sep 5, 2018 in Python by Priyaj
• 58,120 points
3,495 views
0 votes
1 answer

Measuring the distance between pixels on OpenCv with Python

Assuming input frames will have "close to ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,120 points
3,143 views
0 votes
1 answer

Measuring the distance between pixels on OpenCv with Python

Assuming input frames will have "close to ...READ MORE

answered Sep 21, 2018 in Python by Priyaj
• 58,120 points
946 views
+4 votes
2 answers

AWS Save & Share while using AWS Calculator

After doing your estimate, click the "Estimate ...READ MORE

answered Oct 11, 2018 in Cloud Computing by findingbugs
• 4,790 points
636 views
0 votes
1 answer

Use different distance formula other than euclidean distance in k means

K-means is based on variance minimization. The sum-of-variance formula ...READ MORE

answered Jun 21, 2018 in Data Analytics by Sahiti
• 6,380 points
730 views
+1 vote
2 answers

AWS: Long Distance Movement to S3 buckets

This description may interest you, check the ...READ MORE

answered Aug 30, 2018 in Cloud Computing by eatcodesleeprepeat
• 4,770 points
2,519 views