Question: NOTE: In Python 3. Dictionary and List Comprehensions histo(s) 30% (a) Define a function, histo(S) computing the histogram of a given string. histo(S) should return
NOTE: In Python
3. Dictionary and List Comprehensions histo(s) 30% (a) Define a function, histo(S) computing the histogram of a given string. histo(S) should return a list of characters in the input string S each paired with its frequency (the number of times character appears in the string). The space character should be excluded in the result. Characters must appear in the list ordered from least frequent to most frequent. For example, histo('Cpts355 --- Assign1') is: [('1', 1), ('3', 1), ('A', 1), ('C', 1), ('g', 1), ('i', 1), ('n', 1), ('p', 1), ('t', 1), ('5', 2), ('-', 3), ('s', 3)] Characters with the same frequency must appear in increasing alphabetical order. To implement sorting you can use the Python built-in function sorted. You dont need to write your own sorting code. (Hints: Use a dictionary to maintain the counts. But remember that your function should return a list of tuples. You should be able to build the dictionary in a single pass over the input string.) (b) Rewrite your histogram function using list comprehension. Name this function histo2(S). (Hint: Use the count() method of string to get the number of occurrences of a character in a string. For example: how many as are there.count('a') returns 3) Define a function testhisto() that tests your histo(S) and histo2(S)functions, returning True if the code passes your tests, and False if the tests fail. You can start with the following code: def histo1(S): #write your code here pass def histo2(): #write your code here pass def testhisto(): #write your code here; see the sample test function on page#4 pass

3" Dictionary and List Comprehensions-histo (s)-30% Define a function, histo (S) computing the histogram of a given string. histo (S) should return a list of characters in the input string S each paired with its frequency (the number of times character appears in the string). The "space character" should be excluded in the result. Characters must appear in the list ordered from least frequent to most frequent. For example, histo ('Cpts 355 Assignl') is: Characters with the same frequency must appear in increasing alphabetical order. To implement sorting you can use the Python built-in function sorted. You don't need to write your own sorting code (Hints: Use a dictionary to maintain the counts. But remember that your function should return a list of tuples. You should be able to build the dictionary in a single pass over the input string.) Rewrite your histogram function using list comprehension. Name this function his to2(S) . (Hint: Use the count() method of string to get the number of occurrences of a character in a string. For example: "how many a's are there".count ('a returns 3) Define a function testhisto ) that tests your histo (S) and histo2 (S) functions, returning True if the code passes your tests, and False if the tests fail You can start with the following code def histol (S): Write your code here pass def histo2 ) : Write your code here pass def testhistoO #write your code here; see the sample test function on page#4 pass
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
