, , , , ,

[SOLVED] Cse2050 data structures and object-oriented design homework: 01

$25

File Name: Cse2050_data_structures_and_object_oriented_design_homework__01.zip
File Size: 593.46 KB

5/5 - (1 vote)

Letter frequency is the number of times letters of the alphabet appear on average in written language.  According to Wikipedia, in the English language, the character “e” appears most often, then the characters “t”, “a”, and “o”.

In this assignment, you will implement a letter frequency calculator to find i) the total number of times each letter occurs in a text file and ii) a percentage that shows how common the letter is in relation to all the letters in the text file.

You can import the string module in this assignment if you wish. Do not import any other modules, except for code that you write yourself.

f = open(‘frost.txt’)

for line in f:

print(line, end=”)  # The text file has a newline

# already at the end of each

# line.

f.close()

 

Fire and Ice

Some say the world will end in fire, Some say in ice.

From what I’ve tasted of desire I hold with those who favor fire.

But if it had to perish twice,

I think I know enough of hate

To say that for destruction ice Is also great And would suffice.

-Robert Frost

import string

def letter_count(file):
”’Counts the instances of each ascii character from a file and returns an alphabetized dictionary”’
with open(file) as f:
count = {} #initializes new dictionary
data = f.read()
data = data.lower() #reads and forces all characters as lower case
for char in data:
if char in string.ascii_lowercase:
count[char] = count.get(char, 0) + 1 #check a-z frequency with default value of 0
sorted_count = {} #initializes and sorts dictionary in alphabetical order
for key in sorted(count):
sorted_count[key] = count[key]
return sorted_count

def letter_frequency(dict_letters):
”’Given a dictionary of letters and their values, returns the ratio or frequency of each character in relation to the others in an alphabetized dictionary”’
total = sum(dict_letters.values())
freqs = {}
for letter, count in dict_letters.items():
freqs[letter] = count / total
return freqs

if __name__ == ‘__main__’:
#first test
frostdict = {‘a’: 13, ‘b’: 2, ‘c’: 6, ‘d’: 10, ‘e’: 23, ‘f’: 12, ‘g’: 2, ‘h’: 12, ‘i’: 23, ‘k’: 2, ‘l’: 6, ‘m’: 3, ‘n’: 9, ‘o’: 20, ‘p’: 1, ‘r’: 14, ‘s’: 14, ‘t’: 20, ‘u’: 5, ‘v’: 2, ‘w’: 8, ‘y’: 3}
assert(letter_count(‘frost.txt’)) == frostdict
#second test
frostfreq = {‘a’: 0.06190476190476191, ‘b’: 0.009523809523809525, ‘c’: 0.02857142857142857, ‘d’: 0.047619047619047616, ‘e’: 0.10952380952380952, ‘f’: 0.05714285714285714, ‘g’: 0.009523809523809525, ‘h’: 0.05714285714285714, ‘i’: 0.10952380952380952, ‘k’: 0.009523809523809525, ‘l’: 0.02857142857142857, ‘m’: 0.014285714285714285, ‘n’: 0.04285714285714286, ‘o’: 0.09523809523809523, ‘p’: 0.004761904761904762, ‘r’: 0.06666666666666667, ‘s’: 0.06666666666666667, ‘t’: 0.09523809523809523, ‘u’: 0.023809523809523808, ‘v’: 0.009523809523809525, ‘w’: 0.0380952380952381, ‘y’: 0.014285714285714285}
assert(letter_frequency(letter_count(‘frost.txt’))) == frostfreq “C:UsersartjsOneDrive – University of ConnecticutDocumentsSpring 24CSE 2050cse2050hw1.venvScriptspython.exe” “C:UsersartjsOneDrive – University of ConnecticutDocumentsSpring 24CSE 2050cse2050hw1letters.py”  Process finished with exit code 0

>>> counts = letter_count(‘frost.txt’)

>>> print(counts)

{`a`: 13, `b`: 2, (24 letter:count pairs omitted)}

>>> freqs = letter_frequency(counts)

>>> print(freqs)

{‘a’: 0.06190476190476191, ‘b’: 0.009523809523809525,

(24 letter:frequency pairs omitted)}

import letters
def highest_freq(file):
”’Given a file name, calls functions from letters.py and returns the letter with the highest frequency and its ratio”’
letter_count = letters.letter_count(file)
letter_freq = letters.letter_frequency(letter_count)
keymax = max(letter_freq, key=letter_freq.get)
return keymax, letter_freq[keymax]

ltr, freq = highest_freq(‘frost.txt’)
#third test
assert(ltr) == ‘e’
assert(freq) == 0.10952380952380952

“C:UsersartjsOneDrive – University of ConnecticutDocumentsSpring 24CSE 2050cse2050hw1.venvScriptspython.exe” “C:UsersartjsOneDrive – University of ConnecticutDocumentsSpring 24CSE 2050cse2050hw1highest_freq.py”  Process finished with exit code 0

 

Broadly speaking, we’ll grade this and all assignments on four main areas:

∗ input – text file

∗ output – dictionary of letter:count pairs

∗ input – dictionary of letter:count pairs

∗ output – dictionary of letter:frequency pairs

∗ input – a text file

∗ output – highest frequency letter in form of a tuple:
(letter, frequency)

 

 

 

Print statements are not valid tests – use assert instead.

 

 

 

 

 

Every function you write in this course should include a docstring.

Shopping Cart
[SOLVED] Cse2050 data structures and object-oriented design homework: 01[SOLVED] Cse2050 data structures and object-oriented design homework: 01
$25