0

Smallest Permutation

Given a number, find the permutation with the smallest absolute value (no leading zeros). -20 => -20 -32 => -23 0 => 0 10 => 10 29394 => 23499 The input will always be an integer. Here is my code: def min_permutation(n): digits = list(str(n)) result = sorted(digits) e = ''.join(result) sum = len(str(n)) if n == 0: return 0 elif sum == 2 and '0' in str(n): return n else: return int(e) The test cases are correct but when I press submit half of the test cases are right and the other half is wrong. I want my code to output -20 if n is a 2 digit number and has a 0 in it we return n... Please explain where I'm wrong https://www.codewars.com/kata/5fefee21b64cc2000dbfa875/train/JUMP_LINK__&&__python__&&__JUMP_LINK Please explain where I'm wrong! Or where I have to add something into my code.

4th Jun 2021, 3:13 AM
Ailana
Ailana - avatar
14 ответов
+ 1
visph How ' getting/providing help ' is against rules? Op provided attempt and asking help ,not code..! Link is not opening in my browser so I did not know about it.. Am trying to help to solve it only, with explanation, not only with code..
4th Jun 2021, 5:47 PM
Jayakrishna 🇮🇳
0
getting/providing help for solving problem of any platforms (including sololearn) is against sololearn rules... getting help to solve codewars kata is against codewars rules (cheating) Ailana please kindly delete this thread before moderators find it and delete it for you ;P
4th Jun 2021, 4:09 PM
visph
visph - avatar
0
Jayakrishna🇮🇳 it's against rules of sololearn to help to solve or give solution of either code coach problem or any other platform problem... codewars also state clearly that you must be solve kata (problem) by yourself and not share solutions...
4th Jun 2021, 6:01 PM
visph
visph - avatar
0
visph You can find many questions about code coach help in search. But I never see a warn from mod. Even mods answered . But we should not "post solutions and any pro info". Yes must solve yourself but sometimes it takes to find many hours for even simple solution. Better to get help . If not share attempt then how can find problem , or how to trust that op tried, not homework. I just want to try to help to get op identify problem and find solution by own
4th Jun 2021, 6:23 PM
Jayakrishna 🇮🇳
0
Jayakrishna🇮🇳 I recently seeing a mod warning and deleting a code coach help... and mentioning that rule without specifying 'pro' or not... maybe you're right: for sololearn code coach that's only for 'pro' ones... as I initially though ^^ anyway, here we are in the obvious case that's an another platform challenge, and sololearn doesn't allow giving help nor solution in such case ;P (all the more that the platform doesn't allow to be helped, nor got solutions from others)
4th Jun 2021, 6:28 PM
visph
visph - avatar
0
Ailana I think you need to know more about functions ... I can't see the format of code wars here.. so what you are trying and what I mentioning is not matching.. My suggestion is "try here in playground to solve it and then apply in codewars in suitable format . You can debug here with input sample and know easily what problem or error you getting very easily.. so try it here in SL playground... " The error may be you are not passing any argument to calling function..
5th Jun 2021, 9:16 AM
Jayakrishna 🇮🇳
- 1
You are taking length of str(n) into sum, which is 3 ,not 2 for -20 ..
4th Jun 2021, 7:44 AM
Jayakrishna 🇮🇳
- 1
Here is my new code: def min_permutation(n): digits = list(str(n)) result = sorted(digits) e = ''.join(result) s = len(str(n)) if s < 0 and s == 3: return n if n == 0: return 0 elif '0' in str(n) and s == 2: return n else: return int(e) AND it still doesn't work :< PLZ help
4th Jun 2021, 3:17 PM
Ailana
Ailana - avatar
- 1
For input -20 , len(str(n) return 3 ( -,2,0 => 3 characters). (yes your solution works for this) Why you are considering this input sample..? What if input is -2031 ? Output is " -2031 or -2013 ? I think if input have 0,then count no.of 0 and use return int(e)*10**(n.count('0')) Else s = len(str(abs(n))) I think question has fault that for input -32 => small value is -32 , not -23. (For negative numbers, if sign doesn't matter then use absolute value by abs(n).
4th Jun 2021, 3:41 PM
Jayakrishna 🇮🇳
- 1
My new code... def min_permutation(n): digits = list(str(n)) result = sorted(digits) e = ''.join(result) s = len(str(n)) if n < 0 and n != abs(n) and s == 3 and '0' in str(n): return n if n == 0: return 0 elif '0' in str(n) and s == 2: return n else: return int(e)
4th Jun 2021, 3:42 PM
Ailana
Ailana - avatar
- 1
Is your code not have calling statement for function ? Add that also... Ailana just try this : if input have '0' ('0' in str(n)), then return same int(n) else return int(e). comment 2nd if.
4th Jun 2021, 3:45 PM
Jayakrishna 🇮🇳
- 1
def min_permutation(n): digits = list(str(n)) result = sorted(digits) e = ''.join(result) s = len(str(n)) if n == 0: return 0 elif '0' in str(n): return n else: return int(e) Still doesn't work :,<
4th Jun 2021, 3:51 PM
Ailana
Ailana - avatar
- 1
def min_permutation(n): digits = list(str(n)) result = sorted(digits) e = ''.join(result) s = len(str(n)) if n == 0: return 0 elif '0' in str(n): return n else: return int(e) min_permutation(int(input())) #why you are not adding this line? try this Ailana
4th Jun 2021, 3:55 PM
Jayakrishna 🇮🇳
- 1
I've written the line min_premution()) and It gave me an error...
4th Jun 2021, 6:55 PM
Ailana
Ailana - avatar