Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def keywordSearchDict(keyword, myDict):
- """
- Find the value(s) of the keyword
- :param keyword: Given key to search for
- :param myDict: The disctionary to search in
- :return: The value(s) of the keyword
- """
- userKey = keyword # Set the keyword to a string
- if userKey in myDict: # If the keyword is in the dict
- for key in myDict.keys(): # Iterate all over keys in myDict
- if userKey == key: # If the userKey matches the dictKey
- return myDict.get(key) # Return the values of the key
- else:
- return None # Or don't
- def sortLargest2Small(itemList):
- """
- Sort the given list from largest to smallest
- :param itemList: Given list
- :return: The list sorted largest to smallest
- """
- sortedList = sorted(itemList, reverse=True)
- return sortedList
- def makeSimpleDict(aList):
- """
- Make a dict for item:values
- :param aList: list of item, value pairs
- :return: item dictionary
- """
- myDict = {}
- keyList = []
- valueList = []
- for pair in aList:
- keyList.append(pair[0])
- valueList.append(int(pair[1]))
- for idx in range(len(keyList)): # For every index in the wordlist
- myDict[keyList[idx]] = [valueList[idx]] # Add that word sorted with a key of that word
- return myDict
- def makeComplexDict(keyList):
- """
- Make a dict for box:weight
- :param keyList: The weights n boxes
- :return: Dictionary of boxes and max weight
- """
- myDict = {}
- count = 0
- for idx in range(len(keyList)):
- boxName = "Box" + str(count)
- count += 1
- myDict[boxName] = [int(keyList[idx])]
- myDict[boxName].append(0)
- return myDict
- def roomiest(boxList, itemList, boxDict, itemDict):
- """
- For each item find the box with the greatest remaining allowed weight that can support the item and place the item in that box
- :param boxList: The sorted list of boxes( large to small )
- :param itemList: The sorted list of items ( large to small )
- :param boxDict: Dict w/ boxes
- :param itemDict: Dict w/ items
- :return: If boxes were able to fit all items(1); items in box with individual weights(2); Box name with max
- weight(3); items with their weights that were left behind(4)
- """
- sortedItems = sortLargest2Small(itemList)
- sortedBoxes = sortLargest2Small(boxList)
- for item in sortedItems:
- for box in sortedBoxes:
- itemWeight = keywordSearchDict(item, itemDict)
- boxRemWeight = keywordSearchDict(box, boxDict)
- if itemWeight <= boxRemWeight:
- itemDict[
- box] = # Need to add item to box with its weight as well as modify the remaining weight the box
- # can hold
- def fileReader(filename):
- """
- Take the given txt file and format into proper list for boxes and items
- :param filename: The filename of the text file
- :return: Send lists to make a dictionary from them
- """
- with open(filename, 'r') as myFile: # Open the correct file
- itemList = []
- boxString = ""
- myList = [line.split() for line in myFile.readlines()]
- boxLine = str(myList[0:1])
- for idx in range(1, len(myList)):
- itemList.append(myList[idx])
- for idx in range(2, len(boxLine) - 2):
- if boxLine[idx] != "," and boxLine[idx] != "'":
- for char in boxLine[idx]:
- boxString = boxString + char
- else:
- idx += 1
- boxList = boxString.split()
- return makeComplexDict(boxList), makeSimpleDict(itemList)
- def main():
- fileCont = fileReader("text.txt")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement