In this code-golf, you will attempt to match a string that is tail-repeating with one character falling off the head on each iteration. The code needs to be able to accept a string, and return to the user whether the string meets the above criteria.
Examples:
"", "x", and "xx" -> False
EDIT: There is some speculation about whether this is theoretically correct. I will accept any implementation that returns the same value for the three above values as of now. However I do not have an official position on whether this is true or false. Hopefully someone can formalize this.
The above cases will always be false regardless of the circumstance because the string is too short to have the property of tail repetition with one character falling off the head. For example "xx"'s first "tail" would be "x" and there is nothing after the "xx", so it would be false. The character does not matter here, "x" is an example.
"abb" -> True
"abb" is the simplest string I can think of that should be true, because if you take what we will call the "tail" off of it, you get "bb", then knock one character off the head, and you get "b", which is the next character in the string.
"abcbcc" -> True
"abcbcc" is the next simplest string I can think of that should be true. So far in contemplating this problem, testing the truth from the beginning to the end of the string seems difficult, so I'll explain from the end up.
"c", "cb", "cba"
When you examine the string in reverse it seems easier to determine where the "first tail" ends. It ends at the first "c" in "abc", but this can only be inferred from examining the string in reverse, to the best of my knowledge so far.
Here is an example of a string that does not match:
"abcbca" -> False
If we try to apply the above reverse-proof method to it:
"a", "cb"
we realize at the second step when the first character in the second string is not "a", that this must be false.
Shortest code in characters to implement this logic in any programming language wins.
EDIT: If you find a solution other than the suggested one, it is still a valid entry (and very cool!)
x
should be accepted? The first string isx
; the second is the empty string, so end iteration. \$\endgroup\$e
denote the empty word,a
denote a letter in the alphabet, andw
denote a word over the alphabet. Define a functionf
from words of the alphabet to words of the alphabet recursively asf(e) = e
andf(aw) = aw f(w)
. Define the languageL
over the alphabet as the set of wordsl
for which there exists a wordw
such thatl = f(w)
. The language you're asking us to accept is the words inL
of length at least two, but that's an arbitrary additional constraint. \$\endgroup\$