Python script to be run on the Unix timeshare, called Search. This program will emulate the most elementary functionalit
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
Python script to be run on the Unix timeshare, called Search. This program will emulate the most elementary functionalit
Tolkien1.txt Three Rings for the Elven-kings under the sky, Seven for the dwarf-lords in their halls of stone, Nine for Mortal Men doomed to die, One for the Dark Lord on his dark throne, In the Land of Mordor where the Shadows lie. One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them In the Land of Mordor where the Shadows lie.
Tolkien2.txt Last of all Hurin stood alone. Then he cast aside his shield, and wielded an axe two-handed; and it is sung that the axe smoked in the black blood of the troll-guard of Gothmog until it withered, and each time that he slew Hurin cried: 'Aure entuluva! Day shall come again!' Seventy times he uttered that cry, but they took him at last alive, by the command of Morgoth, for the Orcs grappled him with their hands, which clung to him still through he hewed off their arms; and ever their numbers were renewed, until at least he fell buried beneath them. Then Gothmog bound him and dragged him to Angband with mockery.
$ Search Ring Tolkien1 'Ring' found at: line 1, column 7 line 6, column 5 line 6, column 32 line 7, column 5 $ Search "nd t' Tolkien1 'nd t' found at: line 6, column 42 line 7, column 50 $ Search ';' Tolkien2 ';' found at: line 2, column 26 line 8, column 5 $ Search Ring Tolkien2 'Ring' not found $ Search Ring Usage: Search <string> <file> $ Search Usage: Search <string> <file> $ As always, your output must match the above format exactly Observe that if the user does not have exactly two command line arguments after the Search command, a usage message will be printed. The basic algorithm for this program will be to get each line of <file>, and search for all occurrences of the substring <string> within that line. When you find a match, save the line number and the position in the line at which the substring begins. When all lines have been searched, print out your match positions in the proper format, or print a message stating that the substring was not found. Note that when counting lines and columns, your counts must begin at 1, not at 0. See the following examples to learn different ways to iterate over the lines in a file. /Examples/Filel.py /Examples/File2.py /Examples/File.py /Examples/File.py /Examples/pa 6/FileCopy.py Also, see the documentation of function str.find() at https://docs.python.org/3.9/library/std ... e-type-str
# main() def main(): # read all lines into a list f = open('Matrix.py') # default mode is 'r' L = f.readlines (). f.close() # print the list print() print(L) print() # L.sort # print to a back-up file g = open('Matrix.py.bak', 'w') for line in L: g.write(line) # note that write() does not insert a newline # end for g.close() # another way to print to a file g = open( 'Matrix.py-2.bak', 'W') # re-use the file variable g for line in L: print(line, end='' file=g) # print() does insert a newline, supress it # end for g.close(). # end main() # if name ==' main main() # end if