Home > Back-end >  Survive the attack (7 kyu) - comparing arrays
Survive the attack (7 kyu) - comparing arrays

Time:12-12

I'm having troubles with this

Given two Arrays in which values are the power of each soldier, return true if you survive the attack or false if you perish.

CONDITIONS

Each soldier attacks the opposing soldier in the same index of the array. The survivor is the number with the highest value.

If the value is the same they both perish

If one of the values is empty(different array lengths) the non-empty value soldier survives.

To survive the defending side must have more survivors than the attacking side.

In case there are the same number of survivors in both sides, the winner is the team with the highest initial attack power. If the total attack power of both sides is the same return true.

The initial attack power is the sum of all the values in each array.

Here is what I have tried doing - it passes about half of the tests, and returns wrong values for the other half. I was not able to figure out why

    def is_defended(attackers, defenders):
    survivors_a = 0
    survivors_b = 0
    
    if attackers < defenders:
        survivors_b =1
        
    if attackers > defenders:
        survivors_a =1
    
    if attackers == defenders:
        survivors_a =0
        survivors_b =0

    if survivors_a == survivors_b and sum(attackers) > sum(defenders):
        return False
    
    if survivors_a == survivors_b and sum(attackers) < sum(defenders):
        return True
    
    if survivors_a == survivors_b and sum(attackers) == sum(defenders):
        return True
        
    elif survivors_a > survivors_b:
        return False
    
    elif survivors_a < survivors_b:
        return True
    

CodePudding user response:

The reason your code doesn't work is that your assumption about how comparison operators work on arrays is mistaken.

What you need to do instead is compare them element by element:

# I'm assuming that there's as many attackers as defenders, for the sake of brevity 
surviving_attackers = 0
surviving_defenders = 0
for i in range(len(attackers)):
  if attackers[i] > defenders[i]:
    surviving_attackers  = 1
  elif attackers[i] < defenders[i]:
    surviving_defenders  = 1
  else:
    pass # if strengths are equal, neither soldier survives

# and then compare the number of survivors and account for initial strength to resolve a tie if both sides have the same number of survivors

Or, for a more pythonic approach, do:

for attacker_strength, defender_strength in zip(attackers, defenders):
   if attacker_strength > defender_strength:
     surviving_attackers  = 1 
   # ... and so on ...

instead of:

for i in range(len(attackers)):
   #  ... comparison code ...

And to deal with lists of uneven length, use itertools.zip_longest instead of zip

CodePudding user response:

  • compare the first item in attackers with the first item in defenders; decide which one survives

  • compare the second item in attackers with the second item in defenders; decide which one survives.
    ...

  • compare the Nth item in attackers with the Nth item in defenders; decide which one survives

  • compare number of survivors

  • Related