Document stub.png This article or section may not have content matching Scratch Wiki editing standards. Please improve it according to Scratch Wiki:Guidelines and Scratch Wiki:Editing Conventions. (January 2019)
Reason: Formatting, should have more description

This uses the same main algorithms as Checking if a String Contains a String. Without 3.0's <() contains ()> block, these are no slower than the ones that only return booleans, but a builtin block is always faster than a script.

Group-Push

  • (location) stores the result of the script (scratch has no custom reporters)
  • (grouper) stores the string to compare to (sub :: custom-arg)
  • (i) stores the current group index
  • (x) selects the region for the group itself
define set location to location of [sub] in [base]
set [i v] to (1) // should be atomic
repeat (((length of (base)) - (length of (sub))) + (1))
  if <(location) = [0]> then
    set [grouper v] to [] // this begins the grouping with a blank string
    set [x v] to (i)
    repeat (length of (sub)) // this groups together the current sequence
      set [grouper v] to (join(grouper)(letter (x) of (base)))
      change [x v] by (1)
    end
    if <(grouper) = (sub)> then
      set [location v] to (i)
	  stop [this script v] // these two lines fake a return statment
    end
    change [i v] by (1)
  end
end
set [location v] to [0] // no location found

Convert this to Snap! by adding a script variables ((grouper):: grey)((i):: grey)((x):: grey) @delInput @addInput:: grey on the top and replacing set [location v] to (. . .:: grey) with report (. . .:: grey) :: control cap

Alternate Algorithm

This algorithm is less efficient and takes up more processing power but can be used anyways. For this algorithm, assume the following:

  • (starting letter) is the variable used to define what letter the current combination begins with.
  • (ending letter) is the variable used to define what letter the current combination ends with.
  • (letter#) is the variable used to define what specific letter if the string is being added on.
  • (changer) is the variable used to combine and check each possible combination.
  • (output) is the return statment
define check if [string a] contains [string b] // the custom block is checking if "string b" is in any combination in "string a"
set [output v] to [0] // because it will turn true throughout the combinations, if so
set [starting letter v] to [1] // sets the base letter originally to "1" for organization
set [ending letter v] to (length of (string a))
repeat (length of (string a)) // because the base, starting letter must be iterated by "1" by the length of the string itself
set [changer v] to [] // set it to nothing so far
repeat (length of (string a)) // to iterate through "string a" while changing the ending letter by "-1" the length of "string a"
set [letter# v] to (starting letter) // so the first letter is added on
repeat (((ending letter) - (starting letter)) + (1)) // so it tests the combination consisting of all characters from the starting letter to the ending one
set [changer v] to (join (changer) (letter (letter#) of (string a))) // adds a letter to the combination
change [letter# v] by (1) // moves on to the next letter to add to the combination
if <(changer) = (string b)> then // checks if the combination is true and within "string a"
set [output v] to (starting letter) // shows that "string b" is within "string a"
end
end
change [ending letter v] by (-1) // for testing out the next combination group with the same base, starting letter
set [changer v] to [] // resets the variable to prepare for a new group of combinations
end
set [changer v] to [] // resets the variable
change [starting letter v] by (1) // prepares for the next combination group with each combination beginning with the next base letter
set [ending letter v] to (length of (string a)) // since the "ending letter" value decreases by "1" each group
end
if <(string a) = (string b)> then // for the case of "string a" and "string b" both being blank and the above script not functioning therefore
set [output v] to [1]

Convert this to Snap ! by adding a script variables ((starting letter):: grey)((ending letter):: grey)((letter#):: grey)((changer):: grey)((output):: grey) @delInput @addInput:: grey block at the top and a report (output):: control cap at the end.