Python Forum
sorting a list using unicodes acending order, no loops, no sort(), using recursion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sorting a list using unicodes acending order, no loops, no sort(), using recursion
#1
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 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
**
Reply


Messages In This Thread
sorting a list using unicodes acending order, no loops, no sort(), using recursion - by lrn2codee - Jun-20-2021, 06:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to sort a list with duplicates and return their unique indices. Echoroom 3 3,640 Sep-23-2022, 07:53 AM
Last Post: deanhystad
  Count occurences in a string and add to a list using loops Leyo 4 1,766 Mar-11-2022, 03:52 PM
Last Post: Leyo
  Sorting list - Homework assigment ranbarr 1 2,288 May-16-2021, 04:45 PM
Last Post: Yoriz
  Input validation for nested dict and sorting list of tuples ranbarr 3 3,985 May-14-2021, 07:14 AM
Last Post: perfringo
Question Recursion and permutations: print all permutations filling a list of length N SantiagoPB 6 3,421 Apr-09-2021, 12:36 PM
Last Post: GOTO10
  how to sort a list without .sort() function letmecode 3 3,524 Dec-28-2020, 11:21 PM
Last Post: perfringo
  GCF function w recursion and helper function(how do i fix this Recursion Error) hhydration 3 2,599 Oct-05-2020, 07:47 PM
Last Post: deanhystad
  Sorting nested lists in ascending order jszum 2 2,329 May-17-2020, 01:35 PM
Last Post: jefsummers
  Appending to a list in the right order Noobstudent 2 2,368 Dec-07-2019, 10:39 PM
Last Post: Noobstudent
  Question about Sorting a List with Negative and Positive Numbers Than999 2 12,854 Nov-14-2019, 02:44 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020