Question

In: Computer Science

Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...

Use Python for this quetions:

Write a python functions that use Dictionary to:

1) function name it addToDictionary(s,r)

that take a string and add it to a dictionary if the string exist increment its frequenc

2) function named freq(s,r)

that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.

Solutions

Expert Solution

Explanation:-

Dictionary in python works like map where we have two things key:value pair where we can find value corresponding to each key .In addToDictionary() funciton what we did is we checked if that key is present in the dictionary or not if it is present then we just incremented its value and if it is not present then we pushed the new string into the dictionary . In freq() we just returned the value corresponding to that key in the record and if it is not present we just returned 0.

CODE:-

record = {"a":1,"b":2,"c":3}
def addToDictionary(s,r):
   if r.get(s) == None:
       r[s]=1
   else:
       r[s]=r[s]+1

def freq(s,r):
   if r.get(s)==None:
       return 0
   return r[s]
  
print("-----record before any operation---")
print(record)
addToDictionary("d",record)

print("-------a key is added--------")

print(record)

print("-----use freq function to find freq of alphabet \"a\"-------")
print(freq("a",record))

OUTPUT:-


Related Solutions

python 3.6: You will have two functions: Function 1: update(user_dictionary): This function will take a dictionary...
python 3.6: You will have two functions: Function 1: update(user_dictionary): This function will take a dictionary entry as user that has keys, ‘username’,’password’,’uid’, and ‘gid’ along with values. You will search if the username is already in your user_list that you get from your userfile.json file that you have created in your previous task. If the username in your user_dictionary is not in the list that is in your .json file then add the user_dictionary into the existing list and...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that...
Create two functions that you can use for a dictionary manager: 1. remove_item(dictionary,key): a function that removes the item with the supplied key from the dictionary, if it exits. If the input key doesn’t exist in the dictionary, print out a message saying that the new item has not been removed because there is no matching key in the dictionary. Your function should not produce a Python error if the item does not exist; 2. add_new_item(dictionary,key,value): a function that adds...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function always returns some value c)different function cannot use the same variable names d) function must use the same parameter names as the corresponding variables in the caller what benefits does structuring a program through defining functions bring? a) there is a possibility of reducing the number of variables and/or objects that must be managed at any cost at any one point b)the program is...
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”.
(Python) I want to use a function called level() that takes a dictionary. Here is a...
(Python) I want to use a function called level() that takes a dictionary. Here is a dictionary with people's job and skill level. dict1 = {'Jame': {'Cleaning': 5, 'Tutoring': 2, 'Baking': 1},Pam': {'Plumbing': 2, 'Cleaning': 5}) like if I called level(dict1), the output will return {'Pam', 'James'} It finds the people's average skill level like for Pam is (2+5)/2=3.5 and sorted descending. How do I do that and how do I do it in only one return statement(using comprehension or...
IN PYTHON Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low...
IN PYTHON Write a function called check_inventory(inventory, low) that passes a dictionary as inventory and low as an integer, and returns a sorted list of items that are below an inventory level that is given by thelowinteger parameter. The function should work without the low parameter supplied - in which case, you should assume low is 5. You do not have to worry about reading and writing to a file, that code is provided, and you don’t have to change...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi *...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi * parameters: none * returns: N/A * operation: just say "hi" when called. * expected output: >>> say_hi() hi * function name: personal_hi * parameters: name (string) * returns: N/A * operation: Similar to say_hi, but you should include the name argument in the greeting. * expected output: >>> personal_hi("Samantha") Hi, Samantha * function name: introduce * parameters: name1 (string) name2 (string) * returns: N/A...
Python 3 Functions that give answers Define and complete the functions described below. * function name:...
Python 3 Functions that give answers Define and complete the functions described below. * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. * expected output: JUST RETURNS THE NAME...TO VIEW IT YOU CAN PRINT IT AS BELOW >>> print(get_name()) John * function name: get_full_name * parameters: fname (string) lname (string) first_last (boolean) * returns: string * operation: Return (again, NOT print) the full name based on the first...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary...
Write a function that accepts a dictionary and produces a sorted list of tuples The dictionary looks like this: {‘US’: [{'Chicago, IL': ('2/1/2020 19:43', 2, 0, 0)}, {'San Benito, CA': ('2/3/2020 3:53', 2, 0, 0)}, {'Santa Clara, CA': ('2/3/2020 0:43', 2, 0, 0)}, {'Boston, MA': ('2/1/2020 19:43', 1, 0, 0)}, {'Los Angeles, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Orange, CA': ('2/1/2020 19:53', 1, 0, 0)}, {'Seattle, WA': ('2/1/2020 19:43', 1, 0, 0)}, {'Tempe, AZ': ('2/1/2020 19:43', 1, 0, 0)}], 'Australia'...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT