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 using | |
| prices from the provided inventory_tuple_list. If an item does not | |
| have a price, it is free. The descriptions are extra, useless | |
| information for this function. | |
| The example inputs here would have a total cost of: | |
| 5 + 5 + 10 + 0 = 20 | |
| """ | |
| return 0 | |
| # Implement this function: | |
| def highest_frequency_count(item_list): | |
| """ | |
| item_list: | |
| A list of strings. Each string represents an item name. | |
| Example: ["A", "A", "B", "C", "A", "B", "B"] | |
| Find and return a count for the most frequently occurring item | |
| in item_list. | |
| In the example, "A" and "B" each appear 3 times, while "C" appears | |
| only 1 time. Therefore, expected return value would be 3. | |
| """ | |
| return 0 |
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
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 using
prices from the provided inventory_tuple_list.
If an item does not
have a price, it is free. The descriptions are
extra, useless
information for this function.
The example inputs here would have a total cost
of:
5 + 5 + 10 + 0 = 20
"""
#initializing cost to 0
cost=0
#looping through each item in
item_purchase_list
for i in
item_purchase_list:
#looping through
each tuple in inventory_tuple_list
for record in
inventory_tuple_list:
#validating tuple's length and checking if first element equals
i
if len(record)==3 and
record[0]==i:
#adding price (second element of tuple) to cost
cost+=record[1]
return cost
def highest_frequency_count(item_list):
"""
item_list:
A list of strings. Each string represents an
item name.
Example: ["A", "A", "B", "C", "A", "B",
"B"]
Find and return a count for the most frequently
occurring item
in item_list.
In the example, "A" and "B" each appear 3 times,
while "C" appears
only 1 time. Therefore, expected return value
would be 3.
"""
#creating a dictionary
counts=dict()
#looping and finding the counts of each item
in item_list
for i in
item_list:
#if i is already
there in counts dict, incrementing count by 1
if
i in counts:
counts[i]+=1
else:
#else, adding to dict with i being key and 1 being value
counts[i]=1
#sorting the counts dict by value (kv is
individual item, kv[1] returns the count value)
#in descending order
counts_sorted=sorted(counts.items(),key=lambda
kv:kv[1], reverse=True)
#if length is 0, returning None (item_list
is empty)
if
len(counts_sorted)==0:
return
None
# counts_sorted will be a list of
tuples where each tuple is in the
# form ('A', 2) where 'A' is the key and 2 is
the count
# returning the count value of first tuple in
counts_sorted
return
counts_sorted[0][1]
PYTHON FILL IN THE BLANK CODE REMEMBER TO UTILIZE DICTIONARIES # Implement this function: def store_checkout(inventory_tuple_list,...
Language: Python Topic: Tuples Function name : todo_tuple Parameters : todo (list of tuples of strings), completed (list of strings) Returns: final_list (list) Description : Write a function that takes in a list of tuples of strings that represents the work you have to do in each class, and a list of strings that represent the work you have already completed. Each tuple in the todo list represents the work for a single class. For this function, go through the...
Language: Python Topic: Dictionaries Function name: catch_flight Parameters: dictionary, tuple containing two strings Returns: dictionary Description: You’ve been stuck in NYC for around 8 months all by yourself because you haven’t been able to find a good time to fly home. You’re willing to go to any city but want to see how many flights to each location fit your budget. You’re given a dictionary that has city names (strings) as the keys and a list of prices (list) and...
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...
Python 5. Write a function named grade_components that has one parameter, a dictionary. The keys for the dictionary are strings and the values are lists of numbers. The function should create a new dictionary where the keys are the same strings as the original dictionary and the values are tuples. The first entry in each tuple should be the weighted average of the non-negative values in the list (where the weight was the number in the 0 position of the...
Im supposed to create a function arg1 arg2 def compare_back(('c', 'u', 'p', 'b', 'o', 'a', 'r', 'd'),('b', 'u', 'c', 'k', 'b', 'o', 'a', 'r', 'd')): we can compare either 2 strings, 2 tuples or 2 lists and the function is supposed to return the number of characters that are in both parameters but we stop the counter when we get to a specific character and do the comparison and the other tuple isnt the same so like for this example...
Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...
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...
Help needed related python task ! Thanx again How you're doing it • Write a function write_to_file() that accepts a tuple to be added to the end of a file o Open the file for appending (name your file 'student_info.txt') o Write the tuple on one line (include any newline characters necessary) o Close the file • Write a function get_student_info() that o Accepts an argument for a student name o Prompts the user to input as many test scores...
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...
Phython read_file (fp) D1,D2,D3: a) This function read a file pointer and returns 3 dictionaries: - Parameters: file pointer (fp) - Returns : 3 dictionaries of list of tuples - The function displays nothing b) You must use the csv.reader because some fields have commas in them. c) Each row of the file contains the name of a video game, the platform which it was released, the release year, the genre (i.e. shooter, puzzle, rpg, fighter, etc.), the publishing...