Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rite a program that simulates some of the data manipulation performed by Excel. In particular your program should do the following:
- Display a menu with the options below:
- "Open": Opens a file. When this option is selected, the user will be asked for the filename. If the file reads successfully, the data from the file will be printed as a table nicely formatted. A file will read sucessfully if it exists and can be opened and the data in it constitutes a table (each line will have the same number of pieces of information separated by commas)
- good file (each line has 4 numbers):
- 1.2, 4.3, 7, 0
- 3, 5, 8.2, 9
- bad file (some lines have 4 numbers and other have 3 numbers):
- 1.2, 4.3, 7, 0
- 3, 5, 8.2
- The data that is read from the file should be stored in the program as a list of lists that has as many inner lists as lines in the file (one inner list for each line in the file). In the good example above, the data should be stored as: [[1.2, 4.3, 7, 0], [3, 5, 8.2, 9]]. In this list some numbers are integers and some are floats. If you prefer, you can read all the numbers as floats (e.g.: [[1.2, 4.3, 7, 0], [3, 5, 8.2, 9]]).
- In the table that is printed, rows are numbered and columns are annotated with letters (like in Excel). The user will be using the numbers and the letters to indicate rows or columns.
- In order to speed up the testing of my program, if the user does not enter a filename (they hit Enter without having typed anything), by default my program will use the filename data1.txt. See the 3-rd coice of menu option 1 from the sample run below.
- Find and print the minimum value in a row (given the row number) or a column (given the column letter).
- Find and print the maximum value in a row (given the row number) or a column (given the column letter).
- Compute and print the sum of all the elements in a row or a column
- Compute and print the average of all the elements in a row or a column
- Sort the whole table by the values in a certain column (reorder the rows s.t. the values in that column are sorted in ascending or descending order). The user will give 2 letters separated by a space: the column letter and another letter that spceifies the ordering. The letter 'a' or 'A' indicate ascending order and 'd' or 'D' would indicate descending order. If any of the letters for the column and for the order is invalid, the table will be left as is ( NO reordering will be performed). Hint: see how Python sorts a list of lists (the principle is the same as for sorting/comparing strings).
- Insert a row or a column, right before the one indicated by the user. The user indicates a row by a number and a column by a letter. The inserted row or column should contain zeros. NOTE that you can insert the last column or row. In that case, the indexes are not valid for the original list, but they would be for teh new list and thus you should recognize them as valid. See the examples of inserting row 6 in a table of 5 rows and column H in a table with F columns. But there are still invalid indexes to be checked for. See the example of attempting to insert row 12 in a table of 6 rows.
- Delete a row or a column indicated by the user (with a number for a row or a letter for a column).
- Modify an element at a certain cell in the table. The user will specify the row, the column and the new value (e.g.: 3,B, 2.6).
- "Save": Save the current data in the same file that it was loaded from (it will overwrite the original file with the current data).
- "Save As": Save this data in another file. When this option is selected, the user should be asked to enter a filename and then the program will save the data in a file with that name. You should check that the file does not already exist. If it exists the program should let the user know that and ask for a confirmation to save (and thus to overwrite that file). You can use the 'os.path.isfile()' function to check that a file exists.
- "Exit": Finish/stop the program (exit from the menu loop).
- Example:
- 1 - Open and load from a file
- 2 - Minimum
- 3 - Maximum
- 4 - Sum
- 5 - Average
- 6 - Sort (by column, ascending or descending)
- 7 - Insert
- 8 - Delete
- 9 - Modify an element
- 10 - Save
- 11 - Save as (specify new file name)
- 0 - Exit
- Your choice: 1
- Enter a filename (if you hit enter, data1.txt will be opened):
- -----------------------------------------------
- | | A | B | C | D | E |
- -----------------------------------------------
- | 1 | 1.00 | 2.00 | 3.00 | 4.00 | 5.00 |
- | 2 | 6.00 | 7.00 | 8.00 | 9.00 | 0.00 |
- | 3 | 1.10 | 2.20 | 3.30 | 0.10 | 0.30 |
- -----------------------------------------------
- Example:
- -----------------------------------------------
- | | A | B | C | D | E |
- -----------------------------------------------
- | 1 | 1.00 | 2.00 | 3.00 | 4.00 | 5.00 |
- | 2 | 6.00 | 7.00 | 8.00 | 9.00 | 0.00 |
- | 3 | 1.10 | 2.20 | 3.30 | 0.10 | 0.30 |
- -----------------------------------------------
- 1 - Open and load from a file
- 2 - Minimum
- 3 - Maximum
- 4 - Sum
- 5 - Average
- 6 - Sort (by column, ascending or descending)
- 7 - Insert
- 8 - Delete
- 9 - Modify an element
- 10 - Save
- 11 - Save as (specify new file name)
- 0 - Exit
- Your choice: 2
- Enter a row (as a number) or a column (as an uppercase letter): D
- Minimum is: 0.1
- Example:
- 1 - Open and load from a file
- 2 - Minimum
- 3 - Maximum
- 4 - Sum
- 5 - Average
- 6 - Sort (by column, ascending or descending)
- 7 - Insert
- 8 - Delete
- 9 - Modify an element
- 10 - Save
- 11 - Save as (specify new file name)
- 0 - Exit
- Your choice: 6
- Enter a column and the order(a/A-ascending, d/D-descending) separated by comma (e.g.: D,a) : D,a
- -----------------------------------------------
- | | A | B | C | D | E |
- -----------------------------------------------
- | 1 | 1.10 | 2.20 | 3.30 | 0.10 | 0.30 |
- | 2 | 1.00 | 2.00 | 3.00 | 4.00 | 5.00 |
- | 3 | 6.00 | 7.00 | 8.00 | 9.00 | 0.00 |
- -----------------------------------------------
- Example:
- 1 - Open and load from a file
- 2 - Minimum
- 3 - Maximum
- 4 - Sum
- 5 - Average
- 6 - Sort (by column, ascending or descending)
- 7 - Insert
- 8 - Delete
- 9 - Modify an element
- 10 - Save
- 11 - Save as (specify new file name)
- 0 - Exit
- Your choice: 7
- Enter a row (as a number) or a column (as an uppercase letter): 1
- -----------------------------------------------
- | | A | B | C | D | E |
- -----------------------------------------------
- | 1 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
- | 2 | 6.00 | 7.00 | 8.00 | 9.00 | 0.00 |
- | 3 | 1.00 | 2.00 | 3.00 | 4.00 | 5.00 |
- | 4 | 1.10 | 2.20 | 3.30 | 0.10 | 0.30 |
- -----------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement