Smallest number by rearranging digits of a given number - Spicy Coders

Recent

Wednesday, February 07, 2018

Smallest number by rearranging digits of a given number

Find the Smallest number (Not leading Zeros) which can be obtained by rearranging the digits of given number.
Sample Examples:
Input:
846903

Output:
304689

Input:
55010
Output:
10055

Source Code:

from collections import Counter
s=''.join(sorted(input()))
x=s.rfind('0')         //find the last index
c=Counter(s)       //get the frequency 
y=list(c.values())[1]    //get the [1]particular index value
print(s[x+1:(y+x)+1],s[0:x+1],s[(x+y)+1:],sep="")


No comments:

Post a Comment