Python Forum
Cant understand the question...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant understand the question...
#1
Question 
3. Given a 2 D Array of N X M Dimension, Write a function that accepts this array as well as two
numbers N and M. The method should return the top-left N X M sub matrix e.g:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
]
top_left_sub_matrix (matrix, 2, 2) -> should return:
[
[1, 2]
[4, 5]
]
Reply
#2
What have you tried?

Hint: Create a new empty list inside the function, then iterate over the matrix and you'll get the rows. Then you can limit the rows and the colums. The rows, if you count them for example or use itertools.islice. The colums can be addressed with the square brackets [:2].

The naive solution:
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]


# if matrix size is huge, then
# this method is inefficient
# but for small lists, do not care...
for row in matrix[:2]:
    print(row[:2])
from itertools import islice


matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
]


for row in islice(matrix, 0, 2):
    print(row[:2]) # <- creates a new list from index 0 to index 2 (exclusive)
Output:
[1, 2] [4, 5]
The rest is your task.

You should also use the REPL to manually select rows and colums.
>>> matrix[0]
[1, 2, 3]
>>> matrix[0][:2]
[1, 2]
Pro-Tip: Later, after you've learned about lists (sequences in general), you can use numpy to do this task in a single step and memory efficient.

import numpy as np


matrix = np.array([
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9],
])

print(matrix[:2,:2])
Output:
[[1 2] [4 5]]
As you can see, the syntax for index access is more advanced. You can do this for each axis.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
I think the challenge in the question is that N and M are used in 2 different ways. The question states the matrix is NxM, then you get passed in to the function N and M but they are different - the example is 3x3 then 2x2.
So, to re-format the question (I hope correctly!):
Write a function that accepts a 2D array and two integers, Row and Cols. Function should then return the subset of the array that is the upper left corner, size Row x Cols.

That actually works out to be _relatively_ easy.
Good luck.
BashBedlam likes this post
Reply
#4
N and M are utilized in the question in two different ways, which is my opinion the problem. The question specifies that the matrix is NxM; after that, you are passed into the function N and M, although they are distinct; an example of this is 3x3 followed by 2x2.
Thus, I'll reformat the query as follows (I hope correctly!):
Create a function that takes two integers, Row and Cols, and a 2D array. the subset of the array that is the upper left corner, size Row x Cols, should then be returned by the function words from letters
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020