Posts

Showing posts from February, 2023
 File Handling Scenario Prof. Jekyll conducts classes with students and regularly makes notes in a text file. Each line of the file contains three elements: the student's first name, the student's last name, and the number of point the student received during certain classes. The elements are separated with white spaces. Each student may appear more than once inside Prof. Jekyll's file. The file may look as follows: John Smith 5 Anna Boleyn 4.5 John Smith 2 Anna Boleyn 11 Andrew Cox 1.5 samplefile.txt Your task is to write a program which: asks the user for Prof. Jekyll's file name; reads the file contents and counts the sum of the received points for each student; prints a simple (but sorted) report, just like this one: Andrew Cox 1.5 Anna Boleyn 15.5 John Smith 7.0 output Note: your program must be fully protected against all possible failures: the file's non-existence, the file's emptiness, or any input data failures; encountering any data error should ...
 File Handling (ii) Scenario The previous code needs to be improved. It's okay, but it has to be better. Your task is to make some amendments, which generate the following results: the output histogram will be sorted based on the characters' frequency (the bigger counter should be presented first) the histogram should be sent to a file with the same name as the input one, but with the suffix '.hist' (it should be concatenated to the original name) Assuming that the input file contains just one line filled with: cBabAa samplefile.txt the expected output should look as follows: a -> 3 b -> 2 c -> 1 output Tip : Use a   lambda  t o change the sort order Python sorted() key sorted() function has an optional parameter called ‘key’ which takes a function as its value. This key function transforms each element before sorting, it takes the value and returns 1 value which is then used within sort instead of the original value. For example, if we pass a list of strings i...
File Handling Lab (i)  Scenario A text file contains some text (nothing unusual) but we need to know how often (or how rare) each letter appears in the text. Such an analysis may be useful in cryptography, so we want to be able to do that in reference to the Latin alphabet. Your task is to write a program which: asks the user for the input file's name; reads the file (if possible) and counts all the Latin letters (lower- and upper-case letters are treated as equal) prints a simple histogram in alphabetical order (only non-zero counts should be presented) Create a test file for the code, and check if your histogram contains valid results. Assuming that the test file contains just one line filled with: aBc samplefile.txt t he expected output should look as follows: a -> 1 b -> 1 c -> 1 output Tip : We think that a dictionary is a perfect data collection medium for storing the counts. The letters may be keys while the counters can be values. Mycode: a="text.txt" his...
  File handling Copying files - a simple and functional tool Now you're going to amalgamate all this new knowledge, add some fresh elements to it, and use it to write a real code which is able to actually copy a file's contents. Of course, the purpose is not to make a better replacement for commands like  copy  (MS Windows) or  cp  (Unix/Linux) but to see one possible way of creating a working tool, even if nobody wants to use it. Look at the code in the editor. Let's analyze it: lines 3 through 8: ask the user for the name of the file to copy, and try to open it to read; terminate the program execution if the open fails; note: use the  exit()  f unction to stop program execution and to pass the completion code to the OS; any completion code other than  0   says that the program has encountered some problems; use the   errno  v alue to specify the nature of the issue; lines 10 through 16: repeat nearly the same action, but this time...