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 Edureka
• 120 points
3,950 views

1 answer to this question.

0 votes

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,840 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,220 points
10,553 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,020 points
4,973 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,020 points
5,704 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,020 points
2,729 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,780 points
1,811 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,370 points
1,647 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,710 points
4,382 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP