DEV Community

dev.to staff
dev.to staff

Posted on

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 (16)

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
exts profile image
Lamonte • Edited on

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
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
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
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
viktordimitrievski profile image
Viktor • Edited on

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
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
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑
def super_size(num):
  integers = [int(c) for c in str(num)]
  integers.sort(reverse=True)
  return ''.join([str(i) for i in integers])
Collapse
zoejm profile image
zoe-j-m • Edited on

Scala

def superSize = (_ : Int).toString.sorted.reverse.mkString
Collapse
abdellani profile image
Mohamed ABDELLANI • Edited on
def superSize(x)
 x.to_s.to_s.split("").sort{|x,y|-(x<=>y)}.join.to_i
end
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