#Python Program to calculate the city equidistant between two given cities
#Sort the cities by arranging the distances from greatest to least
#Define function "sorting" 
def sorting(numbers): 
    nums = numbers
#Loop will execute over each city until they are arranged least to greatest distance
    for i in range(len(nums)):
#List includes the last entry as well by accounting for i+1
        for j in range(i+1, len(nums)):
            if numbers[j] < numbers[i]:
#When numbers[j] is less than numbers[i] they are equal to their conjugate  
                numbers[j], numbers[i] = numbers[i], numbers[j]
#In this case return "numbers"
    return numbers

def distance(i,j):
    cities = []
#Range is a pre defined 20 by 20 matrix
    for a in range (0,20):
#Append adds a sinle element to the end of cities
        cities.append(0)
#Pre defined list or matrix of city's distances to one another, not yet arranged least to greatest
    cities[0] = [0,106,517,542,376,377,412,511,214,535,29,39,103,580,26,438,43,513,401,396]
    cities[1] = [106,0,262,490,507,633,38,22,84,540,562,138,80,47,522,183,495,465,574,156]
    cities[2] = [517,262,0,629,490,42,216,143,427,524,56,118,5,483,519,554,535,576,297,140]
    cities[3] = [542,490,629,0,560,44,358,262,649,607,42,339,421,33,393,262,531,289,98,32]
    cities[4] = [376,507,490,560,0,35,316,242,171,626,326,121,370,285,550,51,457,322,335,33]
    cities[5] = [377,633,42,44,35,0,619,607,538,240,357,388,649,313,23,588,361,478,272,554]
    cities[6] = [412,38,216,358,316,619,0,75,128,641,86,380,323,193,430,327,651,370,579,570]
    cities[7] = [511,22,143,262,242,607,75,0,506,52,336,283,291,270,290,587,250,497,90,574]
    cities[8] = [214,84,427,649,171,538,128,506,0,254,272,130,349,305,192,370,49,570,163,497]
    cities[9] = [535,540,524,607,626,240,641,52,254,0,214,607,605,200,200,267,227,71,606,184]
    cities[10] = [29,562,56,42,326,357,86,336,272,214,0,379,574,193,497,287,495,250,157,328]
    cities[11] = [39,138,118,339,121,388,380,283,130,607,379,0,566,573,80,139,5,404,261,631]
    cities[12] = [103,80,5,421,370,649,323,291,349,605,574,566,0,466,252,236,448,427,533,632]
    cities[13] = [580,47,483,33,285,313,193,270,305,200,193,573,466,0,184,479,207,176,49,306]
    cities[14] = [26,522,519,393,550,23,430,290,192,200,497,80,252,184,0,15,464,123,414,305]
    cities[15] = [438,183,554,262,51,588,327,587,370,267,287,139,236,479,15,0,319,41,152,560]
    cities[16] = [43,495,535,531,457,361,651,250,49,227,495,5,448,207,464,319,0,538,286,99]
    cities[17] = [513,465,576,289,322,478,370,497,570,71,250,404,427,176,123,41,538,0,517,378]
    cities[18] = [401,574,297,98,335,272,579,90,163,606,157,261,533,49,414,152,286,517,0,243]
    cities[19] = [396,156,140,32,33,554,570,574,497,184,328,631,632,306,305,560,99,378,243,0]
    sums = []
    for b in range(0,20):
#For entry within the matrix if that value happens to be of type list, then it will append a value to that list
        sums.append(cities[i][b] + cities[j][b])
    for b in cities[i]:
        for c in cities[j]:
            sums.append(b+c)
#Execution of function is given start point 0 for both sums to work from
    sums[i] = 0
    sums[j] = 0
 #print "sums ", sums
    sumsToSort = sums[:]
    sortedSums=sorting(sumsToSort)
#print "sortedSums ", sortedSums
#print sortedSums[2]
#print "new sums", sums
    city = sums.index(sortedSums[2])
    print ("The city with the minimum total distance is City", city, "with a distance of", sortedSums[2])
    return city
