On 02.09.2010 22:24, bearophile wrote:
simendsjo:
Suggestions for D-ifying the code is welcome.

Your unit tests are not good enough, they miss some important corner cases.
This my first version in D2:

import std.string: indexOf;

/// return True if s1 is a rotated version of s2
bool isRotated(T)(T[] s1, T[] s2) {
     return (s1.length + s2.length == 0) ||
            (s1.length == s2.length&&  indexOf(s1 ~ s1, s2) != -1);
}

unittest { // of isRotated
     assert(isRotated("", ""));
     assert(!isRotated("", "x"));
     assert(!isRotated("x", ""));
     assert(isRotated("x", "x"));


(...)
I agree that's much simpler, but s1 ~ s1 doesn't perform too well I think. Always creates a new heap allocation and copies the array..

Reply via email to