Write a function union that accepts two dictionaries (whose keys and values are both integers) as...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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. 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.
Expert Answer:
Related Book For
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp
Posted Date:
Students also viewed these programming questions
-
What keys and values are contained in the following map after this code executes? Map map = new HashMap (); map.put(8, "Eight"); map.put(41, "Forty-one"); map.put(8, "Ocho"); map.put(18, "Eighteen");...
-
Two masses m1 and m2 are connected by a string passing through a hollow pipe with m1 being swung around in a circle of radius R and m2 hanging vertically as shown in the figure. Obviously if m1 moves...
-
Two masses m1 = 18.0 kg and m2 = 26.5-kg, are connected by a rope that hangs over a pulley (as in Fig. 8047). The pulley is a uniform cylinder of radius .260m and mass 7.50 kg. Initially m1 is on the...
-
You are the VP of Human Resources for a company with 250 employees. You have both outside sales representatives and inside sales representatives that work primarily by phone. James is an outside...
-
Find Q for each material. 1. Steel, w = 3.00 lb, T = 500F, Q = _____ Btu 2. Copper, m = 155 kg, T = 170C, Q = _____ kcal 3. Water w = 19.0 lb, T = 200F, Q = _____ Btu 4. Water m = 250 g, T = 17.0C, Q...
-
Comment on the following observations. (a) In its complexes, Co(III) forms strong bonds to O- and N-donor ligands, moderately strong bonds to P-donor ligands, but only weak bonds to As-donor ligands....
-
Refer to E5-47B and E5-48B. Assume the Landon Dairy Forming Department has the following costs per equivalent unit (EU) on its own production cost report for the month of January: Cost per EU...
-
Rework in job costing, journal entry (continuation of 18-35). Assume that the 7 spoiled units of Whitefish Machine Shops Job #10 can be reworked for a total cost of $1,700. A total cost of $10,150...
-
Identify 2 tools used for backup purposes that would support business continuity to ensure that the data can be accessed during the disaster. Explain who is responsible for the backup. How often...
-
(Allocating Parking Spots) You are the manager of a luxury apartment building whose parking garage contains 300 parking spots. Residents may choose to purchase a dedicated parking spot for $60,000...
-
Assume the following hypothetical economy: C = 600 + 0.8YD I = 200 G = 400 T = 300 X = 100 M = 0.2Y where C is consumption, YD is disposable income, I is investment, G is government spending, T is...
-
Facing an important job interview, the college graduate searches her closet for the perfect outfit, then rifles through her medicine cabinet for just the right cognitive-en hancement pill. Adderall,...
-
Paul Dyer always was able to hold off his bosss invitations to party by employing that arms-length response: Well have to do that sometime, hed say. But when his boss, in his 30s, invited Dyer, 24,...
-
What can managers in white-collar firms learn from roughnecks and roustabouts on an offshore oil rig? That extinguishing macho behavior is vital to achieving top performance. Thats a key finding from...
-
Dirk Cuypers, the top official at Belgiums health ministry, is sick of sick leave. Belgians, like many Europeans, are entitled to extensive or even unlimited sick leaveand they tend to stretch the...
-
Im the purchasing manager for an engineering company, and Ive been told to lay off one employee. All my staffers are hard-working, and I dont feel morally right about laying off one of them. My boss...
-
A large wheel has a diameter of 20 cm and can travel a set distance in 135 rotations. A small wheel must rotate 180 times to travel the same distance. What is the diameter of the small wheel?
-
Medi-Exam Health Services, Inc. (MEHS), located in a major metropolitan area, provides annual physical screening examinations, including a routine physical, EKG, and blood and urine tests. MEUS's...
-
Write a program called Squares that uses the DrawingPanel to draw the shape shown in Figure 3G.28 . The DrawingPanel is 300 pixels wide by 200 pixels high. Its background is cyan. The horizontal and...
-
Write a method called fromCounts that converts an ArrayIntList of counts into a new ArrayIntList of values as follows. Assume that the list stores a sequence of integer pairs that each pair indicates...
-
Write a method called area that accepts as a parameter the radius of a circle and that returns the area of the circle. For example, the call area(2.0) should return 12.566370614359172. Recall that...
-
Climate change (a) is a subject most scientists disagree about. (b) refers to the effect of greenhouse gases on atmospheric temperature. (c) refers to temperature increases in all of Earths climate...
-
Why do earthquakes produce seismic waves?
-
What method uses records of past earthquakes to predict future earthquakes? What are the limitations of this method?
Study smarter with the SolutionInn App