Here's another variation:
rotn=: 1 :'{&((65 97 +/~ i.26) m&|.@[`[`]} i.256)&.(a.&i.)'
1 rotn 'testing'
uftujoh
13 rotn 'testing'
grfgvat
13 rotn 13 rotn 'testing'
testing
_1 rotn 1 rotn 'testing'
testing
In this expression, each arbitrary constant is used only once:
26: letters in the alphabet
65: 'A'
97: 'a'
256: the number of character codes
I could replace 256 with #a. but I do not think that adds any clarity.
That said...
rottn=: 1 :'{&((65 97 +/~ i.26) m&|.@u:@[`[`]} a.)&(3&u:)'
--
Raul
On Fri, Dec 23, 2011 at 11:27 AM, Raul Miller <[email protected]> wrote:
> Wait, bad idea... that rot13 only works because 13 is a factor of 26.
>
> Instead (and this is a fast bug fix, I have not thought much about
> more elegant forms, though several paths for rephrasing come to mind):
>
> rotN=: 1 :'{&((65 97 +/ m |. i.26) ((65 97 +/i.26)}) i.256)&.(a.&i.)'
>
> --
> Raul
>
> On Fri, Dec 23, 2011 at 11:21 AM, Raul Miller <[email protected]> wrote:
>> Here's what I use for rot13:
>>
>> rot13=: {&((65 97+/~i.2 13) |.@[} i.256)&.(a.&i.)
>>
>> If I wanted to generalize that, I would probably go with:
>>
>> rotN=: 1 :'rot13=: {&((65 97+/~i.2,m) |.@[} i.256)&.(a.&i.)'
>>
>> 13 rotN 'testing'
>> grfgvat
>> 13 rotN rot13 'testing'
>> testing
>>
>> --
>> Raul
>>
>> On Fri, Dec 23, 2011 at 11:00 AM, Lorenzo Costanzia di Costigliole
>> <[email protected]> wrote:
>>> NB. What is the most elegant way to rotate uppercase
>>> NB. and lowercase letters by n? ("rotation cipher")
>>>
>>> c2n=: a.&i.
>>>
>>> lc=: >&96 *. <&123
>>> uc=: >&64 *. <& 91
>>>
>>> s=: 'Btggn Rwgxhibph 2011 id iwt Y-Egdvgpbbxcv bpxaxcv axhi!'
>>>
>>>
>>>
>>> NB. I came up with those, but I think there’s still room for improvement
>>> NB. (Isn't there something in the J-Phrases?)
>>>
>>> NB. uppercase -> 2, lowercase -> 1, else -> 0
>>> cl=: +:@uc + lc
>>> cl`(] , ([: 26&| +&n)&.(-&97) ,: ([: 26&| +&n)&.(-&65))}&.c2n s
>>>
>>> ((] + ] ]`([: 26&| n + ])@.((0~:[)"0) [ - ]) (97&*@lc + 65&*@uc))&.c2n s
>>>
>>> NB. Enumerate all possibilities
>>> |: ((] + ] (26 # ])`([: 26&| (i.26) + ])@.((0 ~: [)"0) [ - ]) (97&*@lc
>>> + 65&*@uc))&.c2n s
>>>
>>> NB. Thanks!
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm