[Challenge] numbers manipulation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 :)

13th Jul 2017, 12:44 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
24 Answers
13th Jul 2017, 12:48 PM
Pixie
Pixie - avatar
13th Jul 2017, 1:06 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 12
@yuri he wants answer without collections like arrays, sets etc.
13th Jul 2017, 2:25 PM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 11
@Baptiste : Slightly modified it.
13th Jul 2017, 1:18 PM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
+ 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--; } } } }
13th Jul 2017, 2:16 PM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 7
It is a great start for a Kaprekar number procedure, my first project here on Sololearn: https://code.sololearn.com/cRqDeh0LFCLy/?ref=app
13th Jul 2017, 8:01 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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)
13th Jul 2017, 2:42 PM
Vaibhav Sharma
Vaibhav Sharma - avatar
+ 5
Yeah. I just wanted to post it before anyone else 😂 Wrote what first came to find
13th Jul 2017, 12:50 PM
Pixie
Pixie - avatar
+ 5
Added another method. Could be faster than the first one since inbuilt sort function takes up more time.
13th Jul 2017, 1:29 PM
Pixie
Pixie - avatar
+ 4
#Ruby def smallest(num) num.to_s.chars.sort.join.to_i end puts smallest(2017) #Output: 127 puts smallest(1996) #Output: 1699
13th Jul 2017, 2:35 PM
SC Lee
SC Lee - avatar
+ 3
Nice @Krishina but ... come on ... You can do more optimized than that, I am sure of it ! :) Same for you Pixie ;)
13th Jul 2017, 1:11 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 3
It's actually easy
13th Jul 2017, 1:30 PM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 3
https://code.sololearn.com/cuQHrC6GjCN9/?ref=app Just made a more optimised solution in Ruby 😄
13th Jul 2017, 4:38 PM
Pixie
Pixie - avatar
+ 2
@Pixie easy but not optimized at all :p
13th Jul 2017, 12:48 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
I can understand, my first solution was the same 😂
13th Jul 2017, 12:51 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 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
13th Jul 2017, 4:40 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
Here you go. Checks for valid input. https://code.sololearn.com/cDNc81dlY1S5/?ref=app
18th Sep 2017, 10:19 AM
Vari93
Vari93 - avatar
+ 1
You can do it only with number manipulation, not array :) But still, I think you both have the shortest solutions in your languages ^^
13th Jul 2017, 1:23 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
@$Vengat, then show it to us :p
13th Jul 2017, 2:04 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 1
Hi, for very long numbers the below should be faster. https://code.sololearn.com/cKKmT395hhef/?ref=app
13th Jul 2017, 2:20 PM
yuri