Written by

Senior Cloud Architect at InterSystems
MOD
Article Eduard Lebedyuk · 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)
    }
    
    }
    

Comments

Alexey Maslov · May 18, 2023

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

0
Eduard Lebedyuk  May 18, 2023 to Alexey Maslov

There was actually supposed to be a hard mode, where rotation can happen in any (of two) directions, but I removed that.

0
Stuart Strickland · May 18, 2023
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)
}
0
Julius Kavay  May 18, 2023 to Stuart Strickland

It seems, you rotate to the left instead to the right...

0
Stuart Strickland  May 19, 2023 to Julius Kavay

I agree, but that was to make it work with the unit tests that were provided!

0
Julius Kavay · May 18, 2023

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

0
a c · May 18, 2023

ClassMethod Rotations(As %String, As %String) As %Integer
{
     $F(b_b,a)-$L(a)-1
}

0
a c · May 18, 2023

Without $Find

ClassMethod Rotations(As %String, As %String) As %Integer
{
    
     $L($P(b_b,a))
}

0