DEV Community

loading...

Daily Challenge #177 - Supersize Me

thepracticaldev profile image dev.to staff ・1 min read

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)

pic
Editor guide
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 W Langham

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 W Langham

Ah, I totes prefer the readability over conciseness approach

Collapse
brandelune profile image
Jean-Christophe Helary

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 :-)

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
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
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
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

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
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
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
Collapse
zoejm profile image
zoe-j-m • Edited

Scala

def superSize = (_ : Int).toString.sorted.reverse.mkString
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
brandelune profile image
Jean-Christophe Helary

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...)