Question

In: Computer Science

Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary...

Write a function that accepts a dictionary and produces a sorted list of tuples

The dictionary looks like this:

{‘US’: [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)}, {'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)}, {'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)}, {'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)}, {'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)}, {'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}], 'Australia' : [{'New South Wales': ('2/1/2020 18:12', 4, 0, 2)}, {'Victoria': ('2/1/2020 18:12', 4, 0, 0)}, {'Queensland': ('2/4/2020 16:53', 3, 0, 0)}, {'South Australia': ('2/2/2020 22:33', 2, 0, 0)}]

For these counts, I need to use the numbers that are bolded above). The returned sorted list (in descending order) will contain key-value pairs such that each key is a country and the corresponding value is the number of cases observed within that country.

For example: [('Australia', 13),(‘US’: 11)]

Solutions

Expert Solution

All the explanation is in the code comments. Hope this helps!

Code:

# required function
def func(dictionary):
  
# initial dictionary
res = {}
  
# loop for all keys in dictionary
for key in dictionary.keys():
  
# store the sum of cases
sum_cases = 0
  
# loop for all elements in list for key
for e in dictionary[key]:
# get the key and the tuple associated
for k in e.keys():
# get the second value (index = 1) of tuple
sum_cases = sum_cases + e[k][1]
  
# add the key and sum to dictionary
res[key] = sum_cases
  
# now sort the res to form a list of tuple
res = sorted(res.items(), key=lambda item: item[1], reverse=True)
# return result
return res

# sample run
x = {'US': [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)},
{'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)},
{'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)},
{'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)},
{'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)},
{'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)},
{'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)},
{'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}],
'Australia' : [{'New South Wales': ('2/1/2020 18:12', 4, 0, 2)},
{'Victoria': ('2/1/2020 18:12', 4, 0, 0)},
{'Queensland': ('2/4/2020 16:53', 3, 0, 0)},
{'South Australia': ('2/2/2020 22:33', 2, 0, 0)}]}


# call the function
print('Result:',func(x))

Sample run:

Code screenshots:


Related Solutions

Write a function average_steps(step_records) that takes a list of records, ie, tuples in the form (date_str,...
Write a function average_steps(step_records) that takes a list of records, ie, tuples in the form (date_str, step_count), and returns the average number of steps made across all the records as an unrounded floating point number. If step_records is an empty list then None should be returned. Note: You should include and use your total_steps function. Test Result step_records = [('2010-01-01',1), ('2010-01-02',2), ('2010-01-03',3)] avg = average_steps(step_records) print(avg) 2.0 step_records = [] avg = average_steps(step_records) print(avg) None step_records = [('2010-01-01',3), ('2010-01-02',3), ('2010-01-03',3),...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input,...
In python, write a function, called ThreeSum, that accepts a list of non-negative numbers as input, and returns the highest sum of three neighboring elements in it. Write a main method that initializes the following five lists, gets the ThreeSum result for all of them using the above function, and prints the result to the screen. Example of the output: List 1: [4,5,4,5] , Three sum = 14 List 2: [7] , Three sum = 7 List 3: [ ]...
C++ Data Structures 4. Write a client function that merges two instances of the Sorted List...
C++ Data Structures 4. Write a client function that merges two instances of the Sorted List ADT using the following specification. MergeLists(SortedType list1, SortedType list2, SortedType& result) Function: Merge two sorted lists into a third sorted list. Preconditions: list1 and list2 have been initialized and are sorted by key using function ComparedTo. list1 and list2 do not have any keys in common. Postconditions: result is a sorted list that contains all of the items from list1 and list2. c. Write...
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
The groups_per_user function receives a dictionary, which contains group names with the list of users.
The groups_per_user function receives a dictionary, which contains group names with the list of users. Users can belong to multiple groups. Fill in the blanks to return a dictionary with the users as keys and a list of their groups as values.def groups_per_user(group_dictionary):   user_groups = {}   # Go through group_dictionary   for ___:       # Now go through the users in the group       for ___:           # Now add the group to the the list of# groups for this...
Python Question: Write a function that checks to see if an array of integers is sorted...
Python Question: Write a function that checks to see if an array of integers is sorted in an increasing fashion, returning true if it is, false otherwise. Test it with at least4 arrays - 2 sorted and 2 not sorted. Use a CSV formatted input file as described in the previous question to run your program through some tests, where again the filename is passed as an argument. Heres what I have so far: import sys # command line arguement...
Write a function that accepts an int array and the array's size as arguments.
Write a function that accepts an int array and the array's size as arguments. The function should create a copy of the array, except that the element values should be reversed int the copy. The function should return a pointer to the new array. Demonstrate the function in a complete program.  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT