DEV Community

Daily Challenge #177 - Supersize Me

dev.to staff on January 31, 2020

Setup Implement a function that rearranges an integer into its largest possible value. If the integer is already in its maximum possible...
Collapse
 
savagepixie profile image
SavagePixie •

JavaScript oneliners for the win!

const superSize = n => +n.toString().split('').sort((a, b) => b - a).join('')
Collapse
 
georgewl profile image
George WL •

Had a feeling it was this simple.

Why the plus though?

Collapse
 
craigmc08 profile image
Craig McIlwrath •

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.

Thread Thread
 
georgewl profile image
George WL •

Ah, I totes prefer the readability over conciseness approach

Collapse
 
jehielmartinez profile image
Jehiel Martinez •

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(''));
};
Collapse
 
dimitrimarion profile image
Dimitri Marion •

Javascript

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

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

    return parseInt(nArrSorted.join(''));
};
Collapse
 
exts profile image
Lamonte • • Edited

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.add(val);
    }
  }
  dataInt.sort((a, b) => b.compareTo(a));
  return int.tryParse(dataInt.join());
}

I'm sure this could be done shorter.

Collapse
 
cipharius profile image
Valts Liepiņš •

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!

Collapse
 
viktordimitrievski profile image
Viktor • • Edited

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;
}
Collapse
 
kruzzy profile image
Andrei Visoiu •

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;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
abdellani profile image
Mohamed ABDELLANI • • Edited
def superSize(x)
 x.to_s.to_s.split("").sort{|x,y|-(x<=>y)}.join.to_i
end
Collapse
 
zoejm profile image
zoe-j-m • • Edited

Scala

def superSize = (_ : Int).toString.sorted.reverse.mkString
Collapse
 
craigmc08 profile image
Craig McIlwrath •

Haskell:

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