Functions: Difference between revisions

From 22116
Jump to navigation Jump to search
mNo edit summary
Line 26: Line 26:


== Exercises for extra practice ==
== Exercises for extra practice ==
You will program a [https://en.wikipedia.org/wiki/Reverse_Polish_notation reverse polish notation] calculator using the functions you want. 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 [https://en.wikipedia.org/wiki/Stack_(abstract_data_type) 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, '''EXP'''onential and '''SQUARE'''. Modulo operation with '''%'''. '''FAC'''torial. 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.

Revision as of 16:00, 29 August 2025

Previous: List manipulation Next: Simple pattern matching

Required course material for the lesson

Powerpoint: Functions
Resource: Example code - Functions

Subjects covered

  • Functions: a function is both a way of hiding complexity and a way of reusing code.
  • Arguments, scope of variables.

Exercises to be handed in

  1. The Hamming distance is the distance between two strings of equal length. Make a function which takes two strings as arguments and calculates the distance between them. Add error handling.
  2. Make a function fibonacci(no1, no2, count) which calculates the first count fibonacci numbers based on no1 and no2 and returns them in a list. The next number in a fibonacci sequence is the sum of the two previous numbers. Test it with printing the resulting list (one number per line) from fibonacci(0,1,20).
  3. Make a function that returns the unique elements of a list as a list. Try it on the accession numbers in ex5.acc, which contains 6461 unique accessions, but also make your own file with simple numbers.
  4. Make a function that calculates the standard deviation of a list of numbers. Try with the file ex1.dat, where you pool all the numbers in the the 3 columns into one list and get the result 1.8355. You can either do the two-pass algorithm (iteration through all the numbers twice) which is clear from the formula or the one-pass algorithm which you can derive from the formula if you are up to the challenge.
  5. Look at the fasta file dna7.fsa. It contains several fasta entries. Reading more than one entry in a file is more complex. Make a program that mostly consists of one function fastaread(filename) which takes a filename as a parameter and returns 2 lists, first list is the entry headers, second list is the entry sequences (as single strings without whitespace). Add appropriate error handling to the function. Just print the headers and sequences out pairwise as proof of concept.
  6. Make a function fastawrite(filename, headers, sequences) which takes a filename, a list of headers and a corresponding list of sequences as parameters and writes a fasta file with that file name. Add appropriate error handling to the function. You can test your function using the file dna7.fsa and your previous function - reading dna7.fsa and then writing a new fasta file, should make the files identical.
  7. Make a function that take a DNA sequence (string) as parameter and return the complement strand (reverse complement). Make the function generic and portable, i.e. not dependent on any external factors. Together with the functions you made in exercise 5 & 6, read the fasta entries in file dna7.fsa and write the complement strands in the new file revdna7.fsa. Add 'Complement strand' to each header.
  8. Make a function that calculates the GC-content of a DNA sequence. It takes a DNA (string) as parameter and returns the GC percentage. Make the function generic and portable, i.e. not dependent on any external factors. Together with the functions you made in exercise 5 & 6, read the fasta entries in file dna7.fsa and write only the sequences which has a GC-content percentage over 50% in the new file dna7GC.fsa.

Exercises for extra practice