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

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 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 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'
python Write a program that prints your name 100 times to the screen. Write a function...
python Write a program that prints your name 100 times to the screen. Write a function that takes a string s and an integer n as parameters and prints the string s a total of n times (once per line). Write a for loop that prints all the integers from 3141 to 5926, skipping every other one. Write a for loop that prints every 5th integer from 5926 down to 3141. Write a program (using a for loop) to print...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values...
Python pls Create a function dict_sum. This function takes a dictionary and sums up the values in the dictionary. For example: dict1 = {1: {'una': 5, 'dos': 7, 'tres': 9, 'quar' : 11}, 2: {'dos':2, 'quar':4}, 3:{'una': 3, 'tres': 5}, 4:{'cin': 6}, 5:{'tres': 7 , 'cin': 8}} dict2 = {300:{'s': 300}, 400:{'s': 100, 'g': 100, 'p': 100}, 500: {'s': 50 ,'m': 400, 'p':30, 'i': 50}, 600: {'s': 40, 'i': 400}, 700: {'m': 100, 'p': 50}} def dict_sum(db): should give output...
1.Suppose a dictionary Groceries maps items to quantities.     Write a python to print the names...
1.Suppose a dictionary Groceries maps items to quantities.     Write a python to print the names of the grocery items whose quantity exceeds 400.     For example, if groceries = {‘cake mixes’: 430, ‘cheese’:312, ‘Orange’:525, ‘candy bars’: 217}         The output will be                                 Cake mixes                                 Orange 2. which Python string method would be the best choice to print the number of time “the” occurs in a sentence entered by the user? Provide a one word answer. Just...
Using Python Question 1 Write an input function. Then use it to supply the input to...
Using Python Question 1 Write an input function. Then use it to supply the input to following additional functions: i) Print multiplication table of the number from 1 to 12. ii) Print the sum of all the numbers from 1 to up the number given. iii) Print if the number supplied is odd or even. Question 2 Write function that asks for input from a user. If the user types 'end', the program exits, otherwise it just keeps going.
Python create a function tryhard and print out the dictionary as a string form. For example...
Python create a function tryhard and print out the dictionary as a string form. For example def tryhard(d:dict): #Code here input: d = {'first': {}, 'second': {'1': {'move'}, '0': {'move', 'slow'}}, 'third': {'1': {'stop'}}} output = " first movie: [ ]\n third movie: [('1', ['stop'])]\n second movie: [('0', ['slow', 'move']), ('1', ['move'])]\n"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT