1- # Given a 2D grid, each cell is either a wall 'W',
2- # an enemy 'E' or empty '0' (the number zero),
3- # return the maximum enemies you can kill using one bomb.
4- # The bomb kills all the enemies in the same row and column from
5- # the planted point until it hits the wall since the wall is too strong
6- # to be destroyed.
7- # Note that you can only put the bomb at an empty cell.
1+ """
2+ Given a 2D grid, each cell is either a wall 'W',
3+ an enemy 'E' or empty '0' (the number zero),
4+ return the maximum enemies you can kill using one bomb.
5+ The bomb kills all the enemies in the same row and column from
6+ the planted point until it hits the wall since the wall is too strong
7+ to be destroyed.
8+ Note that you can only put the bomb at an empty cell.
89
9- # Example:
10- # For the given grid
10+ Example:
11+ For the given grid
1112
12- # 0 E 0 0
13- # E 0 W E
14- # 0 E 0 0
13+ 0 E 0 0
14+ E 0 W E
15+ 0 E 0 0
1516
16- # return 3. (Placing a bomb at (1,1) kills 3 enemies)
17+ return 3. (Placing a bomb at (1,1) kills 3 enemies)
18+ """
1719
1820def max_killed_enemies (grid ):
1921 if not grid : return 0
2022 m , n = len (grid ), len (grid [0 ])
2123 max_killed = 0
2224 row_e , col_e = 0 , [0 ] * n
25+ # iterates over all cells in the grid
2326 for i in range (m ):
2427 for j in range (n ):
28+ # makes sure we are next to a wall.
2529 if j == 0 or grid [i ][j - 1 ] == 'W' :
2630 row_e = row_kills (grid , i , j )
27-
31+ # makes sure we are next to a wall.
2832 if i == 0 or grid [i - 1 ][j ] == 'W' :
2933 col_e [j ] = col_kills (grid , i , j )
30-
34+ # makes sure the cell contains a 0
3135 if grid [i ][j ] == '0' :
36+ # updates the variable
3237 max_killed = max (max_killed , row_e + col_e [j ])
3338
3439 return max_killed
3540
3641# calculate killed enemies for row i from column j
3742def row_kills (grid , i , j ):
3843 num = 0
39- while j < len (grid [0 ]) and grid [i ][j ] != 'W' :
44+ len_row = len (grid [0 ])
45+ while j < len_row and grid [i ][j ] != 'W' :
4046 if grid [i ][j ] == 'E' :
4147 num += 1
4248 j += 1
@@ -45,18 +51,43 @@ def row_kills(grid, i, j):
4551# calculate killed enemies for column j from row i
4652def col_kills (grid , i , j ):
4753 num = 0
48- while i < len (grid ) and grid [i ][j ] != 'W' :
54+ len_col = len (grid )
55+ while i < len_col and grid [i ][j ] != 'W' :
4956 if grid [i ][j ] == 'E' :
5057 num += 1
5158 i += 1
5259 return num
60+
61+
62+
63+ # ----------------- TESTS -------------------------
64+
65+ """
66+ Testsuite for the project
67+ """
68+
69+ import unittest
5370
54- grid = [
55- ["0" , "E" , "0" , "E" ],
56- ["E" , "E" , "E" , "0" ],
57- ["E" , "0" , "W" , "E" ],
58- ["0" , "E" , "0" , "0" ]]
59- print (grid )
71+ class TestBombEnemy (unittest .TestCase ):
72+ def test_3x4 (self ):
73+ grid1 = [["0" ,"E" ,"0" ,"0" ],
74+ ["E" ,"0" ,"W" ,"E" ],
75+ ["0" ,"E" ,"0" ,"0" ]]
76+ self .assertEqual (3 ,max_killed_enemies (grid1 ))
77+ def test_4x4 (self ):
78+ grid1 = [
79+ ["0" , "E" , "0" , "E" ],
80+ ["E" , "E" , "E" , "0" ],
81+ ["E" , "0" , "W" , "E" ],
82+ ["0" , "E" , "0" , "0" ]]
83+ grid2 = [
84+ ["0" , "0" , "0" , "E" ],
85+ ["E" , "0" , "0" , "0" ],
86+ ["E" , "0" , "W" , "E" ],
87+ ["0" , "E" , "0" , "0" ]]
88+ self .assertEqual (5 ,max_killed_enemies (grid1 ))
89+ self .assertEqual (3 ,max_killed_enemies (grid2 ))
6090
61- print (max_killed_enemies (grid ))
91+ if __name__ == "__main__" :
92+ unittest .main ()
6293
0 commit comments