Question

In: Computer Science

def box_sort(names, sizes): Given a list of strings names, a corresponding list of ints sizes, we...

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']]

Solutions

Expert Solution

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


Related Solutions

r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...
r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for string in strings: st = "f'" + string + ": {" + string + "}'" print_stmt = 'print(' + st + ', end="; ")' # Uncomment the following print statement to see the statement to be executed. # Each will appear on a separate line. # print(f'\n{print_stmt}') exec(print_stmt) print() print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])']) OUTPUT: list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37];...
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's...
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's temperatures from temps_now are warmer than past temperatures in temps_then. The resulting list should contain "Warmer" at the indexes where this year's temperature is warmer, and "Not Warmer" at the indexes where the past year was warmer, or there is a tie. Precondition: len(temps_then) == len(temps_now) >>> warmer_year([10], [11]) ['Warmer'] >>> warmer_year([26, 27, 27, 28], [25, 28, 27, 30]) ['Not Warmer', 'Warmer', 'Not Warmer',...
Write a program that uses Python List of strings to hold the five student names, a...
Write a program that uses Python List of strings to hold the five student names, a Python List of five characters to hold the five students’ letter grades, and a Python List of four floats to hold each student’s set of test scores. The program should allow the user to enter each student’s name and his or her four test scores. It should then calculate and display each student’s average test score and a letter grade based on the average....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g....
Creating a list/tuple 3.Given a list of tuples containing student first names and last names e.g. (“John”, “Doe”) that represents a class. Ask the user for a students first and last name and check if that student is a member of the class.
Question 1 A telemarketer is given a list of names to contact for a survey on...
Question 1 A telemarketer is given a list of names to contact for a survey on banking services.   He has 40 names per page and must try to complete 5 surveys per hour. How many ways are there to choose 5 names randomly? A parent is making goody bags for their child’s 7th birthday party. They want to place 2 of 24 chocolate bars, 1 of 12 potato chip bags, 3 of 24 lollipops, and 3 of 20 assorted wrapped...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
This function will be given a list of strings and a character. You must remove all...
This function will be given a list of strings and a character. You must remove all occurrences of the character from each string in the list. The function should return the list of strings with the character removed. Signature: public static ArrayList<String> removeChar(String pattern, ArrayList<String> list)
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." In C, Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program in C to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume...
Suppose you are given a file containing a list of names and phone numbers in the...
Suppose you are given a file containing a list of names and phone numbers in the form "First_Last_Phone." Write a program to extract the phone numbers and store them in the output file. Example input/output: Enter the file name: input_names.txt Output file name: phone_input_names.txt 1) Name your program phone_numbers.c 2) The output file name should be the same name but an added phone_ at the beginning. Assume the input file name is no more than 100 characters. Assume the length...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT