Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program that reads in a file and counts the number of each letter in the file. Ignore anything that is not a letter

Write a program that reads in a file and counts the number of each letter in the file. Ignore anything that is not a letter and consider both upper case and lower case

You should write your program in a function called count_letters which accepts a single parameter filename.

Example Output

You should print out the frequency of each of the letters seen as follows:

For the contents of test.txt:

The count of the letters for the file test.txt is:

A: 1

T: 1

W: 1

a: 3

c: 1

d: 3

e: 5

f: 2

g: 1

h: 2

i: 7

l: 3

m: 2

n: 3

o: 2

p: 2

s: 6

t: 1

w: 1

For the contents of words.txt:

The count of the letters for the file words.txt is:

A: 2

C: 4

D: 5

E: 1

F: 2

I: 3

L: 1

M: 6

N: 4

P: 7

Q: 1

S: 3

U: 1

V: 2

a: 164

b: 26

c: 87

d: 52

e: 251

f: 20

g: 40

h: 7

i: 171

j: 1

l: 123

m: 84

n: 130

o: 84

p: 40

q: 26

r: 94

s: 144

t: 141

u: 153

v: 25

x: 5

Getting Started

You can copy and paste the following in to get started.

 

def count_letters(filename):

pass

if __name__ == "__main__":

count_letters("words.txt")

THIS IS WHAT THE TEXT FILE "test.txt" includes:

This is a simple file With a few lines And some odd spacing

THIS IS WHAT THE TEXT FILE "words.txt" INCLUDES:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec non augue sit amet magna placerat accumsan. Donec molestie nibh dapibus tellus hendrerit scelerisque. Nulla pretium accumsan tellus, blandit egestas magna commodo non. Cras commodo enim et sapien scelerisque auctor. Pellentesque id libero gravida urna pharetra dignissim id vel eros. Duis ac ante eu quam finibus gravida. Mauris sagittis nulla in leo dictum elementum. Maecenas eleifend, risus sit amet bibendum laoreet, nunc nisl ultricies augue, eu sollicitudin nibh magna eget purus. Aliquam sagittis ornare metus sit amet molestie.

Suspendisse at consectetur ligula. Maecenas finibus ex et lectus pretium sagittis. Nullam arcu ipsum, tempor at accumsan vel, cursus eget lorem. Pellentesque volutpat faucibus eleifend. Proin congue congue massa vel aliquet. Pellentesque eget mi ullamcorper, eleifend eros non, dapibus magna. Donec mattis vitae purus ac feugiat.

Etiam congue mauris enim, vel ultrices purus ultrices vel. Praesent faucibus purus ex, eget porta nibh viverra a. Mauris ullamcorper elit a eleifend ullamcorper. Integer aliquet nulla augue, eget tincidunt orci ornare ut. Fusce est felis, ornare quis ex ut, pulvinar tristique sapien. Ut bibendum ante vel semper commodo. Sed volutpat ac mi nec blandit. Quisque sed dolor ac purus facilisis vehicula.

Vestibulum finibus lectus id leo pellentesque tincidunt. Integer accumsan, magna non fringilla tristique, nulla elit tristique nunc, id pellentesque metus elit vel massa. Maecenas ut malesuada velit. Vivamus faucibus consequat magna vel blandit. Nam quam sem, maximus non molestie vitae, commodo a quam. Pellentesque eleifend eu erat sed scelerisque. Morbi id vulputate elit. Integer lobortis ligula nec orci eleifend, id elementum tellus fringilla. Donec sed turpis orci. Curabitur nunc dolor, semper ac justo sit amet, congue accumsan magna.

Fusce gravida, enim sed sagittis interdum, orci nisi eleifend neque, ac faucibus odio ex eu lorem. Aenean eu elit venenatis, imperdiet velit tempus, pellentesque lacus. Nam feugiat arcu ac purus ornare, non mattis ante convallis. Sed sollicitudin sed nisi nec lacinia. Cras congue malesuada tempus. Pellentesque sit amet ligula a enim lobortis accumsan. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions