By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

distance between multiple locations

P: 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
Share this Question
Share on Google+
3 Replies


boxfish
Expert 100+
P: 469
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

P: 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
Expert 100+
P: 469
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.