In: Computer Science
def box_sort(names, sizes): Given a list of strings names, a corresponding list of ints sizes, we want to sort items by size so that each of our four sublists contains items in the smallest possible boxes of the following exact sizes: 2, 5, 25, and 50 units. Anything larger than 50 won't fit in a box and is simply ignored at this time. Create and return a list of the four sublists of items.
o Assume: names is a list of strings, and sizes is a list of
ints.
o Restrictions: remember, you can't call any built-in sorting
functions. It's not hard-coding to
directly calculate based on the four given sizes, but keeping a
list of box sizes may actually simplify your code.
box_sort(['a','b','c','d'], [1,5,6,10]) →
[['a'],['b'],['c','d'],[]] box_sort(['a','b','c'], [49,50,51]) →
[[],[],[],['a','b']]
def packing_list(names, sizes, box_size): Given a list of names, a corresponding list of int sizes, and an int box_size, this time we want to keep packing items into boxes of the given box_size until it's full, and then start filling another box of the same size. We return a list of filled boxes as a list of lists of strings. Oversized items are immediately placed into our output in a list by themselves, with an asterisk prefix to indicate that it does not fit. (We continue filling the current box after an oversized item). Items are only considered in their given ordering; do not reorder the items to seek a better packing list! Create and return this list of lists, each one containing items in one box or the single oversized item.
o Assume: names is a list of strings, sizes is a list of ints, and box_size is an int. Order is preserved for all non-oversized items, and oversized items show up immediately before the box that was being filled at the time of consideration.
# boxes of 2+1, 4, and 2.
packing_list(['a','b','c','d'], [2,1,4,2],5) → [['a','b'],['c'],['d']]
# while packing our second box, we find oversized item b, then finish our box.
packing_list(['x','a','b','c'], [5,2,10,3], 5) → [['x'],['*b'],['a','c']]
def box_sort(names, sizes): result = [[], [], [], []] for (i, n) in enumerate(names): # Add item to the list depending on its size.. # Like items with size equal to 2 or less, go in forst list # and so on.. if sizes[i] <= 2: result[0].append(n) elif sizes[i] <= 5: result[1].append(n) elif sizes[i] <= 25: result[2].append(n) elif sizes[i] <= 50: result[3].append(n) return result def packing_list(names, sizes, box_size): result = [] # A list which maintains the items going in current box currentList = [] # Total weight of items going in current box. currentWt = 0 for (i, n) in enumerate(names): # If the current item is more than box size, then it will form # its own list, with asterisk if sizes[i] > box_size: result.append(['*' + n]) else: # If adding item to current list leaves room for more # item, then Do not add to result. if currentWt + sizes[i] < box_size: currentWt += sizes[i] currentList.append(n) # If on adding an item, we reached to capacity of box, then all # items from current list move to result, and current list becomes empty. elif currentWt + sizes[i] == box_size: currentList.append(n) result.append(currentList) currentWt = 0 currentList = [] # if on adding new item, then size will exceed the permissible size on a # single box, hence, add all the items till now to result, and put new item # in next list. else: result.append(currentList) currentWt = sizes[i] currentList = [n] # If there are some items, which are not added to result, if len(currentList) != 0: result.append(currentList) return result print(packing_list(['a','b','c','d'], [2,1,4,2],5)) # while packing our second box, we find oversized item b, then finish our box. print(packing_list(['x','a','b','c'], [5, 2,10,3], 5))
**************************************************Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
pan1115_+IOL (Falco) 312CO) VUA_ILC). result = [] currentlist = [] currentwt = 0 Python 3.7.4 (default, Jul 9 2019, [GCC 6.3.0 20170516] on linux [['a', 'b'], ['c'], ['d']] [['x'], ['*b'], ['a', 'c']] for (i, n) in enumerate(names): if sizes[i] > box_size: result.append(['*' + n]) else: if currentWt + sizes[i] < box_size: currentwt += sizes[i] currentList.append(n) elif currentwt + sizes[i] == box_size: currentList.append(n) result.append(currentList) currentwt = 0 currentlist = [] else: result.append(currentList) currentWt = sizes[i] currentlist = [n] if len(currentList) != 0: result.append(currentlist) return result print(packing_list(['a', 'b', 'c','d'], [2,1,4,2],5)) ALCULLC