-2

Definition:Arranged array an arranged array is an array of dim 2 , shape is square matrix (NXN) and for every cell in the matrix : A[I,J] > A[I,J+1] AND A[I,J] > A[I+1,J]

I have an assignment to write a func that: gets a numpy array and returns True if - the given array is an arranged array False - otherwise

note: We CANNOT use loops, list comps OR recursion. the point of the task is to use numpy things. assumptions: we can assume that the array isn't empty and has no NA's, also all of the cells are numerics

My code isn't very numpy oriented.. :

def is_square_ordered_matrix(A):
    # Checking if the dimension is 2
    if A.ndim != 2:
        return False
    # Checking if it is a squared matrix
    if A.shape[0] != A.shape[1]:
        return False
    # Saving the original shape to reshape later
    originalDim = A.shape
    # Making it a dim of 1 to use it as a list
    arrayAsList = list((A.reshape((1,originalDim[0]**2)))[0])
    # Keeping original order before sorting
    originalArray = arrayAsList[:]
    # Using the values of the list as keys to see if there are doubles
    valuesDictionary = dict.fromkeys(arrayAsList, 1)
    # If len is different, means there are doubles and i should return False
    if len(arrayAsList) != len(valuesDictionary):
        return False
    # If sorted list is equal to original list it means the original is already ordered and i should return True
    arrayAsList.sort(reverse=True)
    if originalArray == arrayAsList:
        return True
    else:
        return False

True example:

is_square_ordered_matrix(np.arange(8,-1,-1).reshape((3,3)))

False example:

is_square_ordered_matrix(np.arange(9).reshape((3,3)))
is_square_ordered_matrix(np.arange(5,-1,-1).reshape((3,2)))
WalaWizon
  • 19
  • 7

1 Answers1

0

Simple comparison:

>>> def is_square_ordered_matrix(a):
...     return a.shape[0] == a.shape[1] and np.all(a[:-1] > a[1:]) and np.all(a[:, :-1] > a[:, 1:])
...
>>> is_square_ordered_matrix(np.arange(8,-1,-1).reshape((3,3)))
True
>>> is_square_ordered_matrix(np.arange(9).reshape((3,3)))
False
>>> is_square_ordered_matrix(np.arange(5,-1,-1).reshape((3,2)))
False

First, compare a[:-1] with a[1:], which will compare the elements of each row with the elements of the next row, and then use np.all to judge:

>>> a = np.arange(8,-1,-1).reshape((3,3))
>>> a[:-1]   # First and second lines
array([[8, 7, 6],
       [5, 4, 3]])
>>> a[1:]    # Second and third lines
array([[5, 4, 3],
       [2, 1, 0]])
>>> a[:-1] > a[1:]
array([[ True,  True,  True],
       [ True,  True,  True]])
>>> np.all(a[:-1] > a[1:])
True

Then compare a[:,:-1] with a[:, 1:], which will compare the columns:

>>> a[:, :-1]   # First and second columns
array([[8, 7],
       [5, 4],
       [2, 1]])
>>> a[:, 1:]    # Second and third columns
array([[7, 6],
       [4, 3],
       [1, 0]])
>>> a[:, :-1] > a[:, 1:]
array([[ True,  True],
       [ True,  True],
       [ True,  True]])

The result of row comparison and column comparison is the result you want.

Mechanic Pig
  • 6,756
  • 3
  • 10
  • 31
  • can u explain in words your solution please? – WalaWizon Jun 17 '22 at 12:03
  • @WalaWizon I updated it. Hope it can help you. – Mechanic Pig Jun 17 '22 at 12:09
  • returns ```True``` for inputs with more than 2 axes eg ```is_square_ordered_matrix(np.arange(35,-1,-1).reshape((3,3,4)))``` – Nin17 Jun 17 '22 at 12:16
  • @Nin17 It should not be difficult to add boundary conditions check. Here, we only focus on solving the core problems. If you like, you also need to check whether the size of the array is 0. – Mechanic Pig Jun 17 '22 at 12:20
  • or you could just change ```a.shape[0] == a.shape[1]``` to ```a.shape[0]**2 == a.size``` – Nin17 Jun 17 '22 at 12:21
  • @MechanicPig can you please explain why a[:-1] gives me the 1st and 2nd lines? i get that index -1 is the last index (n-1) but why does it give you the 2nd line also? sadly I cant see it in pythonTutor – WalaWizon Jun 18 '22 at 06:39
  • @WalaWizon You don't seem to know anything about [slicing](https://stackoverflow.com/questions/509211/understanding-slicing). `a[:-1]` uses the most basic slice, and the complete writing method is `a[0:-1]`, which will obtain the row 0 to the penultimate row of the array. More info about [numpy slicing](https://www.w3schools.com/python/numpy/numpy_array_slicing.asp). – Mechanic Pig Jun 18 '22 at 14:28