Answered You can hire a professional tutor to get the answer.

QUESTION

Write a function union that accepts two dictionaries (whose keys and values are both integers) as parameters, and returns a new dictionary that

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, 18: 5, 42: -9, 76: 10, 98: 6, 234: 50, 11: 9, 9999: 3}

The "union" of two dictionaries m1 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.

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question