Functions, namespace, memory management: Difference between revisions

From 22118
Jump to navigation Jump to search
mNo edit summary
 
(5 intermediate revisions by the same user not shown)
Line 19: Line 19:


== Exercises to be handed in ==
== Exercises to be handed in ==
# Make a function, that returns the relevant one-letter designation for the correct amino acid, when you give it a codon (3 bases). You can find a [[codon list]] here. If something invalid is given as input to the function, raise an exception. Maybe make a dict with codons. You should also have a bit of code apart from the function so you can test it.
The 2 first exercises are re-use from earlier. It is for a good purpose as will be seen later.<br>
# Make a function that calculates the factorial. Add some input control to the function to make sure you get positive integers, when you ask for a number. Consider if you want to raise an exception if the function gets invalid data or you want to halt the program.
You go back to solo git use to maintain your git skills. Commit every exercise to your private exercise repository.
# Make a function '''fastaread(filename)''' which takes a filename as a parameter and returns 2 lists, first list is the headers, second list is the sequences (as single strings without whitespace). Add appropriate error handling to the function. You can test your function on the file ''dna7.fsa''.
# Make a function '''fastaread(filename)''' which takes a filename as a parameter and returns 2 lists, first list is the headers, second list is the sequences (as single strings without whitespace). Add appropriate error handling to the function. You can test your function on the file ''dna7.fsa''. In order to test at use the function you must build a small program around it.
# 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 the fasta file. Add appropriate error handling to the function. You can test your function on the file ''dna7.fsa''.
# 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 the fasta file. Add appropriate error handling to the function. You can test your function on the file ''dna7.fsa''. Build a small program around your function. If you first read the file with '''fastaread''' and then write a new file with '''fastawrite''', then if the files are identical, you know you have done right.
# 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 3 & 4, 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.
# 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 3 & 4, 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''.
# <font color="#AA00FF">Make a function that calculates the standard deviation (1.8355) of a list of numbers.  Use it on the numbers in ''ex1.dat''. You can either do the two-pass algorithm (looking at all the numbers twice) which is clear from the formula or the one-pass algorithm.</font><br>[[File:StandardDeviation.gif]]
# 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.
# Make a function '''fibonacci(no1, no2, count)''' which calculates the first '''count''' [https://en.wikipedia.org/wiki/Fibonacci_number 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).
# The [https://en.wikipedia.org/wiki/Hamming_distance 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.


== Exercises for extra practice ==
== Exercises for extra practice ==

Latest revision as of 13:35, 1 March 2026

Previous: Collaborative git Next: Comprehension, generators, iteration

Required course material for the lesson

Powerpoint: Functions - this will be short as it is a reminder.
Powerpoint: Namespace.
Powerpoint: Memory management.
Video: Functions in Python
Video: Examples and identifying data types
Resource: Example code - Functions
Video: Live Coding

Subjects covered

  • Short about functions
  • Namespace in Python
  • Memory management

Exercises to be handed in

The 2 first exercises are re-use from earlier. It is for a good purpose as will be seen later.
You go back to solo git use to maintain your git skills. Commit every exercise to your private exercise repository.

  1. Make a function fastaread(filename) which takes a filename as a parameter and returns 2 lists, first list is the headers, second list is the sequences (as single strings without whitespace). Add appropriate error handling to the function. You can test your function on the file dna7.fsa. In order to test at use the function you must build a small program around it.
  2. 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 the fasta file. Add appropriate error handling to the function. You can test your function on the file dna7.fsa. Build a small program around your function. If you first read the file with fastaread and then write a new file with fastawrite, then if the files are identical, you know you have done right.

Exercises for extra practice