Advertisement
informaticage

Print odd sum column

Dec 3rd, 2021
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. matrix = [
  2.   [ 1, 2, 3, 4, 5 ], # 2 = matrix[0][1]
  3.   [ 1, 2, 3, 4, 5 ], # 2 = matrix[1][1]
  4.   [ 1, 2, 3, 4, 5 ], # 2 = matrix[2][1]
  5.   [ 1, 2, 3, 4, 5 ], # 2 = matrix[3][1]
  6.   [ 1, 2, 3, 4, 5 ], # 2 = matrix[4][1]
  7. ]
  8.  
  9. sum = [ 5, 10, 15, 20, 25 ]
  10.  
  11. def print_colum(matrix, col):
  12.   for riga in range(0, len(matrix)):
  13.     print(str(matrix[riga][col]) + " ", end="")
  14.  
  15. def sum_col(matrix, col):
  16.   sum = 0
  17.   for riga in range(0, len(matrix)):
  18.     sum = sum + matrix[riga][col]
  19.  
  20.   return sum
  21.  
  22. def sum_all_col(matrix):
  23.   sum_list = []
  24.   for col in range(0, len(matrix[0])):
  25.     sum_list.append(sum_col(matrix, col))
  26.  
  27.   return sum_list
  28.  
  29. def filter_odd_columns(matrix):
  30.   odd_colums = []
  31.   all_colum_sum = sum_all_col(matrix)
  32.  
  33.   for index, column_sum in enumerate(all_colum_sum):
  34.     if(column_sum % 2 != 0):
  35.       odd_colums.append(index)
  36.  
  37.   return odd_colums
  38.  
  39. def print_odd_columns(matrix):
  40.   odd_colums = filter_odd_columns(matrix)
  41.   for col in odd_colums:
  42.     print_colum(matrix, col)
  43.     print()
  44.  
  45. print_odd_columns(matrix)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement