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

@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