Advertisement
Guest User

store_location

a guest
Oct 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. import time
  2. import csv
  3. import random
  4.  
  5. def sumDistance(lst, median):
  6.     """
  7.    Find the total distance from each building to the optimal store location
  8.    :param lst: Given list
  9.    :param median: Calculated median of give list
  10.    :return: sum of distances
  11.    """
  12.     total = 0
  13.  
  14.     for location in range(0, len(lst) - 1):
  15.         total += abs(median - lst[location])
  16.  
  17.     return total
  18.  
  19.  
  20. def getList(filename):
  21.     with open(filename) as csvfile:
  22.         distances = []
  23.         readCSV = csv.reader(csvfile, delimiter=' ')
  24.         for line in readCSV:
  25.             dist = line[1]
  26.             distances.append(int(dist))
  27.  
  28.         return distances
  29.  
  30.  
  31. def testComp():
  32.     testCase(getList("large.csv"), "largeDataset")
  33.     testCase(getList("even.csv"), "evenDataset")
  34.     testCase(getList("odd.csv"), "oddDataset")
  35.  
  36.  
  37. def testCase(lst, name):
  38.     start = time.time()
  39.     sumDistance(lst, quickSelect(lst, quickSelectHelper(lst)))
  40.     end = time.time()
  41.     print("Test case", name, ", elapsed time: ", end - start, " seconds")
  42.  
  43.  
  44. def quickSelectHelper(L):
  45.     """
  46.    Find the index of the correct median depending on the length of the list (odd or even)
  47.    :param L: Given list
  48.    :return: the Median the needs to be translated (k'th element)
  49.    """
  50.     if len(L) % 2 == 0 and len(L) >= 2:
  51.         idx1 = int((0.5 * len(L)) - 1)
  52.         idx2 = (len(L) // 2)
  53.         calculatedMedian = (L[idx1] + L[idx2]) // 2
  54.     elif len(L) % 2 != 0 and len(L) >= 1:
  55.         calculatedMedian = (0.5 * len(L)) - 0.5
  56.  
  57.     return calculatedMedian
  58.  
  59.  
  60. def quickSelect(aList, k):
  61.     """
  62.    Sort the list until the k'th element is found
  63.    :param aList: given list
  64.    :param k: the element to find (k'th)
  65.    :return: The median value of the given list
  66.    """
  67.     # print("qS at it again: ", aList, "with a length of: ", len(aList))
  68.     if len(aList) != 0:
  69.         pivot = aList[len(aList) // 2]
  70.         smallerList = []
  71.         largerList = []
  72.         count = 0
  73.  
  74.         for dist in range(len(aList)):
  75.             if aList[dist] > pivot:
  76.                 largerList.append(aList[dist])
  77.             elif aList[dist] < pivot:
  78.                 smallerList.append(aList[dist])
  79.             else:
  80.                 count += 1
  81.  
  82.         m = len(smallerList)
  83.  
  84.         if m <= k < m + count:
  85.             return pivot
  86.         elif m > k:
  87.             return quickSelect(smallerList, k)
  88.         else:
  89.             return quickSelect(largerList, k - m - count)
  90.  
  91.     return aList[0]
  92.  
  93. def randomList(numElements, sizeRange):
  94.     lst = []
  95.     for num in range(numElements):
  96.         lst.append(random.randint(0, sizeRange))
  97.     return lst
  98.  
  99.  
  100. def main():
  101.     # testComp()
  102.     lst = randomList(12, 100)
  103.     print(lst)
  104.     # print("\n", "Final return value: ", quickSelect(lst, quickSelectHelper(lst)))
  105.     # lst = getList("large.csv")
  106.     x = quickSelectHelper(lst)
  107.     print("X: ", x)
  108.     y = quickSelect(lst, x)
  109.     print("Y: ", y)
  110.     z = sumDistance(lst, y)
  111.     print("Z: ", z)
  112.  
  113.  
  114. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement