Skip to content
Snippets Groups Projects
Commit a0e9f04a authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

fix solutions about matrices and similarity

there was a mismatch between exercises and solutions
parent 631665b2
No related branches found
No related tags found
No related merge requests found
......@@ -482,28 +482,123 @@ This function have to take the phase as parameter
.. _matrix_exercise:
Exercise
--------
Write a program that calculates the similarity of 2 RNA sequences.
In a new file named ``matrix.py`` Implement a matrix and functions to handle it.
choose the data structure of your choice.
The API (**A**\ pplication **P**\ rogramming **I**\ nterface) to implement is the following:
**maker**
have parameter:
* the number of rows
* the number of columns
* a default value to fill the matrix
and return a matrix of rows_num x col_num
**size**
have parameter:
* a matrix
return the number of rows, and number of columns
**get_cell**
have parameter:
* a matrix
* the number of rows
* the number of columns
the content of cell corresponding to row number x col number
**set_cell**
have parameter:
* a matrix
* the row number of cell to set
* the column number of cell to set
* the value to set in cell
set the value val in cell specified by row number x column number
**to_str**
have parameter:
* a matrix
return a string representation of the matrix
**mult**
have parameter:
* a matrix
* val the value to multiply the matrix with
return a new matrix which is the scalar product of matrix x val
**get_row**
have parameter:
* a matrix
* the number of rows
return a copy of the row corresponding to row number
**set_row**
have parameter:
* a matrix
* the row number
* the value to put in cells of the row
set value in each cells of the row specify by the row number
**get_col**
have parameter:
* a matrix
* the column number
return a copy of the column corresponding to the column number
**set_col**
have parameter:
* a matrix
* the column number
* the value to put in cells
set all cells of a column with value
**replace_col**
have parameter:
* a matrix
* the column number to replace
* the list of values to use as replacement of column
replace a column col_no with list of values
**replace_row**
have parameter:
* a matrix
* the row number to replace
* the list of values to use as replacement of row
* To compute the similarity you need to parse a file containing the :download:`similarity matrix <_static/data/similarity_matrix>`.
replace a row row_no with list of values
**Hint**: use the module containing the functions that handle a matrix from previous chapter.
put this matrix.py file in a directory named "my_python_lib" in your home or Desktop
and import it in your current program (the similarity script must be placed elsewhere).
* The similarity of the 2 sequences is the sum of base similarities.
so you have to compare the first base of two sequences and use the matrix to get the similarity
from the similarity table, on so on for all bases then sum these similarities.
First implementation (list of columns)
.. literalinclude:: _static/code/matrix.py
.. literalinclude:: _static/code/similarity.py
:linenos:
:language: python
:download:`matrix.py <_static/code/matrix.py>` .
:download:`matrix.py <_static/code/similarity.py>` .
Second implementation (bad variable naming, no documentation)
......
......@@ -11,45 +11,6 @@ Exercises
=========
Exercise
--------
Implement a matrix and functions to handle it.
choose the data structure of your choice.
The API (**A**\ pplication **P**\ rogramming **I**\ nterface) to implement is the following:
We propose 2 implementations. These 2 implementations work with a list of lists as matrix modelling.
But it is possible to implement it with a single list or a dict of list, ...
The first implementation follow the api used explicit name for inner variables and good documentation.
.. literalinclude:: _static/code/matrix.py
:linenos:
:language: python
:download:`matrix.py <_static/code/matrix.py>` .
But the problem with this implementation is, if we decide to change the inner model for a dixt of list for instance.
We must reimplements most of the functions.
In the following implementation we have only 4 functions that handle directly the lists. All other functions
manipulate the matrix through these 4 functions. So if we change the inner model we will have to modifiy
only these functions. This implementation will be more maintainable.
But this implementation use one letter names for inner variables and is poorly documented which not help
to maintain or develop with this.
The Best solution should be the second implementation but with the name of variables and documentation as in the firsr
implementation.
.. literalinclude:: _static/code/matrix2.py
:linenos:
:language: python
:download:`matrix2.py <_static/code/matrix2.py>` .
Exercise
--------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment