Question: Given a wall and a position, write a function to return how many neighbors the brick at that position has. The position within the wall

Given a wall and a position, write a function to return how many neighbors the brick at that position has.

The position within the wall is given from the top-left and may fall anywhere within a brick (not necessarily at the start).

For example, in the wall below:

- the brick at (0, 2) has four neighbors.

- the brick at (2, 6) has seven neighbors.

- the brick at (4, 2) has six neighbors.

- the brick at (5, 6) has three neighbors.

- the brick at (5, 7) has two neighbors.

+---+---+---+---+---+---+---+---+---+

| (7) | 1 | 1 |

+---+---+---+---+---+---+---+---+---+

| 5 | 1 | 1 | 2 |

+---+---+---+---+---+---+---+---+---+

| 3 | (4) | 1 | 1 |

+---+---+---+---+---+---+---+---+---+

| 1 | 1 | 1 | 1 | 3 | 2 |

+---+---+---+---+---+---+---+---+---+

| 1 | (2) | 1 | 3 | 2 |

+---+---+---+---+---+---+---+---+---+

| 1 | 1 | 1 | 1 | (3) | (2) |

+---+---+---+---+---+---+---+---+---+

brick_wall_7 = [

[7,1,1],

[5,1,1,2],

[3,4,1,1],

[1,1,1,1,3,2],

[1,2,1,3,2],

[1,1,1,1,3,2]

]

brick_wall_8 = [

[1,1,1,1,1,1,1,1],

[1,1,1,1,1,1,1,1],

[1,1,1,1,1,1,1,1]

]

brick_wall_9 = [

[1,5,1]

]

brick_wall_10 = [

[3],

[3]

]

analyze_brick(brick_wall_7, 0, 2) => 4

analyze_brick(brick_wall_7, 2, 6) => 7

analyze_brick(brick_wall_7, 4, 2) => 6

analyze_brick(brick_wall_7, 5, 6) => 3

analyze_brick(brick_wall_7, 5, 7) => 2

Additional test cases using some of the walls from the previous question:

analyze_brick(brick_wall_8, 1, 1) => 4

analyze_brick(brick_wall_9, 0, 4) => 2

analyze_brick(brick_wall_10, 0, 2)=> 1

Complexity analysis variables:

n: number of bricks in the wall

w: wall width

h: wall height (in layers)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!