Question: Python Coding Homework Cant use ord or chr functions Part 1: Frequency Analysis You must implement three functions that can compute basic things about the
Python Coding Homework Cant use ord or chr functions
Part 1: Frequency Analysis You must implement three functions that can compute basic things about the letters that make up a string. Their definitions are detailed in this section. (6 points) def tally(letter, text):
Parameter(s): letter ----- a string; the letter to look for text ----- a string; the text to look in Return value: An integer: the number of times that letter appeared in text Assumptions: o text is an all lowercase string without any special characters. o The letter is not guaranteed to appear in text. How it works: o This is a reimplementation of what list.count() does, but with your own loop. Notes: o There are no provided tests for this function, you must test it on your own! o You are not allowed to use .count()!
Examples: tally("s","mississippi") 4
tally("m","mississippi") 1
tally("x","mississippi") 0 (7 points) def frequency(text): Return value: A list (of integers): how often each letter of the alphabet appears in text Assumptions: The standard English alphabet is used: "abcdefghijklmnopqrstuvwxyz" o All strings will be entirely in lowercase, and only contain valid letters in the alphabet. How it works: Count how many times each letter in the alphabet occurs in the text. Store all of these values in a list. o This list should be returned in alphabetical order: index 0 is the frequency of the letter "a" index 1 is the frequency of the letter "b" index 2 is the frequency of the letter "c" so on until the letter "z" Notes: o HINT: If you haven't written the tally() function yet. you should do that first. o Don't use list.index() or list.count()!
Examples: frequency("abcd") [1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
frequency("zoosarecool") [1,0,1,0,1,0,0,0,0,0,0,1,0,0,4,0,0,1,1,0,0,0,0,0,0,1] (8 points)
def common(frequencies):
Parameter(s): frequencies --- a list; the frequencies of letters in an unknown text (in alphabetical order) Return value: A tuple: the letter(s) that appeared most commonly in some piece of text Assumptions: o The standard English alphabet is used: "abcdefghijklmnopqrstuvwxyz" How it works: This function should compute the letters in the alphabet that are "common" in a text. A "common" letter appears more than 10% of the time in a given text. o If a letter is common, add it to the tuple of common letters. When you have checked all of the letters in the alphabet, return the tuple of common letters. The letters should be reported in alphabetical order. Notes: HINT: while the text itself is unknown, you can determine its length from the letter frequencies Examples:
common([0,0,0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0,0]) ('m', 'o')
common([4,0,0,2,7,0,0,4,7,0,0,3,0,2,1,0,0,0,7,10,0,0,0,0,0,0]) ('e','i','s','t')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
