1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
natulia [17]
4 years ago
5

Write a Python function merge_dict that takes two dictionaries(d1, d2) as parameters, and returns a modified dictionary(d1) cons

tructed using the following rules. The rules for addition into the new dictionary are as follows: Add key/value pairs from d2 into d1. If a key from d2 already appears in d1: the new value in d1 is the sum of the values If a key appear
Computers and Technology
1 answer:
alexandr402 [8]4 years ago
5 0

Answer:

  1. def merge_dict(d1, d2):
  2.    for key in d2:
  3.        if key in d1:
  4.            d1[key] += d2[key]
  5.        else:
  6.            d1[key] = d2[key]
  7.    
  8.    return d1
  9. d1 = {
  10.    'a': 5,
  11.    'b': 8,
  12.    'c': 9  
  13. }
  14. d2 = {
  15.    'c': 5,
  16.    'd': 10,
  17.    'e': 12
  18. }
  19. modified_dict = merge_dict(d1, d2)
  20. print(modified_dict)

Explanation:

Firstly, create a function <em>merge_dict()</em> with two parameters,<em> d1, d2</em> as required by the question (Line 1)

Since our aim is to join the <em>d2</em> into<em> d1</em>, we should traverse the key of <em>d2</em>. To do so, use a for loop to traverse the <em>d2</em> key (Line 2).

While traversing the d2 key, we need to handle two possible cases, 1) d2 key appears in d1, 2) d2 key doesn't appears in d1. Therefore, we create if and else statement (Line 3 & 5).

To handle the first case, we add the values of d2 key into d1 (Line 4). For second case, we add the d2 key as the new key to d1 (Line 6).

Next, we can create two sample dictionaries (Line 11 - 20) and invoke the function to examine the output. We shall get the output as follows:

{'a': 5, 'b': 8, 'c': 14, 'd': 10, 'e': 12}

You might be interested in
This term is also called live, or hot, which means that a voltage is present and it can cause an electrical current. Any time an
tangare [24]
B. Energised although this is not as common
3 0
4 years ago
Which of the following reflects Moore's law? Multiple Choice The doubling of computer chip performance per dollar every two year
Katena32 [7]

Answer:

The correct answer is A.

Explanation:

Moore's Law states/predicts that the maximum number of transistors that can be used on integrated circuits (processors etc.) will be doubled every two years so it basically predicts an exponential growth in terms of transistors used.

Doubling the number of transistors used means that it will almost certainly increase the performance of the processors. Of course there are a lot of other variables that the performance depends on but Moore's Law is proven to be true and  is being used since 1960's. The answer given in option A is the one that best reflects on this law.

I hope this answer helps.

4 0
3 years ago
Software, also called a(n) ____________________, consists of a series of related instructions, organized for a common purpose, t
Naddika [18.5K]
Software is also called a program or an application.
8 0
3 years ago
An example of software is a
Veseljchak [2.6K]

Answer: false

Explanation:

5 0
3 years ago
The most distinctive feature of Google+ is
klio [65]
The answer is B, circles
8 0
4 years ago
Other questions:
  • Name that red flag asigment
    15·2 answers
  • State 3 file formats explain them in an understanding manner apart from Microsoft word and PDF
    5·1 answer
  • How often does colleges update the cost of attendance on their website?.
    13·1 answer
  • Technician A says that high pressure in recycled refrigerant is only caused by air contamination. Technician B says that recycle
    15·1 answer
  • Question 2 (1 point)
    8·1 answer
  • How many passes will it take to find the four in this list? 4, 5, 6, 7, 8, 9, 10 1 2 3 4
    12·2 answers
  • Outline four types of cyber law.
    14·1 answer
  • 5. Why do we need programming language?​
    13·2 answers
  • Which of the following is an example of an incremented sequence?
    13·1 answer
  • Web résumés allow you to include extra graphics and images that you would not include in a traditional résumé. please select the
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!