469,323 Members | 1,522 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

distance between multiple locations

6
so after that part, I have to make a function that returns a list of total distances between each of the cities in the cities list (a list of pair-lists) and I have to have it so the function calculates the total distances between each city and all the other cities. The index of the results list should correspond to the index in the cities list so that index i in the results list represents the total distance between cities[i] and all the other cities.

I understand that it's telling me what to do but don't understand how to write it out :(

right now I have this as the function to calaculate the total distance but it seems as if i'm missing something..

Expand|Select|Wrap|Line Numbers
  1.  
  2. def total_distances(cities):
  3.     distance = 0.0
  4.     for i in cities():
  5.         distance = distance + get_distance(x1,y1,x2,y2)
  6.     return distance
  7.  
and this is my previous function that I used in the total_distance function that calculates the distance between two cities
Expand|Select|Wrap|Line Numbers
  1. def get_distance(x1,y1,x2,y2): 
  2.     return sqrt(((x2-x1)**2) + ((y2-y1)**2))
  3.  
Nov 19 '08 #1
3 5804
boxfish
469 Expert 256MB
I think you're going to need nested for loops. Here's my idea for the pseudocode:
Expand|Select|Wrap|Line Numbers
  1. make a list of distances
  2. for each city:
  3.     set a distance variable to zero
  4.     for each other city:
  5.         add the distance between the cities to distance
  6.     put distance in the list
  7.  
You won't need to worry about adding the distance between a city and the same city, because, being zero, it won't affect the sum.
Nov 19 '08 #2
jcl43
6
@boxfish
ok.so now i have it like this but I how do i add the distance between the citeis to the distance ?
which is this part :
Expand|Select|Wrap|Line Numbers
  1.  distance += get_distance(i[1],i[0],n[1],n[0])
  2.  
Expand|Select|Wrap|Line Numbers
  1. def total_distance(cities):
  2.     total = []
  3.     for i in cities:
  4.         distance = 0
  5.         for n in cities:
  6.             distance += get_distance(i[1],i[0],n[1],n[0])
  7.             total.append(distance)
  8.     return distance
  9.  
Nov 25 '08 #3
boxfish
469 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. def total_distance(cities):
  2.     total = []
  3.     for i in cities:
  4.         distance = 0
  5.         for n in cities:
  6.             distance += get_distance(i[1],i[0],n[1],n[0])
  7.             total.append(distance)
  8.     return distance
  9.  
I don't see a problem with how you find the distance between the two cities. The thing that seems wrong is that the line where you append distance to total is inside of the second for loop. That means that every time you add a distance to your distance variable, you append the current tally to total. If I understand your assignment correctly, you're only supposed to append the total distance to total, so you want:
Expand|Select|Wrap|Line Numbers
  1. def total_distance(cities):
  2.     total = []
  3.     for i in cities:
  4.         distance = 0
  5.         for n in cities:
  6.             distance += get_distance(i[1],i[0],n[1],n[0])
  7.         total.append(distance)
  8.     return distance
  9.  
I hope I have this right and that it's helpful. Good luck.
Nov 25 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

20 posts views Thread by Xenophobe | last post: by
3 posts views Thread by byteschreck | last post: by
9 posts views Thread by nottheartistinquestion | last post: by
18 posts views Thread by lovecreatesbea... | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.