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

Slumbering Samp - 2 sets of codes

Hi, I have 2 sets of codes for this level, in my opinion, both sets should work and have the same result, but unfortunately, the second one doesn’t give me the right answer, can you have a look and help me with it ? thanks

yetis = hero.findByType("yeti")

# Implement the averageSize function from scratch:
def averageSize(enemies):
    sum = 0
    for i in range(len(enemies)):
        enemy = enemies[i]
        sum += enemy.size
    return sum/enemies.length

# Say the average size of the yetis:
avgSize = averageSize(yetis)
hero.say(avgSize)
yetis = hero.findByType("yeti")

# Implement the averageSize function from scratch:
def averageSize(enemies):
    sum = 0
    for enemy in enemies:
        sum += enemy.size
    return sum/enemies.length

# Say the average size of the yetis:
avgSize = averageSize(yetis)
hero.say(avgSize)

Not sure what the problem is. I combined both samples, separating the two code blocks with a ‘hero.say(“next”)’, so I could tell when the next block ran…both returned the same average, every time I ran it, or submitted it. Yes, that average changed with each new seed, but both blocks returned the same one.

However, if the question is “what’s the difference between the two methods?”, then…the first example is a slightly simplified version of a more advanced method. Typically, this method is preferred, as you have much more flexibility with it.

The second example is a very simple method, but limited in how it can be manipulated.

Both are valid methods.

1 Like