Skip to content
← Back to Community
Map Cordinates Python
Profile icon
CodingSL

A road map defines locations as map references like B3, where B is the x-coordinate value and 3 is the y-coordinate.

image

The grid lines are 0.5 km apart.

I'm writing a program that allows the user to enter a trip as a sequence of any number of map references on one line, and reports the total length of the trip, assuming they can travel in straight lines.
Like in the below picture:

image

Can please somebody instruct me how to do this?

Answered by SixBeeps [earned 5 cycles]
View Answer
Voters
Profile icon
CodingSL
Comments
hotnewtop
Profile icon
SixBeeps

This sounds like a job for the distance formula!
To calculate the distance between two points in 2D space, use this formula:

![image]
(https://storage.googleapis.com/replit/images/1588858280061_e609e5852afab9c326d22d40ab6d33dd.png)

where the X's and Y's are the coordinates of the two points.
All you'd need to do then is to convert those letters into points, prefferably by keeping an array of letters and finding the index of each.

Profile icon
CodingSL

How can I do that
@SixBeeps

Profile icon
SixBeeps

@CodingSL For the distance formula, you should only need your language's sqrt function, then just do simple mathematical functions from there.

For the letter conversion, I'd do something like

arrayOfLetters = ['A', 'B', 'C', ... , 'Y', 'Z'] indexOfT = arrayOfLetters.index('T')
Profile icon
CodingSL

Can give me the code to do this please?
@SixBeeps

Profile icon
SixBeeps

@CodingSL I'm not allowed to give you code

Profile icon
CodingSL

It's OK,
Can you instruct me on how to do this from the beginning?
I'm new to Python Coding and I don't know much.
@SixBeeps

Profile icon
SixBeeps

@CodingSL Do you want to do a Multiplayer Repl instead? I feel like that would be easier.

Profile icon
CodingSL

Ok I'll send an invite link.
@SixBeeps

Profile icon
CodingSL