Question

Question 2 - Objects You are to implement the following functions: getCharacterFrequency -10pts This function takes a single string (str) argument and returns an object. o The objects properties will be the unique letters present in str The value of each property will be the frequency of each character present in the string. NOTE: Uppercase and Lowercase letters should be grouped in the same count. For example, the word Babble would result in the following object B3, 11 This count includes upper and lovercase BS printcharacterFrequency 10pts This function will take a single object. The object should be of the type returned by getCharacterFrequency This function will display each character and its corresponding frequency Each character should be surrounded by quotation marks in your output. Your output should account for singular and plural values. For example, the word Foo would result in: F occurs 1 time o occurs 2 times Next, use the following function to generate output.

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

Code.py:

def getCharacterFrequency(str):
    str = str.lower()
    result = {}
    for x in str:
        if not result.get(x):
            result[x] = 1
        else:
            result[x] += 1
    return result

def printCharacterFrequency(result):
    for x in result.keys():
        print("'",end="")
        print(x,end="")
        print("' occures",result[x],"time",end="")
        if(result[x] > 1):
            print("s",end="")
        print(".")

printCharacterFrequency(getCharacterFrequency("Babble"))

Output:

b occures 3 times. a occures 1 time. occures 1 time occures 1 time Process finished with exit code 0

Add a comment
Know the answer?
Add Answer to:
Question 2 - Objects You are to implement the following functions: getCharacterFrequency -10pts This function takes...
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
  • PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set...

    PLEASE CODE IN PYTHON Run-length encoding is a simple compression scheme best used when a data-set consists primarily of numerous, long runs of repeated characters. For example, AAAAAAAAAA is a run of 10 A’s. We could encode this run using a notation like *A10, where the * is a special flag character that indicates a run, A is the symbol in the run, and 10 is the length of the run. As another example, the string KKKKKKKKKKKKKBCCDDDDDDDDDDDDDDDKKKKKMNUUUGGGGG would be encoded...

  • python3.x Write a function print_letter_count(word) that takes a string as a parameter and prints each unique...

    python3.x Write a function print_letter_count(word) that takes a string as a parameter and prints each unique character (in alphabetical order and lowercase) together with their frequency. For example: consider the following word: Programming The characters are: 1 x p, 2 x r, 1 x o, 2 x g, 1 x a, 2 x m, 1 x i and 1 x n. The output would be: a: 1 g: 2 i: 1 m: 2 n: 1 o: 1 p: 1 r:...

  • 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...

  • Write code in Java. In this step, functions and methods are synonymous. You may assume that...

    Write code in Java. In this step, functions and methods are synonymous. You may assume that strings only contain uppercase letters A through Z. 1. Write a function as follows: o Receive a string as a parameter. o Count the length of the string Count the occurrences of each distinguishable symbol in the string (use an array or map/dictionary). o Divide the factorial of the length by the product of the factorials of the occurrences. o Return the quotient as...

  • 2) Write a function stringManip that takes in a character string and returns a character string...

    2) Write a function stringManip that takes in a character string and returns a character string following these rules: a) any vowel (a, e, i, o, u) is replaced by the character '&' b) any numeric character has 1 added to it (if it was 9, it becomes O instead) (str2num() is useful here) c) all lowercase characters become uppercase d) replace the final character with '!' e) append the length of the string before this step to the end...

  • Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You...

    Programs 1. String Utilities In this exercise you will implement several utility functions involving strings. You will place all of your function prototypes in a header file named string utils.h and all of your function definitions in a source file named string utils.c. You should implement your own main test driver program to test your functions, but you need not hand it in. a. void addChar(char *str, char c, int n) - this function should add a char c at...

  • Flatten Text: Write a function to implement simple string flatten operation with the help of number...

    Flatten Text: Write a function to implement simple string flatten operation with the help of number of occurrence of characters. For example, the string xxyzzzzzxxx results in x2ylz5x3. In case the "flattened" string doesn’t result in becoming shorter than what we started with, then the function simply returns the starting string. The string should use only uppercase and lowercase letters (a through z). #include <iostream> using namespace std; string flattenText(string str) {    string flattenedText = "";    return flattenedText;...

  • trouble with formatting titles :( Your friend is in charge of recording titles of books in...

    trouble with formatting titles :( Your friend is in charge of recording titles of books in the library they work in. Since they know you're in EECS 183, they've asked you to write a function that turns a lowercase string into Title Case format. This way, they can use the function to correctly format the titles without having to worry about capitalizing anything themselves. The function you will implement is stringToTitleCase, which takes a lowercase string str as input and...

  • using basic c++ and use no functions from c string library b) Write a fragment of...

    using basic c++ and use no functions from c string library b) Write a fragment of code (with declarations) that reads a pair of words from the keyboard and determines how many letters in corresponding positions of each word are the same. The words consist only of the letters of the English alphabet (uppercase and lowercase). For example, if the words are coat and CATTLE, the following output should be generated: 012245 0122 matching letter in position 0 t is...

  • 2. This question is about processing strings 2.1 [4 marks] Given a string str, we can...

    2. This question is about processing strings 2.1 [4 marks] Given a string str, we can calculate a checksum of the string by summing up the ASCII codes of the characters in the string. For example, the checksum of a string "apple" is 530, which equals to 'a' + 'p' + 'p' + 'l' + 'e' = 97 + 112 + 112 + 108 + 101. Write a function int calculate_checksum(char str[] that calculates and returns the checksum of the...

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