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

[Adventurer] Hashing Magic


The new level from the Hashing series: Hashing Magic

Complete and use a hash function to place soldiers for the Magic!


I don’t know why, but I can’t iterate string in python

Строка 8: TypeError (ошибка типа данных): Need an object

So I change default code to

for i in range(0, len(name)):
        hash += ord(name[i])
1 Like

I’ve got the same problem as @RobAnybody with the code:

# Use the hash function to find places for soldiers.
totalCells = 135

# This function hashes a string value to an integer.
def hashName(name):
    hash = 0
    for letter in name:
        hash += ord(letter)
    # The hash value should be from 0 to 134.
    # Use modulus operation to "cut" the hash value.
    hash = hash.mod(135)
    return hash

soldiers = hero.findByType("soldier")
# For each soldier say his/her name (id) and hash of it.
# The name and the number should be splitted by whitespace.
for soldier in soldiers:
    hashedName = hashName(
    hero.say( + hashedName)

The error is:
Fix Your Code
Line 8: TypeError: need an object
The weird thing is that when I debug with hero.say(name) in the hashName Function it says the name of the soldier correctly.

1 Like

This code doesn’t even work (it produces the same “Need an object” error):

for letter in "this is a string":

It looks like a transpiler problem…

…Soon later, I found this comment inside the python-to-javascript transpiler on GitHub:

// TODO: for/in on a string should go through items, not indexes. String obj and string literal.

Thanks. I’ve changed the sample code.


My apologies if this is supposed to be obvious, but I don’t understand what I’m supposed to do when you say (line 11):

# Use modulus operation to "cut" the hash value.

I understand that there are only 135 total cells, but how am I supposed to know how high the sums from the hash function are, in order to modulus them into something in between 1 and 135? I can hack it, but I understand that there’s something that’s supposed to be intuitive that I missed here.


Remove 135 from hash until it will be less than 135.

For example
136 - 135 = 1 (remove once)
272 - 135 - 135 = 2 (remove twice)


Why would that be % 135 though? Shouldn’t % 135 be the remainder after dividing by 135?

1 Like

Sorry, there is a typo: should be “modulo operation”.


O_o maybe hash % 135 will be enough? :wink:

1 Like