[Adenturer] Count Links


#1

Several hours of planning, Few hours of coding. One hour of arrangement. Yes, it’s a new level Count Links

It’s the second level about linked list. So it’s better to play “Linked Keys” first.


#2

Hello,
the level is working and is pretty cool.
Thanks for this new level


#3

I need help @Bryukh with this:

# Each cannon has from 1 to 5 shells and shoot them once.
# The first (index 0) has 1, the second - 2 and etc.

# The function find the element by index in a linked list.
def elementByIndex(linkedList, index):
    current = linkedList
    while True:
        
        current=current.next
        if not current:
            break
        return current
        

# The reference to the linked list of cannons.
cannonList = hero.findNearest(hero.findFriends())

while True:
    enemies = hero.findEnemies()
    if len(enemies) > 0:
        # We need a cannon with the certain number.
        hero.say(elementByIndex(cannonList, len(enemies) - 1))

My hero is stuck with Faule Mette


#4

You need to change your while True loop in the function. The loop is only supposed to run index times. Then return outside the loop.


#5

The level works, but it took me a while to figure out what I was doing:

I’ve pasted the starter code, but added some comments after the lines that confused me:

# Each cannon has from 1 to 5 shells and shoot them once.

This should be: “# Each cannon has from 1 to 5 shells and shoots them all at once.”

# The first (index 0) has 1, the second - 2 and etc.

# The function find the element by index in a linked list.
def elementByIndex(linkedList, index):
    current = linkedList
    # Iterate from 0 to "index" (exclusive):

I think this last line is a bit confusing, might be clearer if you just said: Iterate to the given index

    
        # Each element should have a property "next".
        # Reassign "current" with the next element:
        
        # If the current element is empty, stop it:

How am I supposed to stop it? I ignored this line and did just fine.

        
    # Return the current element.
    

# The reference to the linked list of cannons.
cannonList = hero.findNearest(hero.findFriends())

while True:
    enemies = hero.findEnemies()
    if len(enemies) > 0:
        # We need a cannon with the certain number.
        hero.say(elementByIndex(cannonList, len(enemies) - 1))


#6

break – this way it will work for indexes out of the list.


#7

Oh, I get it now, this is in case the index given is larger than the list.

# If the current element doesn't have a `next`, exit the loop

I was trying to figure out how to stop a cannon! :slight_smile:


#8

Good point. Thanks! [20chars]


#9

Wont work:

# Each cannon has from 1 to 5 shells and shoot them once.
# The first (index 0) has 1, the second - 2 and etc.

# The function find the element by index in a linked list.
def elementByIndex(linkedList, index):
    current = linkedList
    while index:
        current=current
        
        current=current.next
        
        if not current:
            
            break
        return current
            
        
        
        

# The reference to the linked list of cannons.
cannonList = hero.findNearest(hero.findFriends())

while True:
    enemies = hero.findEnemies()
    if len(enemies) > 0:
        # We need a cannon with the certain number.
        hero.say(elementByIndex(cannonList, len(enemies) - 1))


#10

Have you played levels about arrays? your code iterates ALL nodes and returns the last null. Use some counter variable, increase it for each iteration and break when it has a certain value.


#11

Can you give me like an example @Bryukh?


#12
someArray = [1, 3, 7, 10];
index = 0
while True:
    if someArray[index] == 7:
        break
    index += 1

Of course it’s an artificial example.
Or use for or while (expression)
I think it’s better for you to play array level and about loops (mountains and dessert)