More with Lists

From 22101
Revision as of 11:02, 30 October 2024 by WikiSysop (talk | contribs) (→‎Exercises to be handed in)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Previous: Lists/Sequences Next: Simple Pattern Matching

Required course material for the lesson

Powerpoint: More with Lists
Video: More advanced methods with lists
Video: Miscellaneous list concepts and workings
Resource: Example code - Lists
Video: Live Coding

Subjects covered

Lists and sequences and some methods that operate on lists;

  • sort, which surprisingly sorts the list according to some principle,
  • reverse, which reverses a list,
  • split, which splits a sting into a list.
  • join, which concatenates a list into a string.

Exercises to be handed in

  1. Similar to the first exercise last week, make a program that takes the words you input and saves them in the words.txt file. The difference is that this time there may be several words on the input line, not just one. The output is still one word per output line.
  2. Make a program that reads all the words in the file words.txt into an list. First the words must be sorted alphabetically, then the list should be reversed (the first word shall be the last and vice versa), finally the resulting list should be written in words.txt - one word per line - the file is being overwritten.
  3. After having looked at the cleaned accession numbers in clean5.acc, you will have seen that the accession numbers are sorted. This means that you can use the much more powerful binary search method when searching for accession numbers. Repeat exercise 3 from last week, but this time use binary search instead of the linear search you did then. See what Wikipedia has to say about binary search. Binary search as a method is well described in the solution to the "guess a number" exercise.
  4. Calculate the three sums of the three columns in one reading of the file ex1.dat using split to separate the columns. The sums of the three columns are: -904.4143, 482.8410, 292.0515
  5. Improve on the previous exercise by making a program that calculates the sum of all columns in the file, no matter how many columns there are. Each column should be summed individually. You can assume that each row (line) has the same number of columns in the file.
  6. Make a Python program that can select specific columns from a column based (tab separated) file and save them in a new file. It should select the columns that you specify in the order you specify. Ask for input and output file names and column numbers (3 questions, example "ex1.dat", "2col.acc" & "3 1"). The program should work with any number of columns in the input file.

Exercises for extra practice

You will program a reverse polish notation calculator. This is somewhat complicated, so I will split up the job in a number of smaller exercises, where the one exercise builds on the previous. Shortly, reverse polish notation is a mathematical notation in which operators follow their operands, i.e. adding two numbers is written by "3 4 +" instead of "3 + 4". We will utilize a stack, which is simply a list, where elements are added to and removed from the bottom.

  • Let's start with making the overall structure; a loop that asks for input, which is one item/command at a time. Inside the loop, there can be a number of operations depending on the input. If you enter a number, it should be simply appended to your list (the stack, which is the word I will use in the rest of the exercises here). If you enter commands STOP, EXIT or QUIT, the loop should end and the program stop. If you enter something that the program does not understand it should write "Illegal command", but otherwise do nothing.
  • Add the following commands: PEEK, which displays the last number in the stack, and SHOW which will display the entire stack.
  • Add the standard math operations, +, -, * and /: These remove 2 numbers from the stack (the last number in stack is the first operand and the second last number is the second operand), perform the operation and appends the result back to the stack. You notice operand order matters for - and /. At this point we have a simple but functional calculator.
  • Add single number operations LOG and SQRT, which replaces the last number in the stack with the log/square root of the number.
  • Solidify your program by ensuring any operations first check if the required number of operands are on the stack before trying to perform the operation. Display "Not enough operands on the stack" and otherwise do nothing if the check fails. Hint: Maybe lists of zero, one and two operands commands could be useful.
  • Add the commands: DROP which simply removes the last number on the stack, =, which removes the last number on the stack and displays it, SWITCH, which switches the last two numbers on the stack, and DUP which duplicates the last number on the stack, i.e stack consists of 1, after DUP it consists of 1 1.
  • Add the commands: ABS, NEG which replaces the last number on the stack which the absolute/negative value of the number.
  • At this point you can probably work out some mathematical operations to add that require one or 2 operands by yourself. I can think of the SIN, COS & TAN operations, the INT taking the integer part of a number, EXPonential and SQUARE. Modulo operation with %. FACtorial. You could add constants like PI and E that simply puts an appropriate value on the stack.
  • Add stack control with CLEAR to empty the stack and extend the SWITCH to switch the numbers as two given position like "SWITCH 2 4" if given positions.