Code Golf: String Rotations
Let's have a round of Code Golf!
String rotation is when you take a word and move some of its letters to the end of the word, so the first letter becomes the second letter, the second letter becomes the third, and so on. Last letter becomes first. Rotation can happen only in one direction →. Your task is to write a method that will receive two strings. It then must return an integer value of how many times needed to rotate the strings to be equal. As usual shortest solution wins.
Input
"hello", "llohe"
Output
3
Note
Rules
The signature of the contest entry MUST be:
Class codeGolf.StringRotation { ClassMethod Rotations(a As %String, b As %String) As %Integer { // Your solution here } }It is forbidden to modify class/signature, including but not limited to:
- Adding inheritance
- Setting default argument values
- Adding class elements (Parameters, Methods, Includes, etc).
It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:
ClassMethod Build(f As %Integer) { W ##class(myPackage.myClass).test(a) }The use of
$ZWPACKand$ZWBPACKis also discouraged.You can use this test case:
Class codeGolf.unittests.StringRotation Extends %UnitTest.TestCase { Method TestRotation() { Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("ABCD","CDAB"), 2) Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("ABCD","ABCD"), 0) Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("abcde","cdeab"), 3) Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("Nun","nNu"), 1) Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("Iris ","s Iri"), 2) Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("goose","osego"), 3) Do $$$AssertEquals(##class(codeGolf.StringRotation).Rotations("o","o"), 0) } }
Comments
the first letter becomes the last letter, the second letter becomes the second-to-last letter, and so on
This definition means string reverse rather than rotation, while you apparently meant right (or clockwise) string rotation which is:
1st character becomes the 2nd, last ones becomes the 1st, repeat until the first string will become equal to the second string. As to your sample:
hello -> ohell -> lohel -> llohe
Thanks, @Alexey Maslov, fixed.
There was actually supposed to be a hard mode, where rotation can happen in any (of two) directions, but I removed that.
38, not good if the strings will never match
ClassMethod Rotations(a As %String, b As %String) As %Integer
{
f i=0:1 ret:a=b i s b=$e(b,2,*)_$e(b)
}It seems, you rotate to the left instead to the right...
I agree, but that was to make it work with the unit tests that were provided!
My solution, assuming that B is originated from A by some number of left or right rotations
ClassMethod RotationRight(a As%String, b As%String) As%Integer
{
f i=0:1 ret:$e(a_a,$l(a)+1-i,*-i)=b i
}
ClassMethod RotationLeft(a As%String, b As%String) As%Integer
{
f i=0:1 ret:$e(a_a,i+1,$l(a)+i)=b i
}write##class(%Dictionary.MethodDefinition).%OpenId("...||Rotate...").Implementation.Size
gives 40 (for RotateRight) and 38 (for RotateLeft).
ClassMethod Rotations(a As %String, b As %String) As %Integer
{
Q $F(b_b,a)-$L(a)-1
}
Without $Find
ClassMethod Rotations(a As %String, b As %String) As %Integer
{
Q $L($P(b_b,a))
}
👍 M+

Splendid!
Looks like we have a winner!
wow