Commit ed9fbaba authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

add comment on exercise about matrix implementation

add 2nd implementation with very tiny comparison between
the 2 implementations
parent c5f7cd0a
......@@ -13,8 +13,12 @@ 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 implemet is the following:
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/
......@@ -22,6 +26,27 @@ The API (**A**\ pplication **P**\ rogramming **I**\ nterface) to implemet is the
:download:` <_static/code/>` .
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
.. literalinclude:: _static/code/
:language: python
:download:` <_static/code/>` .
.. _Scripting:
.. _Variables:
Variables, Expression and statements
\ No newline at end of file
# matrix is implemented by list of list
def matrix_maker(ligne, col, val=None):
m = []
for i in range(ligne):
c = [val]*col
return m
#---- functions that depends on the matrix srtructure
def matrix_size(m):
return len(m), len(m[0])
def matrix_get(matrix, i, j):
return matrix[i][j]
def matrix_set(matrix, i, j, val):
matrix[i][j] = val
def matrix_print(m):
im, jm = matrix_size(m)
for i in range(im):
print m[i]
#---- independant regarding matrix structure
def _check_matindex(matrix,i,j):
imax, jmax = matrix_size(matrix)
if (i < 0 or i >= imax) or (j < 0 or j>= jmax):
raise IndexError, "matrix index out of range"
def matrix_get_line(matrix, i):
im, jm = matrix_size(matrix)
line = []
for n in range(jm):
line.append(matrix_get(matrix, i, n))
return line
def matrix_set_line(matrix, i, val):
im, jm = matrix_size(matrix)
for n in range(jm):
matrix_set(matrix, i, n, val)
def matrix_get_col(matrix, j):
im, jm = matrix_size(matrix)
col = []
for n in range(im):
col.append(matrix_get(matrix, n, j))
return col
def matrix_set_col(matrix, j, val):
im, jm = matrix_size(matrix)
for n in range(im):
matrix_set(matrix, n, j, val)
if __name__ == '__main__':
m = matrix_maker(5, 3)
matrix_set(m,0, 0, 1)
matrix_set(m,0, 2, 2)
matrix_set(m,4, 0, 12)
matrix_set(m,4, 2, 15)
print "get line 0", matrix_get_line(m, 0)
print "get col 0", matrix_get_col(m, 0)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment