Discussion
· 16 hr ago

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"), "321")
        }
    
        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("!@#$%^&*()')", "')(*&^%$#@!)")
        }
    }
    
Discussion (1)1
Log in or sign up to continue