Interview Query

Triangle as Binary Array

Start Timer

0:00:00

Upvote
5
Downvote
Save question
Mark as completed
View comments (1)
Next question

Given an (integer) height h and base b, write a function draw_isosceles_triangle that returns the shape of the isosceles triangle using (a 2D list) of 0s and 1s, where 0 and 1 represent the space outside and inside of the triangle, respectively.

If the given height h and base b cannot make a triangle, return None.

Note: Valid arguments for b should be odd

Example 1

Input:

h = 2
b = 3

Output:

draw_isosceles_triangle(h,b) -> [
  [0,1,0],
  [1,1,1]
]

Example 2

Input:

h = 3
b = 9

Output:

draw_isosceles_triangle(h,b) -> [
  [0,0,0,0,1,0,0,0,0],
  [0,0,1,1,1,1,1,0,0],
  [1,1,1,1,1,1,1,1,1]
]

Example 3

Input:

h = 5
b = 9

Output:

draw_isosceles_triangle(h,b) -> [
  [0,0,0,0,1,0,0,0,0],
  [0,0,0,1,1,1,0,0,0],
  [0,0,1,1,1,1,1,0,0],
  [0,1,1,1,1,1,1,1,0],
  [1,1,1,1,1,1,1,1,1]  
]

Example 4

Input:

h = 9
b = 9

Output:

draw_isosceles_triangle(h,b) -> None
.
.
.
.
.


Comments

Loading comments