In: Computer Science
Posting together because they are related.
Python
1. Create a program to count the number of occurrences of each letter of the alphabet in a text file, and print out the total for each letter (if greater than 0). Ignore numbers, punctuation, and special characters. Make sure to treat capital and lowercase letters as occurrences of the same letter. The text file (sample_text.txt) is attached to the assignment.
Example: For the text "My dog's name is Winston." The results would be:
A - 1
D - 1
E - 1
G - 1
I - 2
M - 2
N - 3
O - 2
S - 3
T - 1
W - 1
Y - 1
2. Rewrite # 1 above using a dictionary to hold each alphabetic character as a key, and the count of the characters as the value. Reuse as much of the code from #1 as you can! However, use different variable names to prevent errors caused by using the same names. (5 pts.)
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Program 1:
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
# reading File name from user
# comment below line if not required
# and pass File name in open function
File_name = str(input("Enter File Name: "))
# opening the File
File = open(File_name,"r")
# reading the complete data from the File using read()
# and converting it into all upper case letters
text = File.read().upper()
# A List of Alphabets
Alphabets =
["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
# for each value in the List of Alphabets
for x in Alphabets:
# counting the occurrence of x in text
count = text.count(x)
# if count is greater than 0
if(count > 0):
# printing x and its count
print(x,"-",count)
#code ended here
Input file (sample_text.txt):
Output:
Program 2: (As mentioned in the Question Mostly reused the Program 1 code.)
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
# reading File name from user
# comment below line if not required
# and pass File name in open function
File_name = str(input("Enter File Name: "))
# opening the File
File = open(File_name,"r")
# reading the complete data from the File using read()
# and converting it into all upper case letters
text = File.read().upper()
# A List of Alphabets
Alphabets =
["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
# creating a empty dictionary
Dict = {};
# for each value in the List of Alphabets
for x in Alphabets:
# counting the occurrence of x in text
count = text.count(x)
# if count is greater than 0
if(count > 0):
# inserting it into dictionary
Dict[x]=count
# printing the values in the dictionary
# used sorted function on keys to display in order
for x in sorted(Dict.keys()):
# printing key and its value
print(x,"-",Dict.get(x))
#code ended here
Input file (sample_text.txt):
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!