String.prototype.isRepeated
Python (24)
lambda s:s in(s+s)[1:-1]
Checks if the string is a substring of itself concatenated twice, eliminating the first and last characters to avoid trivial matches. If it is, it must be a nontrivial cyclic permutation of itself, and thus the sum of repeated segments.
Regex (ECMAScript flavour), 11 bytes
Sounds like a job for regex!
^([^]+)\1+$
Test it here.
I've chosen ECMAScript, because it's the only flavour (I know) in which [^]
matches any character. In all others, I'd either need a flag to change the behaviour of .
or use [\s\S]
which is three characters longer.
Depending on how we're counting the flag, that could of course be a byte shorter. E.g. if we're counting pattern + flags (e.g. ignoring delimiters), the PCRE/Perl equivalent would be
/^(.+)\1+$/s
Which is 10 bytes, ignoring the delimiters.
Test it here.
This matches only strings which consist of at least two repetitions of some substring.
Here is a full 26-byte ES6 function, but I maintain that regular expression submissions are generally valid:
f=s->/^([^]+)\1+$/.test(s)
Pyth, 9
/:+zz1_1z
Or
}z:+zz1_1
These are both close translations of @xnor's python answer, except that they take input from STDIN and print it. The first is equivalent to:
z = input()
print((z+z)[1:-1].count(z))
0 for False, 1 for True.
The second line is equivalent to:
z = input()
print(z in (z+z)[1:-1])
False for False, True for True.
Pyth's official compiler had a bug related to the second one, which I just patched, so the first is my official submission.