Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function union that accepts two dictionaries (whose keys and values are both integers) as parameters, and returns a new dictionary that represents
Write a function union that accepts two dictionaries (whose keys and values are both integers) as parameters, and returns a new dictionary that represents a merged union of the two original dictionaries. For example, if two dictionaries m1 and m2 contain these pairs: {7-1, 18-5, 42-3, 76-10, 98=2, 234=50} m1 {7-2, 11-9, 42--12, 98-4, 234-0, 9999=3} m2 The call of union (m1, m2) should return a dictionary that contains the following pairs: {7-3, 11-9, 18-5, 42=-9, 76-10, 98=6, 234-50, 9999=3} The "union" of two dictionaries ml and m2 is a new dictionary that contains every key from m1 and every key from m2. Each value stored in your "union" map should be the sum of the corresponding value(s) for that key in m1 and m2, or if the key exists in only one of the two maps, that map's corresponding value should be used. For example, in the maps above, the key 98 exists in both maps, so the result contains the sum of its values from the two maps, 2 + 4 = 6. The key 9999 exists in only one of the two maps, so its sole value of 3 is stored as its value in the result map. Either dictionary passed in (or both) could be empty. Though the pairs are shown in sorted order by key above, you should not assume that the dictionaries passed to you store their keys in sorted order. You may create one collection of your choice as auxiliary storage to solve this problem. You can have as many simple variables as you like. You should not modify the contents of the dictionaries passed to your function.
Step by Step Solution
★★★★★
3.34 Rating (145 Votes )
There are 3 Steps involved in it
Step: 1
The detailed ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started