Help with code please. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

Help with code please.

I am asked to build a function that returns string in numerical order in range 1-9. My code isn’t working. Any suggestions are greatly appreciated. https://code.sololearn.com/cZvP6se13gw7/?ref=app

16th Jan 2021, 3:11 PM
Javier
Javier  - avatar
9 Answers
+ 20
hmmmm here is my idea. It sorts the words by that char, which is in a set of the numbers. makes the story a bit shorter. nice challenge by the way. https://code.sololearn.com/c8Ybj6E3x2sR/?ref=app
16th Jan 2021, 4:03 PM
Oma Falk
Oma Falk - avatar
+ 17
print(' '.join(sorted(s.split(), key=sorted))) https://code.sololearn.com/cC01C3XizgIu/?ref=app
16th Jan 2021, 4:53 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 13
《 Nicko12 》 , Frogged , RKK ,Vitaly Sokol , all of you with brilliant ideas and a great code. 👍👍👍👍 and BTW a real nice challenge!
16th Jan 2021, 5:59 PM
Lothar
Lothar - avatar
+ 12
First, the reason why the if-elif-else statements wont work is because even though the list is still empty, the program will still insert an element to N index. We insert 3 so the list will now look like this: [3] Next elements = 7, 1, 6, 4, 5: [3, 7] [1, 3, 7] [1, 3, 7, 6] #Since 6 means insert to 5th index it will append instead of inserting between 4 and 7. [1, 3, 7, 4, 6] #Then the 4 will insert to 3rd index. And the sequence will insert randomly and so on... - - - - - - - -- - - - - - - - - - - - So I thought of doing and sorting this using Dictionary. (see the code for further explanation) https://code.sololearn.com/cA61a1A9A0a1
16th Jan 2021, 4:02 PM
noteve
noteve - avatar
+ 11
lil' hack added to your solution Franc. https://code.sololearn.com/cRZ9L6nRijcj/?ref=app
16th Jan 2021, 4:11 PM
Rohit
+ 8
I added te1n0 and e1even1 to make it more interesting. https://code.sololearn.com/c76y9DOuisbF/?ref=app
19th Jan 2021, 9:27 PM
Louis
Louis - avatar
+ 6
Louis , great code 👍👍👍👍
20th Jan 2021, 11:54 AM
Lothar
Lothar - avatar
17th Jan 2021, 2:29 PM
RuntimeERROR
RuntimeERROR - avatar
+ 2
See this Francisco Javier https://code.sololearn.com/cdGAVLNwuxnN/?ref=app taking in account also no numbers and multiple numbers in each word, not only 1...9
21st Jan 2021, 4:33 AM
David Ordás
David Ordás - avatar