Question

Version:0.9 StartHTML:0000000105 EndHTML:0000008229 StartFragment:0000000141 EndFragment:0000008189 In Python, a tuple is a collection similar to a list...

Version:0.9 StartHTML:0000000105 EndHTML:0000008229 StartFragment:0000000141 EndFragment:0000008189

In Python, a tuple is a collection similar to a list in that it is an ordered collection of items. An important difference,

however, is that a tuple is immutable – once created, a tuple cannot be changed. Also, tuples are denoted using

parentheses instead of square brackets. As with lists, elements of a tuple are accessed using indexing notation.

For example, given the tuple subjects = (’physics’, ’chemistry’, ’biology’), we could ac

cess the elements of the tuple using subjects[0], subjects[1] and subjects[2].

Write a function grocery shopping() that takes a list of tuples called items_purchased that represent

CSE 101 – Spring 2020 Homework #2 4items purchased at a grocery store. Each tuple contains three items, in this order:

the name of the item to purchase

the unit cost of the item in dollars

the number of units desired

The example tuple (’Juice’, 3, 5) indicates that we bought 3 units of “Juice” at $5 apiece, for a total of

$15.

The purpose of the function is to determine and return the name of the item that cost the most to buy.

You may assume that each named item occurs once in the list. For example, we would not fifind two tuples that

both have “Juice” as the item to purchase.

Examples:

Function Argument Return Value

[(’Bread’, 3, 3), (’Eggs’, 2, 3)] ’Bread’

[(’Milk’, 5, 2), (’Butter’, 4, 1), (’Juice’, 1, 5),

(’Eggs’, 4, 3)]

’Eggs’

[(’Milk’, 2, 2), (’Butter’, 4, 1), (’Juice’, 3, 5),

(’Eggs’, 4, 3)]

’Juice’

[(’Peas’, 5, 2), (’Meat’, 3, 13), (’Beer’, 4, 12),

(’Ice cream’, 3, 5), (’Eggs’, 3, 3)]

’Beer’

[(’Peas’, 5, 2)] ’Peas’

Note: The quotation marks displayed in the example return values are there to emphasize that the return values

are strings. Do not add quotation marks to the return values.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


ANSWER:-

CODE:-

def grocery_shopping(lst):
    maximumCost = 0
    maxItem = ""
    # compare each item cost, if cost of any item is greater than previous max,
    # then replace that item and cost with current item
    for item in lst:
        cost = item[1]*item[2]
        if(maximumCost < cost):
            maximumCost = cost
            maxItem = item[0]
    return maxItem

print(grocery_shopping([("Bread", 3, 3), ("Eggs", 2, 3)]))
print(grocery_shopping([("Milk", 5, 2), ("Butter", 4, 1), ("Juice", 1, 5),("Eggs", 4, 3)]))
print(grocery_shopping([("Milk", 2, 2), ("Butter", 4, 1), ("Juice", 3, 5),("Eggs", 4, 3)]))
print(grocery_shopping([("Peas", 5, 2), ("Meat", 3, 13), ("Beer", 4, 12),("Ice cream", 3, 5), ("Eggs", 3, 3)]))
print(grocery_shopping([("Peas", 5, 2)]))


NOTE:- If you need any modifications in the code, please comment below. Please give postive rating.THUMBS UP.

                      THANK YOU!!!!

OUTPUT:-

Add a comment
Know the answer?
Add Answer to:
Version:0.9 StartHTML:0000000105 EndHTML:0000008229 StartFragment:0000000141 EndFragment:0000008189 In Python, a tuple is a collection similar to a list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the FP growth algorithm generate frequent patterns within the following dataset (use min_sup = 3)....

    Using the FP growth algorithm generate frequent patterns within the following dataset (use min_sup = 3). TID      Itemsets 1          Sausage, Milk, Bread, Yogurt, Beer 2          Milk, Beer, Juice 3          Milk, Bread, Juice 4          Soda, Sausage, Yogurt, Bread 5          Sausage, Milk, Bread, Fruit, Beer 6          Vegetable, Yogurt, Butter, Fruit, Milk 7          Sausage, Vegetable, Butter, Juice 8          Milk, Yogurt, Bread

  • Define a function called collapse() which takes a list as input. Each element of the list...

    Define a function called collapse() which takes a list as input. Each element of the list will either be an integer, or a list of integers. The function should modify the input list by replacing all of the elements which themselves are lists with the sum of their elements. For example: Test Result vals = [1, 2, 3, 4, 5] collapse(vals) print(vals) [1, 2, 3, 4, 5] vals = [1, [2, 3], 4, 5] collapse(vals) print(vals) [1, 5, 4, 5]...

  • write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple

    1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...

  • Python Function Name: networks Parameters: int, list of tuples of int Returns: list of sets of...

    Python Function Name: networks Parameters: int, list of tuples of int Returns: list of sets of int Description: In your class, many students are friends. Let’s assume that two students sharing a friend must be friends themselves; in other words, if students 0 and 1 are friends and students 1 and 2 are friends, then students 0 and 2 must be friends. Using this rule, we can partition the students into circles of friends. To do this, implement a function...

  • Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write...

    Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...

  • 1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical...

    1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical order. Dictionaries cannot be sorted, so we need to use the items method to convert the dictionary into a list of tuples. Then we can sort that new list of tuples, then display the ingredients. Use items and list to convert ingredients into a list of tuples. Sort the new list, called ingred_list Write a for loop which goes through each tuple in ingred_list....

  • PYTHON FILL IN THE BLANK CODE REMEMBER TO UTILIZE DICTIONARIES # Implement this function: def store_checkout(inventory_tuple_list,...

    PYTHON FILL IN THE BLANK CODE REMEMBER TO UTILIZE DICTIONARIES # Implement this function: def store_checkout(inventory_tuple_list, item_purchase_list): """ inventory_tuple_list: A list of tuples. Each tuple has a string representing an item name, an int representing a price, and a string representing a description. Example: [("A", 5, "shiny new A"), ("B", 10, "big heavy B")] item_purchase_list: A list of strings. Each string represents an item name. Example: ["A", "A", "B", "C"] Return the total price of the items in item_purchase_list by...

  • in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all...

    in python A. Define a function called contains_only_integers() that takes a tuple, returns True if all the items in the tuple are integers(2), and returns False otherwise. For example, contains_only_integers (3, 5, 17, 257, 65537) ), contains_only_integers( (-1,) ), and contains_only_integers( ) should all return True, but contains_only_integers (2.0,4.0)) and contains_only_integers (8, 4, "2", 1)) should both return false. Your function should use a while loop to do this calculation. 121 Hint: the is instance() built-in function provides the most...

  • Define the get_list_of_lists() function which is passed a list of strings as a parameter. An example...

    Define the get_list_of_lists() function which is passed a list of strings as a parameter. An example parameter list is: ["4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 12 3 12 4 3", "4 3 1 12 2 6"] The function returns a list of lists of tuples. For each element of the parameter list, e.g., "4 3 1 12 2 12" the function creates a list of tuples, e.g., [("4", 3), ("1", 12),...

  • in python 3 Create a list or tuple with 5 different integers. Prompt the user for...

    in python 3 Create a list or tuple with 5 different integers. Prompt the user for an integer. If the number is in the list (or tuple) print a message stating the number was found. Otherwise, print a message stating the number was not found. Assume the list includes the numbers 0, 2, 4, 6, 8, and 10.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT