Question

In: Computer Science

Posting together because they are related. Python 1. Create a program to count the number of...

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.)

Solutions

Expert Solution

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!


Related Solutions

Using Python, create a program that will act as a number convertor. This program will convert...
Using Python, create a program that will act as a number convertor. This program will convert an input decimal integer into binary and hexadecimal formats. a) Define a main function named numberconvertor(). Inside the main function, write all the logic of your code. [5% marks] b) Looping indefinitely (Hint: use infinite while loop), the program should give the user the 3 options given below and allow the user to select one among them. [15% marks] 1. converting a decimal number...
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
In this program: ================================================================== /* Program to count number of occurrences of a given string in...
In this program: ================================================================== /* Program to count number of occurrences of a given string in original string*/ #include <iostream> #include <cstring> #include <stdio.h> #include <iostream> using namespace std; int main() { const int SIZE = 40; char str[SIZE]; char str1[SIZE]; char searchString[SIZE]; int n; int l1, l2; int count = 0; printf("Enter a sentence: \n"); fgets(str,SIZE,stdin); printf("Enter a search word: \n"); fgets(searchString,SIZE,stdin); if (str1[strlen(str1) - 1] == '\n') { str1[strlen(str1)-1] = '\0'; } if (str[strlen(str) - 1] == '\n')...
Python! Create a program that does the following: Reads a number from the user. Calls a...
Python! Create a program that does the following: Reads a number from the user. Calls a function that finds all the divisors of that number. Calls another function to see if the number 7 is a divisor of the original number. Keeps reading input from the user and calling the function above until the user enters the letter ‘q’. Create 2 functions findDivisors() and lucky7(). Use docstrings to explain what each function does. Use the help() function to output the...
R Programming: create a vector for 1 to 31 and count the number of even and...
R Programming: create a vector for 1 to 31 and count the number of even and odds using ifelse()
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program:...
Create a Python script in IDLE or Kali Python3 CLI to create the following Python Program: Your program will create a username of your choice using a Kali Linux command "useradd -m mark", then set the password for that user using the Kali Linux Command "echo "mark:10101111" | chpasswd". Then you create a dictionary file using the Kali Linux command "crunch 8 8 01 > mylist.txt" Your python script should crack the password for that user and display on the...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
Python. Write a code that asks the user to enter a string. Count the number of...
Python. Write a code that asks the user to enter a string. Count the number of different vowels ( a, e, i, o, u) that are in the string and print out the total. You may need to write 5 different if statements, one for each vowel. Enter a string: mouse mouse has 3 different vowels
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT