# Daily Challenge #177 - Supersize Me

### Setup

Implement a function that rearranges an integer into its largest possible value. If the integer is already in its maximum possible value, simply return it.

### Examples

superSize(123456) //654321
superSize(105) //510

### Tests

superSize(513)
superSize(2020)
superSize(4194)
superSize(608719)
superSize(123456789)
superSize(700000000001)
superSize(666666)

Good luck!

This challenge comes from ozymandias1 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

### Discussion JavaScript oneliners for the win!

const superSize = n => +n.toString().split('').sort((a, b) => b - a).join('')


Why the plus though?

The argument to the unary plus operator is a string. The + is a very concise method of converting a string to a number, in this situation.

Ah, I totes prefer the readability over conciseness approach

dart

int superSize(int number) {
var data = number.toString();
var dataInt = List<int>();
for(var n = 0; n < data.length; n++) {
var val = int.tryParse(data[n]);
if(val != null) {
}
}
dataInt.sort((a, b) => b.compareTo(a));
return int.tryParse(dataInt.join());
}


I'm sure this could be done shorter.

Javascript

const superSize = n => {
const nArr  = Array.from(n.toString()).map(Number);

const nArrSorted = nArr.sort((a, b) => b - a);

return parseInt(nArrSorted.join(''));
};


emacs lisp is shorter than common lisp (no need to "extract" the first value of parse-integer)

(defun superSize (n) (string-to-number (seq-sort #'> (number-to-string n))))

and has as few parens as @savagepixie 's JS solution :-)

in C++ using the standard template library:

typedef unsigned long long ull;
ull superSize(ull x) {
vector<short> d;
while(x > 0) {
d.push_back(x%10);
x /= 10;
}
sort(d.begin(), d.end(), greater<short>());
ull r = 0;
for(auto i: d)
r = r*10 + i;

return r;
}


In JS, implementing Bubble Sort just because.

function superSize(num) {
let str = [...num.toString()];
let swap;
do {
swap = false
for (i = 0; i <= str.length; i++) {
if (str[i] < str[i + 1]) {
const bigger = str[i + 1];
str[i + 1] = str[i];
str[i] = bigger;
swap = true
}
}
} while (swap);
return(+str.join(''));
};


Javascript solution for superSize :)

function superSize(number){
let revNum = "";
let tempNum = number;
while(number != 0){
revNum = ${revNum}${(number % 10)};
number = parseInt(number / 10);
}
revNum = parseInt(revNum);
return revNum > tempNum ? revNum : tempNum;
}


Yet another Ruby one liner:

def superSize int
int.to_s.chars.sort.reverse!.join.to_i
end


Also neat how it reads almost literarly, no need to explain each step in the chain!

import Data.Char (digitToInt)
import Data.List (sortBy)
supersize :: Int -> Int
supersize = read . sortBy (flip compare) . map digitToInt . show


def superSize(x)
x.to_s.to_s.split("").sort{|x,y|-(x<=>y)}.join.to_i
end


Scala

def superSize = (_ : Int).toString.sorted.reverse.mkString


def super_size(num):
integers = [int(c) for c in str(num)]
integers.sort(reverse=True)
return ''.join([str(i) for i in integers])


common lisp

(defun superSize (n) (car (list (parse-integer (sort (write-to-string n) #'char>)))))

parse-integer outputs 2 values but we only need the first, hence the hack with (car (list...)  