In: Computer Science
####################################################################### ##################################### ###############python################# # RQ1 def merge(dict1, dict2): """Merges two Dictionaries. Returns a new dictionary that combines both. You may assume all keys are unique. >>> new = merge({1: 'one', 3:'three', 5:'five'}, {2: 'two', 4: 'four'}) >>> new == {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'} True """ "*** YOUR CODE HERE ***" # RQ2 def counter(message): """ Returns a dictionary where the keys are the words in the message, and each key is mapped (has associated value) equal to the number of times the word appears in the message. >>> x = counter('to be or not to be') >>> x['to'] 2 >>> x['be'] 2 >>> x['not'] 1 >>> y = counter('run forrest run') >>> y['run'] 2 >>> y['forrest'] 1 """ "*** YOUR CODE HERE ***"
def merge(dict1, dict2):
"""Merges two Dictionaries. Returns a new dictionary that combines
both. You may assume all keys are unique.
>>> new = merge({1: 'one', 3:'three', 5:'five'}, {2:
'two', 4: 'four'})
>>> new == {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5:
'five'}
True
"""
"*** YOUR CODE HERE ***"
new={}
for i in dict1:
new[i]=dict1[i]
for i in dict2:
new[i]=dict2[i]
return new
def counter(message):
""" Returns a dictionary where the keys are the words in the
message, and each
key is mapped (has associated value) equal
to the number of times the word appears in the message.
>>> x = counter('to be or not to be')
"""
words=message.split()
dict1={}
for i in words:
dict1[i]=dict1.get(i,0)+1
return dict1