-
-
Save roman-on/d9bb4a51363302f0f2ebff304260622d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Write a program that receives from the user: | |
1. Path to Text File (String) | |
2. Name of one of the actions: rev, sort or last (string) | |
The file whose path is passed as an argument contains rows of word sequences in small English letters separated by a single space. | |
Depending on the user-recorded action the program performs: | |
• sort - The program prints all words in a file that has been transferred as a sorted alphabetical list, without duplicates. | |
• rev - The program prints the characters in each line of the file in the reverse order, that is, from beginning to end. | |
• last - The program receives from the user another parameter which is an integer (n), and prints the last n lines in the file (assume that the number is positive and also less than or equal to the number of lines in the file). | |
An example of a program input and lecture file | |
A file called sampleFile.txt: | |
i believe i can fly i believe i can touch the sky | |
I think about it every night and day spread my wings and fly away | |
Run the program on the sampleFile.txt file: | |
>>> Enter a file path: c:\sampleFile.txt | |
>>> Enter a task: sort | |
['about', 'and', 'away', 'believe', 'can', 'day', 'every', 'fly', 'i', 'it', 'my', 'night', 'sky', 'spread', 'the', 'think', 'touch', 'wings'] | |
>>> Enter a file path: c:\sampleFile.txt | |
>>> Enter a task: rev | |
yks eht hcuot nac i eveileb i ylf nac i eveileb i | |
yawa ylf dna sgniw ym daerps yad dna thgin yreve ti tuoba kniht i | |
>>> Enter a file path: c:\sampleFile.txt | |
>>> Enter a task: last | |
>>> Enter a number: 1 | |
i think about it every night and day spread my wings and fly away | |
""" | |
file1 = r"C:\....." | |
# sort - The program prints all words in a file that has been transferred as a sorted alphabetical list, without duplicates. | |
b = [] # New variable list | |
with open (file1, "r") as file1: # Open the file and close it automatically | |
for line in file1: # For line in the file | |
a = line.split() # Splitting the words in the line and saving in "a" | |
for word in a: # Sorting the words from "a" | |
if word not in b: # If the word not in variable "b" | |
b.append(word) # Adding the word to variable "b" | |
b.sort() # Sorting the words by a,b,c.. | |
print(b) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment