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)
JavaScript oneliners for the win!
Had a feeling it was this simple.
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
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 :-)
Javascript
dart
I'm sure this could be done shorter.
In JS, implementing Bubble Sort just because.
Yet another Ruby one liner:
Also neat how it reads almost literarly, no need to explain each step in the chain!
in C++ using the standard template library:
Javascript solution for superSize :)
Haskell:
Scala
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...)