Skip to content
DeepForgeby svx
All problems
EasyLinear Algebrala-246

Broadcasting Shape Check

Problem

Return True if two array shapes are compatible for NumPy-style broadcasting.

Align the shapes from the right; two dimensions are compatible when they are equal or one of them is 1, and missing leading dimensions are treated as 1.

Hint

Trailing dimensions must match or be 1; a shorter shape is padded with leading ones.

Test cases

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

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