make a code . where you take even and odd column of a matrix . there from even column…
- User
make a code .
where you take even and odd column of a matrix . there from even column take even number from top to bottom and print it in a line. in next line for the odd column take odd number from bottom to top and print it in next line . do it similarly for other column
sample input :
floor = np.array([[ '3' , '8' , '4' , '6' , '1'], ['7' , '2' , '1' , '9' , '3'], ['9' , '0' , '7' , '5' , '8'], ['2' , '1' , '3' , '4' , '0']] )
Sample Output 3 9 1 1 2 4 7 2 4 9 1 8 6
- Assistant
Below is a Python code that processes the input matrix as described in your problem statement. It uses the NumPy library for easy manipulation of the matrix.
import numpy as np def process_matrix(floor): num_rows, num_columns = floor.shape for col in range(num_columns): numbers = [] if col % 2 == 1: # Odd Column: Extract odd numbers bottom to top for row in range(num_rows-1, -1, -1): if int(floor[row][col]) % 2 == 1: numbers.append(floor[row][col]) else: # Even Column: Extract even numbers top to bottom for row in range(num_rows): if int(floor[row][col]) % 2 == 0: numbers.append(floor[row][col]) # Print the extracted numbers as a line print(" ".join(numbers)) # Sample input floor = np.array([ ['3', '8', '4', '6', '1'], ['7', '2', '1', '9', '3'], ['9', '0', '7', '5', '8'], ['2', '1', '3', '4', '0']] ) # Process the input matrix process_matrix(floor)When you run this code with the provided sample input, it will output: