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:
- Reverse Cipher: "Hello World!" becomes "dlroW olleH!"
- 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
Aequal to 0. - Case is ignored, so
Aandaare considered the same. - Non-alphabetical characters remain unchanged.
Rules
-
The signature of the contest entry MUST be:
Class codeGolf.CrypticReversalBinary { ClassMethod Encode(Message) As %String { // 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.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
My current code size is 74 (where alphabetical characters only a-z,A-Z).
size = 74
ClassMethod Encode(m) As %String
{
f j=65,97{f i=0:1:25 s m=$tr(m,$c(j+i),i#2)} q $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?
F i=64:1:89,96:1:121{s m=$tr(m,$c(i+1),i#2)} q $re($e(m,0,*-1))_$e(m,*) //corrected
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
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
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,*)
PS: why is the correct result in the testNumbers method
321and not 213?
Thanks for noticing! Fixed.
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")
Your task is to implement this encryption efficientlyWhat 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)
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)
Yes, it's fewest characters.
??? What is the benefit or use of such code puzzle ???
😈
These techniques can help save valuable bytes. Very important if you are trying to run IRIS on an Apple II from 1970s.
- 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 µÝ Привет Мир!" ?
a-z, A-Z only. Everything else is considered non-alphabetic, although I guess non-latin characters would be a more correct term.
Here's one where the only command used is QUIT
{
b(c="") q $s(c?1a:$a(c)-1#2,1:c)_$s($i(i)'>$l(m):$$b($e(m,*-(i'=$l(m)*i))),1:"")
}
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?
The responses look like hacky Perl scripts