I'm trying to write a function that sorts a list of ints and letters in acending order (sort using unicode (ord())), using recursion with no loops. I am having trouble and I'm a little stuck. I tried using the code that was answered a while ago:
https://stackoverflow.com/questions/6092...-loop-in-p
but that one is using a
this is what i wrote til now but im getting into an infinite recurse, help please..
i will get this output:
for the code i posted i dont seem to notice the problem that i have, i am a begginer and ill be happy to learn what is my problem.
thank you very much for the helpers and the work is much appericated.
**
i would like to note that the code works for an input such as: 54321 ---> output: 12345
**
https://stackoverflow.com/questions/6092...-loop-in-p
but that one is using a
for
loop and works only for ints.this is what i wrote til now but im getting into an infinite recurse, help please..
def sortlist(lst,n,i): if (i == n-1): if((ord(lst[0]) >= ord(lst[i])) and i>=1): lst.insert(0,lst[i]) del lst[i+1] i=0 return [lst[0]] + [sortlist(lst[1:],n-1,i+1)] if n>1: if((ord(lst[0]) >= ord(lst[i])) and i>=1): lst.insert(0,lst[i]) del lst[i+1] sortlist(lst,n,i+1) else: return lst[0] lst = list(input()) n = len(lst) i = 0 sortlist(lst,n,i) print("".join(lst))for this input example:
kd465b21
i will get this output:
Output:124dk65b
instead of :Output:12456bdk
- (right unicode order)for the code i posted i dont seem to notice the problem that i have, i am a begginer and ill be happy to learn what is my problem.
thank you very much for the helpers and the work is much appericated.
**
i would like to note that the code works for an input such as: 54321 ---> output: 12345
**