0

I'm tasked with generating a few unsorted keys that will then be sorted alphabetically for example

["dba321db32d1", "abc123ab12a1". "abd456ab45a4"] will then be output as ["abc123ab12a1", "abd456ab45a4", "dba321db32d1"]. I understand the concept of bucket sort with numbers, but for the life of me I can't find anything on how to use it with strings I only get results for programs like Java and C or to use radix sort. This is what I ahve so far but can't figure out how to get it to sort.

def binsort(a):
  bins = []
  for l in range (a, -1,-1,-1):
    binsTwo = [[] for _ in range(10)]
    for bin in bins:
       for e in bin:
         binsTwo[e[l]].append(e)
    bins = binsTwo
 return [e for bin in bins for e in bin]

a = []
print(a.append(gen_key))

I even tried this but still can't get it to sort

def binsort(a):
  bins = []
  for l in range (a, -1,-1,-1):
    binsTwo = ''.join(choice(a) for _ in range(l))
    for bin in bins:
        for e in bin:
           binsTwo[e[l]].append(e)
    bins = binsTwo
  return [e for bin in bins for e in bin]

 a = []
 print(a.append(gen_key))

gen_key is part of the code that I am using to generate the strings. The reason I added the gen_key part to this defintion is because I was hoping that it would output the strings sorted.

3
  • You can convert a letter to a number by using the function ord Commented Oct 17, 2021 at 20:02
  • @DaniMesejo so to do that, would I add another line for it or add it into what I have now? Commented Oct 17, 2021 at 20:26
  • Ok so I aded ord() however it says it expected a character but found a string of length x Commented Oct 17, 2021 at 22:38

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.