Stucked in Highlanders

# You must defeat the ogres
# But they are using black magic!
# Only the highlander soldiers are immune.
# Find highlanders, their names always contain "mac"

highlanderName = "mac"

# This function should search for a string inside of a word:
def wordInString(string, word):
    lenString = len(string)
    lenWord = len(word)
    # Step through indexes (i) from 0 to (lenString - lenWord)
    for i in range(0, lenString - lenWord):
        # For each of them step through indexes (j) of the word length
        for j in range(lenWord):
            # If [i + j]th letter of the string is not equal [j]th letter of world, then break loop
            if i + j != j:
                break
            # if this is the last letter of the word (j == lenWord - 1), then return True.
            if (j == lenWord - 1):
                return True
    # If loops are ended then the word is not inside the string. Return False.
    return False
    # ∆ Remove this when the function is written.

# Look at your soldiers and choose highlanders only
soldiers = hero.findFriends()
for soldier in soldiers:
    if wordInString(soldier.id, highlanderName):
        hero.say(soldier.id + " be ready.")
        
# Time to attack!
hero.say("ATTACK!!!")

What’s wrong with my code?
There are only 8 soldiers.

You are missing the string and word array comparison using the indexes.

# If [i + j]th letter of the string is not equal [j]th letter of world, then break loop
            if i + j != j:
                break

Add the arrays with the index.

        if string[i+j] != word[j]:
            break
2 Likes

On a separate note, I just noticed that the comment says “world” not “word”. Who is the best person to contact to correct the typo? Thanks.

If [i + j]th letter of the string is not equal [j]th letter of world, then break loop

Thanks! It’s worked now! :smiley: