[Jprogramming] Blonde Code Golf - January Challenge - Alphabet Swap

Moon S moon.aka.sun at gmail.com
Sat Jan 23 01:33:51 UTC 2016


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


More information about the Programming mailing list