Python Program Statement :
Write a program that inputs a text file. The program should print the unique words in the file in alphabetical order. Uppercase words should take precedence over lowercase words. For example, 'Z' comes before 'a'.
The input file can contain one or more sentences, or be a multi line list of words.
An example input file is shown below:
sample.txt
"the quick brown fox jumps over the happy dog"
An example of the program's output is shown below:
Enter the input file name: sample.txt
brown
dog
fox
happy
jumps
over
quick
the
Description : Hi, in this article lets try to solve the above problem statement. As per the prblem statement we try to divide the above problem into four phases.
1) Read the file and store the contents into a variable.
2) Remove the special characters present in the data (if any)
3) Get only unique words
4) Sort the words accordingly and print.
Lets get started with the programming
Phase 1: Open the file, then read contents of file and close the file
file_name = input("Enter the input file name: ")
file_obj = open(file_name, "r")
lines = file_obj.read()
file_obj.close()
Phase 2: Take a string of special characters, use for loop to traverse it. Check if special char is present as part of the file contents if yes then replace the special char with and empty string. The data gets cleaned.
special_char = ',.?!'
for schar in special_char:
if schar in lines:
lines = lines.replace(schar, "")
Phase 3: Split the contents of the file in to list of words. Once we get each word take another list and check if word is not present in new list. If word is not present add the word to new list. This will generate new list with unique words.
all_words = lines.split()
unique_words = []
for words in all_words:
if words not in unique_words:
unique_words.append(words)
Phase 4: Once we get new list of unique words print them using for loop.
unique_words.sort()
for words in unique_words:
print(words)
Here is the Final Program :
file_name = input("Enter the input file name: ")
file_obj = open(file_name, "r")
lines = file_obj.read()
file_obj.close()
special_char = ',.?!'
for schar in special_char:
if schar in lines:
lines = lines.replace(schar, "")
all_words = lines.split()
unique_words = []
for words in all_words:
if words not in unique_words:
unique_words.append(words)
unique_words.sort()
for words in unique_words:
print(words)
Conclusion : Try out the above program yourself and comment down below if you have any queries.
0 Comments