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

Ice hunter - algorithm explanation

Hi,
Any pointer at algorithm teaching for the substring and shifted index m ethod? I search online but ti doesn’t seem to be a usual terminology.

Ice Hunter and Northwest both use the substring search algorithm, but I found the code comments and the level explanations (help/hints) quite poor. The hint talks about double loop, it does not explain the algorithm.

My son and I can code at basic level, but I am not familiar with the algorithm these levels try to teach.
I try to understand the algorithm, not the code.
We get the code working, but not the “why” behind:

  1. why do we “iterate through the start indexes only.”, what is the purpose of rightedge
  2. why using “i in range(rightEdge + 1):” to parse the word/text, as opposed to start from the beginning?
  3. Once in the nested loop, why using shiftedIndex, for what purpose, as opposed to start comparing from the substring’s beginning?

Thank you,

1 Like

Hi Al and welcome to the forum hope you find it useful and your problem gets solved and please post your code so we can help you solve your problem!
-Zax

Hi Zax,
Sorry but I don’t have a problem with the code itself, rather the level (ice hunter/ northwest) comments and hints explaining the algorithm, explaining what is necessary, and more importantly why.

So the question is rather trying to understand the algorithm referred to by these levels: substring search by shifted index.
The Hints mention it’s a well known algorithm but it does not explain it, it only explains nested loop.
I am looking for more guided explanation of the algorithm itself.

The coding part of it has already been covered in past forum/Q&A discussion, fore example:
https://pastebin.com/af3BPrEH


sorry I cannot paste all the links, new users cannot paste more than 2 links.

But I am not after coding help, rather algorithm explanation.

Thank you,

you can post as many links as you want too @Al-al
-Zax

Hi,
No I cannot

“Sorry, new users can only put 2 links in a post”
Proof:
https://imgur.com/a/dPfhiws

But let’s go back on topic…

I would appreciate any pointer to that indexshift search algorithm.
Searching online on that topics gives no results.

Thank you,

Hi Al-al,
I’m a learner as well - I’m about 3 paces ahead of you in the levels. Your questions are good!

I think:

For the first question, rightEdge is defined as the word length - substring length. If you started checking the first letter of the substring at the last letter of the word, then I suspect when it went on to check the second letter of the substring it would have nothing to compare it with, so it would send back an error. Putting the variable of rightEdge stops this happening.

For the third question, when you get to checking the letters, you want to uncorrect for the change you made with rightEdge, as you now want to check all the letters. You achieve this by adding i and j to make the new variable shiftedIndex.

I’m still thinking about your question 2. I’ll get back to you if I come up with an answer (or someone who knows more than me might be able to help :slight_smile:).

1 Like

Ah - think I’ve got question 2.

Say you have a 6 letter word abcdef. If you wanted to check for 1 letter, then you’d run the loop 6 times (starting on a, b, c, d, e, f).

If you wanted to check a 2 letter substring then you’d check the loop 5 times (starting on a, b, c, d, e). But 6 - 2 = 4 (not 5). Similarly if you wanted to check a 3 letter substring, you’d check the loop 4 times, but 6 - 3 = 3, not 4. By doing the deduction you take away an extra 1, so you have to add it back in.

That all makes sense in my head, but if it’s coming out as gibberwaffle then please say, and I’ll let someone else have a go at explaining!

2 Likes

I’m going to lean towards, jka’s thoughts on this. I’ve been busy with work, so am just now getting to this post, so not much time to ponder. What he says makes sense to me.

*she actually - but given that your assumption is (I suspect) statistically more likely, I’ll take it as a compliment :wink:

1 Like

lol…you hit it on the head. It was totally an assumption, and it did kind of have me worried…thanks for understanding :grin:

2 Likes