Advertisement
Guest User

myProject07

a guest
Oct 29th, 2018
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.73 KB | None | 0 0
  1. def keywordSearchDict(keyword, myDict):
  2.     """
  3.    Find the value(s) of the keyword
  4.    :param keyword: Given key to search for
  5.    :param myDict: The disctionary to search in
  6.    :return: The value(s) of the keyword
  7.    """
  8.     userKey = keyword  # Set the keyword to a string
  9.     if userKey in myDict:  # If the keyword is in the dict
  10.         for key in myDict.keys():  # Iterate all over keys in myDict
  11.             if userKey == key:  # If the userKey matches the dictKey
  12.                 return myDict.get(key)  # Return the values of the key
  13.     else:
  14.         return None  # Or don't
  15.  
  16.  
  17. def sortLargest2Small(itemList):
  18.     """
  19.    Sort the given list from largest to smallest
  20.    :param itemList: Given list
  21.    :return: The list sorted largest to smallest
  22.    """
  23.     sortedList = sorted(itemList, reverse=True)
  24.     return sortedList
  25.  
  26.  
  27. def makeSimpleDict(aList):
  28.     """
  29.    Make a dict for item:values
  30.    :param aList: list of item, value pairs
  31.    :return: item dictionary
  32.    """
  33.     myDict = {}
  34.     keyList = []
  35.     valueList = []
  36.  
  37.     for pair in aList:
  38.         keyList.append(pair[0])
  39.         valueList.append(int(pair[1]))
  40.  
  41.     for idx in range(len(keyList)):  # For every index in the wordlist
  42.         myDict[keyList[idx]] = [valueList[idx]]  # Add that word sorted with a key of that word
  43.  
  44.     return myDict
  45.  
  46.  
  47. def makeComplexDict(keyList):
  48.     """
  49.    Make a dict for box:weight
  50.    :param keyList: The weights n boxes
  51.    :return: Dictionary of boxes and max weight
  52.    """
  53.     myDict = {}
  54.     count = 0
  55.     for idx in range(len(keyList)):
  56.         boxName = "Box" + str(count)
  57.         count += 1
  58.  
  59.         myDict[boxName] = [int(keyList[idx])]
  60.         myDict[boxName].append(0)
  61.  
  62.     return myDict
  63.  
  64.  
  65. def roomiest(boxList, itemList, boxDict, itemDict):
  66.     """
  67.    For each item find the box with the greatest remaining allowed weight that can support the item and place the item in that box
  68.    :param boxList: The sorted list of boxes( large to small )
  69.    :param itemList: The sorted list of items ( large to small )
  70.    :param boxDict: Dict w/ boxes
  71.    :param itemDict: Dict w/ items
  72.    :return: If boxes were able to fit all items(1); items in box with individual weights(2); Box name with max
  73.    weight(3); items with their weights that were left behind(4)
  74.    """
  75.     sortedItems = sortLargest2Small(itemList)
  76.     sortedBoxes = sortLargest2Small(boxList)
  77.  
  78.     for item in sortedItems:
  79.         for box in sortedBoxes:
  80.             itemWeight = keywordSearchDict(item, itemDict)
  81.             boxRemWeight = keywordSearchDict(box, boxDict)
  82.             if itemWeight <= boxRemWeight:
  83.                 itemDict[
  84.                     box] =  # Need to add item to box with its weight as well as modify the remaining weight the box
  85.                             # can hold
  86.  
  87.  
  88. def fileReader(filename):
  89.     """
  90.    Take the given txt file and format into proper list for boxes and items
  91.    :param filename: The filename of the text file
  92.    :return: Send lists to make a dictionary from them
  93.    """
  94.     with open(filename, 'r') as myFile:  # Open the correct file
  95.         itemList = []
  96.         boxString = ""
  97.  
  98.         myList = [line.split() for line in myFile.readlines()]
  99.         boxLine = str(myList[0:1])
  100.  
  101.         for idx in range(1, len(myList)):
  102.             itemList.append(myList[idx])
  103.  
  104.         for idx in range(2, len(boxLine) - 2):
  105.             if boxLine[idx] != "," and boxLine[idx] != "'":
  106.                 for char in boxLine[idx]:
  107.                     boxString = boxString + char
  108.             else:
  109.                 idx += 1
  110.         boxList = boxString.split()
  111.  
  112.     return makeComplexDict(boxList), makeSimpleDict(itemList)
  113.  
  114.  
  115. def main():
  116.     fileCont = fileReader("text.txt")
  117.  
  118.  
  119. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement