Question

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

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

Implemented the code as per the requirement. As python is indentation specific, you may not get the formatted text while copying the code,
so I'm attaching the screenshots of the code for reference. Please make sure when you are executing the below code you have same format, especially tabs.

Please comment if any modification required.

code:

====

def print_hist_asc(histogram):
frequencies = {}
for char in histogram:
if char in frequencies:
frequencies[char] = frequencies[char] + 1
else:
frequencies[char] = 1
sort_frequncies = sorted(frequencies.items(), key=lambda x: x[1], reverse=False)
return sort_frequncies

result = print_hist_asc("aaaaabbbbcccdde")
for key,value in result:
print(key,value)

code screenshot:

=============

= 4 1 def print_hist_asc(histogram): 2 frequencies 3 for char in histogram: if char in frequencies: 5 frequencies[char] = fre

output:

=====

In [13]: runfile(C:/Users py3) e 1 d 2 C3 b4 a 5

Add a comment
Know the answer?
Add Answer to:
on python Design a function that can generate a histogram of characters in a string with...
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
  • 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.

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

  • Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that...

    Python Function Name: unscramble Parameters: a string Returns: a string Description: Write a function, unscramble, that takes an input string, and returns a the unscrambled version of the argument. To unscramble the string: When the string has an odd number of characters, middle character is the first character in the unscrambled result Pairs of remaining characters are added to the result, proceeding left to right from inner-most to outer-most characters. Example Call: unscramble('3cis1') Expected result: ics31 Example Call: unscramble('ocicssol') Expected...

  • PYTHON please -Create a function that takes a character chain and returns a histogram in a...

    PYTHON please -Create a function that takes a character chain and returns a histogram in a dictionary format. • The main program must take a dictionary and display it in alphabetical order. Note d.get(val1, val2) will return the value of the key val1 from dictionary d, otherwise val2. If we try to access directly d[val1], we get an error if key val1 does not exist.

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

  • In Python: LoadFile is a function that takes in a string (a filename) and then returns...

    In Python: LoadFile is a function that takes in a string (a filename) and then returns a list. The list is the contents of the file, where each element is a list of data from the file. Here's an example of using this function. The input file had four lines of text. >>> lines = LoadFile("test.txt") >>> print("OUTPUT", lines) OUTPUT ["Hello there", "I am a test file", "please load me in and print me out", "Thanks"]

  • Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument,...

    Define a function called AddEvenPosDigs(string), which takes a string (with symbols and characters) as an argument, and returns an integer. This function should add the digits of a string that are in an even position. If there are no digits, the function should return -1. As an example, the following code fragment: string = "a12b056jk"; result=AddEvenPosDigs(string); print(result) should produce the output: 8

  • Implement a Python function called revd that takes a chain of characters and returns a new...

    Implement a Python function called revd that takes a chain of characters and returns a new chain with the double of the elements but in reverse order. In the main program, ask the user to input the chain of characters, call the function and display the result Example : Please enter a chain of characters: abcd ddccbbaa NOTE: This is a repost. Please DO NOT use GLOBAL Variables

  • In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq)....

    In python please. Write a program that reads whitespace delimited strings (words) and an integer (freq). Then, the program outputs the strings from words that have a frequency equal to freq in a case insensitive manner. Your specific task is to write a function wordsOfFreqency(words, freq), which will return a list of strings (in the order in which they appear in the original list) that have a frequency same as the function parameter freq. The parameter words to the function...

  • Python The Python "<" and ">" comparison operators can be used to compare which string variable...

    Python The Python "<" and ">" comparison operators can be used to compare which string variable has a greater value based on comparing the ASCII codes of the characters in each string, one by one. To take some examples: "tets" > "test" returns True because the third letter of the first string, "t", has a greater value than the third letter of the second string, "s". "testa" > "test" returns True because—while the first four letters in both words are...

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