Unverified Commit 193f605d authored by Bertrand  NÉRON's avatar Bertrand NÉRON
Browse files

python2 -> python3

parent 6b129c1d
Pipeline #58672 passed with stages
in 12 seconds
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
def matrix_maker(ligne, col, val=None): def matrix_maker(ligne, col, val=None):
m = [] m = []
for i in range(ligne): for i in range(ligne):
c = [val]*col c = [val] * col
m.append(c) m.append(c)
return m return m
...@@ -12,26 +12,27 @@ def matrix_size(m): ...@@ -12,26 +12,27 @@ def matrix_size(m):
return len(m), len(m[0]) return len(m), len(m[0])
def matrix_get(matrix, i, j): def matrix_get(matrix, i, j):
_check_matindex(matrix,i,j) _check_matindex(matrix, i, j)
return matrix[i][j] return matrix[i][j]
def matrix_set(matrix, i, j, val): def matrix_set(matrix, i, j, val):
_check_matindex(matrix,i,j) _check_matindex(matrix,i,j)
matrix[i][j] = val matrix[i][j] = val
def matrix_print(m): def to_str(m):
im, jm = matrix_size(m) m_str = []
for i in range(im): for row in m:
print m[i] m_str.append('\t'.join([str(cell) for cell in row]))
m_str = '\n'.join(m_str)
return m_str
#---- independant regarding matrix structure #---- independant regarding matrix structure
def _check_matindex(matrix,i,j): def _check_matindex(matrix,i,j):
imax, jmax = matrix_size(matrix) imax, jmax = matrix_size(matrix)
if (i < 0 or i >= imax) or (j < 0 or j>= jmax): if (i < 0 or i >= imax) or (j < 0 or j >= jmax):
raise IndexError, "matrix index out of range" raise IndexError("matrix index out of range")
def matrix_get_line(matrix, i): def matrix_get_line(matrix, i):
...@@ -41,7 +42,8 @@ def matrix_get_line(matrix, i): ...@@ -41,7 +42,8 @@ def matrix_get_line(matrix, i):
for n in range(jm): for n in range(jm):
line.append(matrix_get(matrix, i, n)) line.append(matrix_get(matrix, i, n))
return line return line
def matrix_set_line(matrix, i, val): def matrix_set_line(matrix, i, val):
_check_matindex(matrix,i,0) _check_matindex(matrix,i,0)
im, jm = matrix_size(matrix) im, jm = matrix_size(matrix)
...@@ -56,22 +58,21 @@ def matrix_get_col(matrix, j): ...@@ -56,22 +58,21 @@ def matrix_get_col(matrix, j):
for n in range(im): for n in range(im):
col.append(matrix_get(matrix, n, j)) col.append(matrix_get(matrix, n, j))
return col return col
def matrix_set_col(matrix, j, val): def matrix_set_col(matrix, j, val):
_check_matindex(matrix,0,j) _check_matindex(matrix,0,j)
im, jm = matrix_size(matrix) im, jm = matrix_size(matrix)
for n in range(im): for n in range(im):
matrix_set(matrix, n, j, val) matrix_set(matrix, n, j, val)
if __name__ == '__main__': if __name__ == '__main__':
m = matrix_maker(5, 3) m = matrix_maker(5, 3)
matrix_set(m,0, 0, 1) matrix_set(m,0, 0, 1)
matrix_set(m,0, 2, 2) matrix_set(m,0, 2, 2)
matrix_set(m,4, 0, 12) matrix_set(m,4, 0, 12)
matrix_set(m,4, 2, 15) matrix_set(m,4, 2, 15)
matrix_print(m) print(to_str(m))
print "get line 0", matrix_get_line(m, 0) print("get line 0", matrix_get_line(m, 0))
print "get col 0", matrix_get_col(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