Article
· May 18, 2023 2m read

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

  1. The signature of the contest entry MUST be:

    Class codeGolf.StringRotation
    {
        ClassMethod Rotations(a As %String, b As %String) As %Integer
        {
        // Your solution here
    
        }
    }
    
  2. 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).
  3. 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)
    }
    
  4. The use of $ZWPACK and $ZWBPACK is also discouraged.

  5. 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)
    }
    
    }
    
Discussion (14)2
Log in or sign up to continue

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

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).