[SOLVED]CodeCombat - Yeti Eater

The lurkers code is an array, right? (I think)

You’re finding the consequences of cheating on the earlier levels is coming back to harm you, since you’re struggling with the late ones. It’s worthwhile to re-implement the ones you cheated on.

When you write enemies = hero.findEnemies(), the variable enemies is set to an array of all enemies. The following code then iterates through that array and does something with each element.

This level is similar, although you want to iterate backward through the array.
What’s the index of the last element in the array?

Is it yetiF? (20 char)

That would be the value you get from yetis[index], which is what we want (so we can attack it).
What value of index will give us yetiF?

My code still doesn’t work:

# Yetis surround us and we need to defeat them.
# Luckily the wizard had time to cast the sleep spell.
# Your hero can devour the yetis' vital powers when they are defeated.
# Defeat them in the order from weakest to the strongest.

# The wizard sorted enemies, but in the order from the strongest to the weakest.
wizard = hero.findNearest(hero.findFriends())
yetis = wizard.findEnemies()

# You need iterate the yetis list in the reverse order with a 'for-loop'.
# The start value should be 'len(yetis) - 1'.
# Iterate while the index greater than -1.
# Use the negative step -1.
len(yetis) - 1
yetis[index] = [yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]
    # Attack each enemy while its health greater than 0.
for yeti in yetis:
    yetis[index] = hero.findEnemies()
    if enemy:
        while enemy.health > 0:
            hero.attack(enemy)

There’s also an error:

Screenshot 2021-06-01 10.12.35 AM

Let’s go step by step working it out here, and only then run the code in the editor, alright? We don’t expect it to work beforehand.

Okay. (20 characters)

What value of index will give us yetiF?

I don’t get what you mean.

If we have a list of any type of element, we can get each element using an index.

arr = ["cat", "dog", "munchkin"]

hero.say(arr[0]) # First element, `cat`
hero.say(arr[1]) # Second element, `dog`
hero.say(arr[2]) # Third element, `munchkin`
hero.say(arr[len(arr) - 1]) # Last (third) element, `munchkin`

# To get `munchkin`, we need to use index `2`

Our yetis list is essentially the same, with more elements.

yetis = [yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]

What value of index will give us yetiF ?

yetis[F]? (20 char)

Not quite, we’re looking a for the number index that represents where yetiF is in the list.

Index #    0      1      2      3      4      5
yetis = [yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]

Oh. So, yetis[5]? (20 char)

Great!
Continuing backwards through the array, what index do we need for yetiE?

yetis[4] (20 characters)

Continuing this process all the way through, what’s the last index we will check before we run out of yetis?

yeti[0] (20 characters)

Perfect!

This process we went through, using the index of the last yeti, then the second last, etc until index 0, is exactly what we want our final program to do. Let’s start turning it into code!

I think using a while loop will be most intuitive.
How should you replace the XXX values to

  1. start at index 5, the last element
  2. decrement the index by 1, so we move backwards through the array
  3. have index 0 be the last index

?

index = XXX
while XXX:
    hero.say(index)
    index XXX

The results from hero.say should be the same numbers you were saying above.

So do I turn

yetis[index] = [yetiA, yetiB, yetiC, yetiD, yetiE, yetiF]

into

yetis[index] = [yeti0, yeti1, yeti2, yeti3, yeti4, yeti5]

?