diff --git a/source/Modules_and_Packages.rst b/source/Modules_and_Packages.rst index 53734b981911804d4f50ffa7f70689b7885ba1d5..5777a21bc66d0992acefb9ce8582ad6cffcf3ec3 100644 --- a/source/Modules_and_Packages.rst +++ b/source/Modules_and_Packages.rst @@ -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/matrix.py :linenos: @@ -22,6 +26,27 @@ The API (**A**\ pplication **P**\ rogramming **I**\ nterface) to implemet is the :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 -------- diff --git a/source/Scripting.rst b/source/Scripting.rst deleted file mode 100644 index b00ab8bfff6eb4c15df0c0eaed976bed6894aa18..0000000000000000000000000000000000000000 --- a/source/Scripting.rst +++ /dev/null @@ -1,6 +0,0 @@ -.. _Scripting: - - -********* -Scripting -********* diff --git a/source/Variables.rst b/source/Variables.rst deleted file mode 100644 index ec525c26347ce7322ca65819fb5fb09c1798d495..0000000000000000000000000000000000000000 --- a/source/Variables.rst +++ /dev/null @@ -1,13 +0,0 @@ -.. _Variables: - -************************************ -Variables, Expression and statements -************************************ - - -Exercises -========= - -Exercise --------- -blabla \ No newline at end of file diff --git a/source/_static/code/matrix2.py b/source/_static/code/matrix2.py new file mode 100644 index 0000000000000000000000000000000000000000..01e654409edc8d3e1aaabce7b757b94abc15e744 --- /dev/null +++ b/source/_static/code/matrix2.py @@ -0,0 +1,77 @@ +# matrix is implemented by list of list +def matrix_maker(ligne, col, val=None): + m = [] + for i in range(ligne): + c = [val]*col + m.append(c) + 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): + _check_matindex(matrix,i,j) + return matrix[i][j] + +def matrix_set(matrix, i, j, val): + _check_matindex(matrix,i,j) + 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): + _check_matindex(matrix,i,0) + 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): + _check_matindex(matrix,i,0) + im, jm = matrix_size(matrix) + for n in range(jm): + matrix_set(matrix, i, n, val) + + +def matrix_get_col(matrix, j): + _check_matindex(matrix,0,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): + _check_matindex(matrix,0,j) + 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) + matrix_print(m) + print "get line 0", matrix_get_line(m, 0) + print "get col 0", matrix_get_col(m, 0) +