Here's a 35 character J implementation:
a.{~(a.j}~a.{~|.j=.65 97+/~i.26)&i.
Example use:
a.{~(a.j}~a.{~|.j=.65 97+/~i.26)&i. '019abcABC|xyzXYZ+='
019zyxZYX|cbaCBA+=
And, as it happens, it can also be used as a stand alone verb definition:
F=: a.{~(a.j}~a.{~|.j=.65 97+/~i.26)&i.
F '019abcABC|xyzXYZ+='
019zyxZYX|cbaCBA+=
Might be able to go shorter - I'd have to think about that.
Thanks,
--
Raul
On Fri, Jan 22, 2016 at 8:33 PM, Moon S <[email protected]> wrote:
> Here's another code golf challenge I found:
>
> http://www.blonde.net/blog/2016/01/18/blonde-code-golf-january-16
>
> In a string, swap a-z with z-a, A-Z with Z-A. Assume that you will always
> get a valid string and that spaces and special characters should remain
> unchanged.
>
> alphaSwap('abcxyz') // -> zyxcba
> alphaSwap('Hello, World!') // -> Svool, Dliow!
>
> The shortest solution there is in Ruby, 37 chars:
>
> s.gsub(/[a-z]/i){|c|(c.ord+4^31).chr}
>
>
> I did it in J obviously, with and without regex.
>
> A=:'019abcABC|xyzXYZ+='
>
> NB. spoiler......
>
>
>
>
>
>
> NB. my solutions:
>
> '[a-z]'(a.{~219-a.i.])R'[A-Z]'(a.{~155-a.i.])(R=.rxapply_jregex_) A
> (a.{~[:(]+((155-+:)*64&<*.<&91)+(219-+:)*96&<*.<&123)a.&i.) A
> (a.{~((i.65),(65+i._26),(91+i.6),(97+i._26),123+i.133){~a.&i.) A
>
> Definitely there must be a shorter solution for making that reordering
> array...
>
>
> - Georgiy
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm