# Boss Fight in Python

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

Suppose we have a binary list called fighters and another list of binary lists called bosses. In fighters list the 1 is representing a fighter. Similarly, in bosses list 1 representing a boss. That fighters can beat a boss’s row if it contains more fighters than bosses. We have to return a new bosses matrix with defeated boss rows removed.

So, if the input is like fighters = [0,1,1]

 0 1 1 0 0 0 0 0 1 0 1 1 1 1 1

then the output will be

 0 1 1 1 1 1

To solve this, we will follow these steps −

• fighter_cnt := sum of all elements of fighters

• result := a new list

• for each row in bosses, do

• if fighter_cnt <= sum of each element in row, then

• insert row at the end of result

• return result

Let us see the following implementation to get better understanding −

## Example

Live Demo

class Solution:
def solve(self, fighters, bosses):
fighter_cnt = sum(fighters)
result = []
for row in bosses:
if fighter_cnt <= sum(row):
result.append(row)
return result
ob = Solution()
fighters = [0, 1, 1]
bosses = [[0, 0, 0], [0, 0, 1], [0, 1, 1], [1, 1, 1]]
print(ob.solve(fighters, bosses))

## Input

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

## Output

[[0, 1, 1], [1, 1, 1]]
Updated on 02-Sep-2020 13:15:57