Kamis, 04 Februari 2021

Typo in Count Letter Coding in examples (Python For Every One Cay Horstmann)

A simple program that counts the number of times each letter of the English alphabet occurs. Make your own file in *.txt. There are 26 letters of the alphabet for which we must maintain a count, we can use a list of 26 counters represented by integer values.
letterCounts = [0] * 26 # Create a list with 26 elements initialized to 0.
 

The number of occurrences for letter "A" will be maintained in counts[0], the count for
letter "B" in counts[1] and so on all the way through counts[25] for letter "Z".

Use ord function. By subtracting the code for the letter A, one obtains a value between 0 and 25 that can be used as an index to the letterCounts list:
 

code = ord(char) - ord("A")
letterCounts[code] = letterCounts[code] + 1
 

Note that all lowercase letters must be converted to uppercase before they are
counted. This is the program: 


Well it coudn'twork.... We have to add one lines like this: 


Input file read should be written twice or the code would only read the first letter.

Tidak ada komentar:

Posting Komentar