From Project ROSALIND: Finding a Motif in DNA http://rosalind.info/problems/subs/ Version History 2020-04-13 — David M. Ng — Initial version.
This code uses the technique of “innocent until proven guilty”. This has the following pattern: substringYes = true // assume yes, we have a // substring (i.e., innocent) repeat if (char in substring) != (char in string) substringYes = false // proven “guilty” Basically, set a Boolean to true assuming all characters of the substring match consecutive string characters. As we check the substring character-by-character, if there is a mismatch set the Boolean to false. A common mistake is to set the Boolean to the match status of the current pair of characters; this will only tell us whether the last character of the substring matches. I.e., substringYes = (char in substring) != (char in string) Notice that to retrieve the character in the string, we find the character at position stringPos + substringPos - 1. We have to subtract 1 because Scratch starts positions (in strings and lists) with 1 rather than 0. I we did not subtract 1, we would commit the common “off by 1” error. This is common error in computing the number of times a loop has to repeat, in addition to indexing in strings and lists. It is important to consider such cases when you are testing your code.