Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active October 5, 2016 16:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeGolfScotland/7e35bb9b0315dab358892415e0d329c8 to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/7e35bb9b0315dab358892415e0d329c8 to your computer and use it in GitHub Desktop.
Code Golf September 2016

September 2016 - Reversed Alphabet

Task

Reverse the alphabet so that abc => zyx and ABC => ZYX

Examples:

("code") => "xlwv" ("Golf") => "Tlou" ("1Avva8TT") => "1Zee8GG"

^You can assume that you will always recieved a valid string

About Code Golf Scotland

@martincarlin87
Copy link

Language: Ruby

Length: 176

Solution:

def x(y) w = ''; y.split('').each do |c| c =~ /^\d/ ? w += c : (c == c.upcase ? w += (90 - ((c.ord + 25) - 90)).chr : w += (122 - ((c.ord + 25) - 122)).chr) end; return w end

@joaquinferrero
Copy link

joaquinferrero commented Sep 2, 2016

Language: Perl 5.14 (inspired by BrianDGLS' JavaScript solution)
Length: 42 40
Solution:

sub R{pop=~s/[a-z]/chr(31^4+ord$&)/rige}

@nevstokes
Copy link

Language: PHP
Length: 120
Solution:

function r($w){return preg_replace_callback('/[a-z]/i',function($m){$c=ord($m[0]);return chr(($c<91?155:219)-$c);},$w);}

@Luciam91
Copy link

Luciam91 commented Sep 2, 2016

Language: PHP
Length: 199
Solution:

function a($a){$b=range('a','z');$s='';foreach(str_split($a)as$l){$r=array_search(strtolower($l),$b);$c=ctype_upper($l);$i=25-$r;$n=($c)?ucfirst($b[$i]):$b[$i];$s.=($r !== false)?$n:$l;}return $s;}

@alessandrozucca
Copy link

alessandrozucca commented Sep 2, 2016

Language: C
Length: 147
Solution:

void r(char *c) { for(int i=0; i<strlen(c); i++) { if (iswlower(c[i])) c[i] = 'z' - (c[i] - 'a'); if (iswupper(c[i])) c[i] = 'Z' - (c[i] - 'A'); }}

Copy link

ghost commented Sep 5, 2016

Language: Javascript
Length: 67
Solution:

_=>_.replace(/[A-Z]/gi,$=>String.fromCharCode($.charCodeAt()+4^31))

@Kolin
Copy link

Kolin commented Sep 9, 2016

Language: PHP
Length: 76
Solution:
function r($x){$l=str_split($x);foreach($l as $i){echo chr(ord($i)+4^31);}}

@nevstokes
Copy link

nevstokes commented Sep 10, 2016

Language: PHP (v5.4 — deprecated use of /e modifier)
Length: 73
Solution:

function r($w){return preg_replace('/[a-z]/ei','chr(ord(\\0)+4^31)',$w);}

or 50 characters without the function() wrapping:

preg_replace('/[a-z]/ei','chr(ord(\\0)+4^31)',$w);

@achanda
Copy link

achanda commented Sep 26, 2016

Language: Python 2.7
Length: 51
Solution:

f=lambda s:`map(lambda x:chr(31^4+ord(x)),s)`[2::5]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment