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

Reply via email to