Question

Write a python function that takes a string as input, and returns a dictionary of frequencies...

Write a python function that takes a string as input, and returns a dictionary of frequencies of all the characters in the string. For example:

freq("dabcabcdbbcd") would return {"d":3, "a":2, "b":4, "c":3}. Then write a driver to demonstrate your function.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

OUTPUT :

CODE :

def f(s):
    #Ans to return
    mp = {}
    #Iterate through all chars in string
    for c in s:
        #Add it to dictionary
        if c not in mp:
            mp[c] = 0
        #Increment the count
        mp[c] += 1
    return mp

Add a comment
Know the answer?
Add Answer to:
Write a python function that takes a string as input, and returns a dictionary of frequencies...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • on python Design a function that can generate a histogram of characters in a string with...

    on python Design a function that can generate a histogram of characters in a string with the function prototype: histogram(string) It takes a string as the input parameter and returns a Python dictionary. Then design a function with the following prototype. print_hist_asc(histogram) It takes a histogram generated from your histogram function and print the histogram according to the frequency in the ascending order. Example input: “aaaaabbbbcccdde” Example output: e 1 d 2 c 3 b 4 a 5

  • Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this...

    Python 3 Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each key is a student, represented by a string. The value of each key is a list of courses, each represented by a string, in which the student is enrolled. The function inverse should compute and return a dictionary in which each key is a course and the associated value is a list of students enrolled in that course. For example, the following...

  • PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number)...

    PYTHON Programming short Questions: 1. Write a function takes as input name (string) and age (number) and prints: My name is <name> and I am <age> years old(where name and age are the inputs) 2. Write a function that takes as input a and b and returns the result of: ??ab 3. Write a function that takes as input a and b and returns the result of: ??ab if it is valid to divide a and b 4. Write a...

  • Write a Python function called more() that takes three string inputs and outputs a string Formally,...

    Write a Python function called more() that takes three string inputs and outputs a string Formally, the function signature and output are given by rucharist, char: str words str) > str Use the same names for the input arguments as shown above Note that charl and char2 will always be a string of length 1 (ie, it is a single character. The function checks which of charl or char2 appears more often in the word string and returns that character...

  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

  • project-8a Write a function named count_letters that takes as a parameter a string and returns a...

    project-8a Write a function named count_letters that takes as a parameter a string and returns a dictionary that tabulates how many of each letter is in that string. The string can contain characters other than letters, but only the letters should be counted. The string could even be the empty string. Lower-case and upper-case versions of a letter should be part of the same count. The keys of the dictionary should be the upper-case letters. If a letter does not...

  • in python Write a function that takes a string as an argument returns a new string...

    in python Write a function that takes a string as an argument returns a new string that is that string repeated 3 times.

  • python 1. Write a function that takes in a string and returns the string sorted For...

    python 1. Write a function that takes in a string and returns the string sorted For example, "cat" becomes "act", "dog" becomes "dgo" Hint: You might need to use the functions join and sorted

  • Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary....

    Python 3 Write a function named starCounter that takes three parameters: 1. a dictionary named starDictionary. Each key in starDictionary is the name of a star (a string). Its value is a dictionary with two keys: the string 'distance' and the string 'type'. The value associated with key 'distance' is the distance from our solar system in light years. The value associated with key 'type' is a classification such as 'supergiant' or 'dwarf'. 2. a number, maxDistance 3. a string,...

  • Write a function called double_str that takes a string argument and returns a new string that...

    Write a function called double_str that takes a string argument and returns a new string that has all of the same characters as the argument, except each one is repeated twice. For example: double_str('dog') --> 'ddoogg' looking for python coding below is what I have so far double_str = 'robert' double_str 'robert' for i in range(len(double_str)): print(double_str[i])

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT