Skip to content
DeepForgeby svx
All problems
MediumLinear Algebrala-266

Memory Layout Contiguity

Problem

Return True if an array with the given shape and strides is C-contiguous (row-major).

Row-major strides satisfy stride[-1] = 1 and stride[i] = stride[i+1] * shape[i+1]. Dimensions of size 1 may have any stride and are ignored.

Hint

Transposing a matrix typically changes its layout from C-contiguous to non-contiguous views.

Test cases

case 1
in: [[2,3],[3,1]]
expected: true
case 2
in: [[2,3],[1,2]]
expected: false
case 3
in: [[3,1],[1,5]]
expected: true
case 4
in: [[],[]]
expected: true
Starter codePython
def is_c_contiguous(shape, strides):
    # Your code here
    pass

4 test cases · run your code against them in the editor