Skip to content

Instantly share code, notes, and snippets.

@CodeGolfScotland
Last active January 25, 2017 09:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeGolfScotland/4f930b87e84b80326c8ec5266c526945 to your computer and use it in GitHub Desktop.
Save CodeGolfScotland/4f930b87e84b80326c8ec5266c526945 to your computer and use it in GitHub Desktop.
Code Golf August 2016

August 2016 - Rotate to the max

Task

Given a number, return the maximum value by rearranging it's digits.

Examples:

(123) => 321 (786) => 876 ("001") => 100 (999) => 999 (10543) => 54310

^Note the number may be given as a string

About Code Golf Scotland

@martincarlin87
Copy link

martincarlin87 commented Aug 2, 2016

Language: Ruby
Length: 72
Solution:

def x(y) y.to_s.split('').map{|n| n.to_i}.sort.reverse.join('').to_i end

@rawkode
Copy link

rawkode commented Aug 2, 2016

Language: Elixir
Length: 59
Solution:

s=fn(x)->String.to_charlist(x)|>Enum.sort|>Enum.reverse end

@robdick
Copy link

robdick commented Aug 2, 2016

Language: Javascript
Length: 49
Solution:

let f=(s)=>s.split('').sort((a,b)=>b-a).join('');

@cgwyllie
Copy link

cgwyllie commented Aug 2, 2016

Language: JavaScript
Length: 58
Solution:

let a = (n) => +(''+n).split('').sort((a,b)=>b-a).join('')

@sunscreem
Copy link

sunscreem commented Aug 2, 2016

Language: PHP
Length: 42
Solution:

$n=str_split($x);rsort($n);$y=join($n,'');

Copy link

ghost commented Aug 2, 2016

Language: JavaScript
Length: 38
Solution:

n=>[...n+''].sort((a,b)=>b-a).join('')

@f-marais
Copy link

f-marais commented Aug 5, 2016

Language: Perl6
Length: 36
Solution:

sub a($a){$a.comb.sort.reverse.join}

@joaquinferrero
Copy link

joaquinferrero commented Aug 5, 2016

Language: Perl 5
Length: 40
Solution:

sub a{join'',reverse sort split//,shift}

@CraigMorton
Copy link

Language: JavaScript
Length: 39
Solution:

s=>+[...s+""].sort((a,b)=>b-a).join("")
(stringOrInt) => {
  let definitelyString = stringOrInt+"";
  let array = [...definitelyString];
  let sorted = array.sort((a,b)=>b-a);
  let string = sorted.join("");
  let integerResult = +string;
  return integerResult;
}

@achanda
Copy link

achanda commented Aug 25, 2016

Language: Python
Length: 45
Solution

def x(y):return ''.join(sorted(str(y))[::-1])

Length: 42

def x(y):return ''.join(sorted(`y`)[::-1])

Length: 40

def x(y):return`sorted(`y`)[::-1]`[2::5]

Copy link

ghost commented Aug 26, 2016

I'm not taking credit for this as it is a direct copy of @achanda 's above attempt. Converting the function def to lambda reduces it to 36 chars.

Language: Python
Length: 36
Solution:

x=lambda y:`sorted(`y`)[::-1]`[2::5]

@joaquinferrero
Copy link

Language: Perl 6
Length: 33
Solution:

sub r{$^a.comb.sort.reverse.join}

Demo:

perl6 -e 'say "$_ : {a($_)}" for 123, 786, "001", 999, 10543; sub a($a){$a.comb.sort.reverse.join}'
123 : 321
786 : 876
001 : 100
999 : 999
10543 : 54310

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