[Challenge] numbers manipulation | SoloLearn: Learn to code for FREE!

+5

[Challenge] numbers manipulation

The challenge is to create a function which will take one INTEGER parameter (greater than 0). It will return the integer with its figures sorted. Example : 2017 -> 127 1996 -> 1699 The more optimized, the better :)

7/13/2017 12:44:54 PM

Baptiste E. Prunier

24 Answers

New Answer

+7

https://code.sololearn.com/c5ftD2L8Yz6U/?ref=app Rather easy in python. Haha 😅

+12

https://code.sololearn.com/W87Feb0YCo3f/?ref=app

+12

@yuri he wants answer without collections like arrays, sets etc.

+11

@Baptiste : Slightly modified it.

+11

//without array or built in functions or collections import java.util.Scanner; public class Program { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int number = sc.nextInt(); for(int i=1;i<10;i++){ int counter=0, num=number; while(num>0){ if(num % 10 == i) counter++; num/=10; } while(counter>0){ System.out.print(i); counter--; } } } }

+7

It is a great start for a Kaprekar number procedure, my first project here on Sololearn: https://code.sololearn.com/cRqDeh0LFCLy/?ref=app

+6

Actually Baptiste algorithm works and number you choose has datatype long for java so please check the algo only and yes this algo is not language dependent except datatype range (this algo will work same for C ++, java, C# or JavaScript)

+5

Yeah. I just wanted to post it before anyone else 😂 Wrote what first came to find

+5

Added another method. Could be faster than the first one since inbuilt sort function takes up more time.

+3

Nice @Krishina but ... come on ... You can do more optimized than that, I am sure of it ! :) Same for you Pixie ;)

+3

It's actually easy

+3

https://code.sololearn.com/cuQHrC6GjCN9/?ref=app Just made a more optimised solution in Ruby 😄

+2

@Pixie easy but not optimized at all :p

+2

I can understand, my first solution was the same 😂

+2

#Ruby def smallest(num) num.to_s.chars.sort.join.to_i end puts smallest(2017) #Output: 127 puts smallest(1996) #Output: 1699

+2

I marked the quickest one as Best as nobody had a solution without containers. Congratulations to all who found an answer to this problem ! :) My answer was : https://code.sololearn.com/coTvuhU2EXZU/?ref=app

+2

Here you go. Checks for valid input. https://code.sololearn.com/cDNc81dlY1S5/?ref=app

+1

You can do it only with number manipulation, not array :) But still, I think you both have the shortest solutions in your languages ^^

+1

@$Vengat, then show it to us :p

+1

Hi, for very long numbers the below should be faster. https://code.sololearn.com/cKKmT395hhef/?ref=app