+ 33

[ ASSIGNMENT: ] Next smaller number with the same digits

TASK : Write a function that takes a positive integer and returns the next smaller positive integer containing the same digits. Input >> Output Examples :: nextSmaller(21) --> return 12 nextSmaller(531) --> return 513 nextSmaller(2071) --> return 2017 Return -1, when there is no smaller number that contains the same digits. Also return -1 when the next smaller number with the same digits would require the leading digit to be zero. nextSmaller(9) --> return -1 nextSmaller(111) --> return -1 nextSmaller(135) --> return -1 nextSmaller(1027) --> return -1 // 0721 is out since we don't write numbers with leading zeros NOTE: - Some tests will include very large numbers! - Test data only employs positive integers! HappyCodings!:) https://code.sololearn.com/Wx9Cg9Nq1zbu/?ref=app thisOne is for --> VcC !!:)

28th May 2018, 9:31 PM
Danijel Ivanović
Danijel Ivanović - avatar
10 Answers
29th May 2018, 11:39 AM
LukArToDo
LukArToDo - avatar
+ 10
https://code.sololearn.com/cah1W4u1OjQI/?ref=app
29th May 2018, 6:06 PM
Rstar
Rstar - avatar
+ 8
Python solution without permutations, commented: https://code.sololearn.com/ctxAG42cGmNU/#py
2nd Jun 2018, 10:09 AM
CĂ©pagrave
CĂ©pagrave - avatar
+ 8
https://code.sololearn.com/cR2gOPeHCOua/?ref=app
20th Jun 2018, 9:28 AM
Muhammad Hasan
Muhammad Hasan - avatar
+ 3
what's about playing with slices? it's reduce so much permutations https://code.sololearn.com/cOIs1rs25rBH/?ref=app Edit: I didn't notice the other assignment about next bigger, so I did another code to get both https://code.sololearn.com/c2WryQgXPPV4/?ref=app
30th May 2018, 7:13 AM
Francisco Casas
Francisco Casas - avatar
+ 3
https://code.sololearn.com/wLX5190Zs0bz/?ref=app https://code.sololearn.com/cWhBW83yu3eF/?ref=app
30th May 2018, 11:43 PM
Johann
Johann - avatar
+ 3
Ruby: Bigger & Smaller https://code.sololearn.com/c5rG4A5xT9uG/#rb quite new to oop en inheritance, so probably it could be optimized quite a lot
31st May 2018, 12:03 PM
bedawang
bedawang - avatar
10th Jul 2018, 9:53 AM
Salman
Salman - avatar
+ 1
I used permutations of itertools in python It works up to 10 digits, above it gives time or memory errors https://code.sololearn.com/ccDqe40cCEs4/#py
30th May 2018, 2:56 PM
Ellen Heuven
Ellen Heuven - avatar
31st May 2018, 3:01 PM
Ellen Heuven
Ellen Heuven - avatar