Written by

Senior Cloud Architect at InterSystems
MOD
Discussion Eduard Lebedyuk · Dec 16, 2025

Code Golf: Binary encoding!

Time for another round of code golf!

Develop a function that performs a two-step encryption process on a given string:

  • First Step: Reverse Cipher

    • Reverse the entire input string.
    • Relocate the last character of the original string (now the first character of the reversed string) to the end.
  • Second Step: Alphabetic Index Binary Encoding

    • For each alphabetic character, determine its zero-based index in the lowercase alphabet (a-z).
    • Replace odd-indexed characters with '1' and even-indexed characters with '0'.
    • Preserve non-alphabetic characters unchanged.

Example:

encrypt("Hello World!") should return "11100 01101!"

Explanation:

  1. Reverse Cipher: "Hello World!" becomes "dlroW olleH!"
  2. Binary Encoding:
    • 'd' (index 3) -> 1
    • 'l' (index 11) -> 1
    • 'r' (index 17) -> 1
    • 'o' (index 14) -> 0
    • 'W' (index 22) -> 0 ... and so on.

Your task is to implement this encryption efficiently

Notes

  • The index for alphabetical characters should start from 0, with A equal to 0.
  • Case is ignored, so A and a are considered the same.
  • Non-alphabetical characters remain unchanged.

Rules

  1. The signature of the contest entry MUST be:

    Class codeGolf.CrypticReversalBinary
    {
    
    ClassMethod Encode(Message) As %String
    {
        // 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.CrypticReversalBinary Extends %UnitTest.TestCase
    {
        Method TestSolution()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("abcdefg"), "1010100")
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("ABCDEFG"), "1010100")
        }
    
        Method testMixedCase()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("aBcDeF"), "010101")
        }
    
        Method testNumbers()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("123"), "213")
        }
    
        Method testPunctuation()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("a!b@c#"), "0@1!0#")
        }
    
        Method testSpaces()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("a b c"), " 1 00")
        }
    
        Method testLongWord()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("supercalifragilisticexpialidocious"), "0000010100110001001000110100101000")
        }
    
        Method testSentence()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("The quick brown fox jumps over the lazy dog."), "001 0101 011 1010 01001 101 10011 00000 011.")
        }
    
        Method testSpecialCharacters()
        {
            Do $$$AssertEquals(##class(codeGolf.CrypticReversalBinary).Encode("!@#$%^&*()')", "')(*&^%$#@!)")
        }
    }
    

Comments

Vitaliy Serdtsev · Dec 17, 2025

My current code size is 74 (where alphabetical characters only a-z,A-Z).

 

size = 74

ClassMethod Encode(mAs %String
{
 j=65,97{i=0:1:25 m=$tr(m,$c(j+i),i#2)$re($e(m,1,*-1))_$e(m,*)
}

"Hello World µÝ Привет Мир!" -> "риМ тевирП Ýµ 11100 01101!"

PS: why is the correct result in the testNumbers method 321 and not 213?

0
Stuart Strickland  Dec 17, 2025 to Vitaliy Serdtsev

 //f i=65:1:90,97:1:122{m=$tr(m,$c(i),i#2)$re($e(m,0,*-1))_$e(m,*)

F i=64:1:89,96:1:121{s m=$tr(m,$c(i+1),i#2)} $re($e(m,0,*-1))_$e(m,*) //corrected

0
Paul Waterman  Dec 17, 2025 to Stuart Strickland

Stuart, your program above fails some of the tests: 1s an 0s round the wrong way.

Here is my effort:

f i=$l(M),1:1:i-1{s c=$e(M,i),b=$s(c?1A:$a(c)-1#2,1:c)_$g(b)} q b

0
Stuart Strickland  Dec 17, 2025 to Paul Waterman

Hey Paul! I knew I should have tested it before posting!

I like your very nice FOR loop. It solves the problem I had trying to get under 72

0
Paul Waterman  Dec 18, 2025 to Stuart Strickland

a-z only is too hard using my method, so I have switched over to same as as you guys. Up to 73 now:

 f i=0:1:25,32:1:57{s m=$tr(m,$c(i+65),i#2)} q $re($e(m,1,*-1))_$e(m,*)

0
Eduard Lebedyuk  Dec 17, 2025 to Vitaliy Serdtsev

PS: why is the correct result in the testNumbers method 321 and not 213?

Thanks for noticing! Fixed.

0
Stuart Strickland · Dec 17, 2025

Q $TR($ZCVT($RE($E(Message,0,*-1))_$E(Message,*),"U"), "VERY MERRY XMAS AND HAPPY NEW YEAR TO THE BEST JOLLY QUICK CODE GOLF WIZARDS", "1010 00110 1000 011 10110 100 0001 10 110 1001 10110 00000 0010 0011 0010110")

0
Vitaliy Serdtsev · Dec 17, 2025
Your task is to implement this encryption efficiently
What does "efficiently" mean?

The condition of the Code Golf competition has always been to write the shortest code, not the fastest (see Code Golf Index)

0
Stuart Strickland  Dec 17, 2025 to Vitaliy Serdtsev

My guess is that efficiently, in code golf, means fewest characters. At a push and when there's a tie you could declare the fastest code the winner.

My entry above is not a serious attempt at code golf but if no-one posts the first answer there won't be a second answer. My real attempt stands at 72 characters. (63 failed a couple of unit tests)

0
Eduard Lebedyuk  Dec 17, 2025 to Stuart Strickland

Yes, it's fewest characters.

0
Luc Morningstar · Dec 17, 2025

??? What is the benefit or use of such code puzzle ???

 😈

0
Matthew Thornhill  Jan 7 to Luc Morningstar

These techniques can help save valuable bytes. Very important if you are trying to run IRIS on an Apple II from 1970s. 

0
Vitaliy Serdtsev · Dec 18, 2025
  • For each alphabetic character, determine its zero-based index in the lowercase alphabet (a-z).
  • Preserve non-alphabetic characters unchanged.

For clarify: does alphabetical characters include only the English letters a-z, A-Z, or all Unicode characters including "ªµºÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéyêëìíîïðñtóôõöøùúûüýþÿ" ?

What should be the correct result for the string "Hello World µÝ Привет Мир!" ?

0
Eduard Lebedyuk  Dec 18, 2025 to Vitaliy Serdtsev

a-z, A-Z only. Everything else is considered non-alphabetic, although I guess non-latin characters would be a more correct term. 

0
Stuart Strickland · Dec 18, 2025

Here's one where the only command used is QUIT

ClassMethod Encode(m) As %String
{
 $$b
b(c="") $s(c?1a:$a(c)-1#2,1:c)_$s($i(i)'>$l(m):$$b($e(m,*-(i'=$l(m)*i))),1:"")
}

0
Nicki Vallentgoed · Dec 23, 2025

Wow, the responses here taught me that a for loop can have multiple forparameter arguments.

And it's so obscure I do not think they should be used in every day development?

0
Dmitrii Baranov  Dec 23, 2025 to Nicki Vallentgoed

The responses look like hacky Perl scripts

0