distance between multiple locations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jcl43
    New Member
    • Nov 2008
    • 6

    distance between multiple locations

    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..

    Code:
    def total_distances(cities):
        distance = 0.0
        for i in cities():
            distance = distance + get_distance(x1,y1,x2,y2)
        return distance
    and this is my previous function that I used in the total_distance function that calculates the distance between two cities
    Code:
    def get_distance(x1,y1,x2,y2): 
        return sqrt(((x2-x1)**2) + ((y2-y1)**2))
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    I think you're going to need nested for loops. Here's my idea for the pseudocode:
    Code:
    make a list of distances
    for each city:
        set a distance variable to zero
        for each other city:
            add the distance between the cities to distance
        put distance in the list
    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.

    Comment

    • jcl43
      New Member
      • Nov 2008
      • 6

      #3
      Originally posted by boxfish
      I think you're going to need nested for loops. Here's my idea for the pseudocode:
      Code:
      make a list of distances
      for each city:
          set a distance variable to zero
          for each other city:
              add the distance between the cities to distance
          put distance in the list
      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.
      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 :
      Code:
       distance += get_distance(i[1],i[0],n[1],n[0])
      Code:
      def total_distance(cities):
          total = []
          for i in cities:
              distance = 0
              for n in cities:
                  distance += get_distance(i[1],i[0],n[1],n[0])
                  total.append(distance)
          return distance

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Code:
        def total_distance(cities):
            total = []
            for i in cities:
                distance = 0
                for n in cities:
                    distance += get_distance(i[1],i[0],n[1],n[0])
                    total.append(distance)
            return distance
        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:
        Code:
        def total_distance(cities):
            total = []
            for i in cities:
                distance = 0
                for n in cities:
                    distance += get_distance(i[1],i[0],n[1],n[0])
                total.append(distance)
            return distance
        I hope I have this right and that it's helpful. Good luck.

        Comment

        Working...