Play Home Contribute Github Join Us! Discourse Staff Members Github Contribution Guides Team

[Adenturer] Count Links

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.

1 Like

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

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

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.

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))

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

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:

Good point. Thanks! [20chars]

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))

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.

Can you give me like an example @Bryukh?

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)