Question

In: Computer Science

####################################################################### ##################################### ###############python################# # RQ1 def merge(dict1, dict2): """Merges two Dictionaries. Returns a new dictionary that...

#######################################################################
#####################################
###############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 ***"

Solutions

Expert Solution

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
  


Related Solutions

Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the...
Write a Python function with prototype “def anagramdictionary(wordlist):” that will return an “anagram dictionary” of the given wordlist  An anagram dictionary has each word with the letters sorted alphabetically creating a “key”.
no dictionaries please Returns a new string which is lowercase version of the given word with...
no dictionaries please Returns a new string which is lowercase version of the given word with special characters and digits removed The returned word should not have any of the following characters: ! . ? : , ' " - _ \ ( ) [ ] { } % 0 1 2 3 4 5 6 7 8 9 tab character and new-line character >>> clean_word("co-operate.") 'cooperate' >>> clean_word("Anti-viral drug remdesivir has little to no effect on Covid patients' chances...
In Python: def _nodeAtIndex(self, index): """Returns the reference to the node at the given index; If...
In Python: def _nodeAtIndex(self, index): """Returns the reference to the node at the given index; If index is out of range, raise IndexError """ # PROBLEM 2 # You can assume that index is non-negative. # You need to traverse the list and stop at the required index. # YOUR CODE HERE (THIS IS WHAT I HAVE CURRENTLY WRITTEN BUT I"M STUCK AND DON"T KNOW WHAT TO DO) while current != None: # use a while-loop. plist.append(current._element) # process the...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
a python function that reads two text files and merges in to one Linked List, be...
a python function that reads two text files and merges in to one Linked List, be able to print each Item in the new single Linked List class Node(object): item = -1 next = None def __init__(self, item, next): self.item = item self.next = next ================================ textfile! 979 2744 5409 1364 4948 4994 5089 703 1994 4637 2228 4004 1088 2812 170 5179 2614 238 4523 4849 3592 3258 1951 3440 3977 1247 4076 1824 4759 4855 5430 347 974...
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted...
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted lists into a single sorted list. Now suppose there are k sorted lists and there are n elements in total in these k lists. Design an efficient algorithm that merges these k sorted lists into one sorted list. The running time of your algorithm should be a function of both n and k.
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top':...
Python pls create a function called search_position. This function returns a dictionary. team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2, 'Top': 5},'Shaco': {'Jungle': 4, 'Top': 2, 'Mid': 1}} def search_position(team1): should return {'Top': {'Fiora': 1, 'Yasuo':5,'Olaf':3,'Shaco':}, 'Jungle': {'Shaco': 4}, 'Mid': {'Yasuo', 2, 'Fiora': 4,'Olaf':2}, 'Bottom': {'Fiora': 3}, 'Support': {'Olaf': 4}} *******IMPORTANT*************** The result should be alphabetical order.
Python Coding: Working with Conditions and Dictionaries. 1. Create three dictionaries (student_1, student_2, student_3). 2. In...
Python Coding: Working with Conditions and Dictionaries. 1. Create three dictionaries (student_1, student_2, student_3). 2. In each student dictionary have a key-value for first_name, last_name, and an id number (6 digit), and a current_course. The values assigned to each of these keys is your choice. 3. Also each student will have in their dictionary a list for grades. You will need to add 3 grade values into the list. 4. Provide the student with a message about each assignment grade...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName,...
How would I setup this dictionary for Python 3? class Student(object): def __init__(self, id, firstName, lastName, courses = None): The “id”, “firstName” and “lastName” parameters are to be directly assigned to member variables (ie: self.id = id) The “courses” parameter is handled differently. If it is None, assign dict() to self.courses, otherwise assign courses directly to the member variable. Note: The “courses” dictionary contains key/value pairs where the key is a string that is the course number (like “course1”) and...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT