Functions, namespace, memory management: Difference between revisions

From 22118
Jump to navigation Jump to search
Line 20: Line 20:
== Exercises to be handed in ==
== 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.<br>
The 2 first exercises are re-use from earlier. It is for a good purpose as will be seen later.<br>
You go back to solo git use to maintain your git skills. Commit every exercise to your private exercise repository.
You go back to solo git use to maintain your git skills. Commit every exercise to your private exercise repository.<br>
# 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.
For many of the exercises you need to make a small program that uses your function in order to test 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''. 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 '''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''.<br><br>
# 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''. 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.<br><br>
* Make a function normalize that takes as argument a list of numbers. The function normalizes the numbers betwen 0 and 1 and returns a normalized list. Normalization in this context is a linear transformation - [https://en.wikipedia.org/wiki/Feature_scaling#Rescaling_(min-max_normalization) min-max rescaling].<br><br>
* Make a new normalize function based on the above. This time the function takes three arguments; the list, a min, and a max that the values should be scaled/normalized between The default value for min and max are 0 and 1.<br><br>
* Make a program that reads the ex1.dat file and counts how many positive and negative numbers there are in each column. Display the result. Now use
your latest normalize function to normalize the numbers in each column between -1 and 1, and then again count the numbers of positive and negative in each column. Display. All this in one program.<br><br>


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

Revision as of 20:39, 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.
For many of the exercises you need to make a small program that uses your function in order to test it.

  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.

  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. 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 normalize that takes as argument a list of numbers. The function normalizes the numbers betwen 0 and 1 and returns a normalized list. Normalization in this context is a linear transformation - min-max rescaling.

  • Make a new normalize function based on the above. This time the function takes three arguments; the list, a min, and a max that the values should be scaled/normalized between The default value for min and max are 0 and 1.

  • Make a program that reads the ex1.dat file and counts how many positive and negative numbers there are in each column. Display the result. Now use

your latest normalize function to normalize the numbers in each column between -1 and 1, and then again count the numbers of positive and negative in each column. Display. All this in one program.

Exercises for extra practice