Skip to content

Instantly share code, notes, and snippets.

@Arafat245
Created September 16, 2016 14:43
Show Gist options
  • Save Arafat245/5c5b0d0c0ded4db99f640f9b67e0c6ee to your computer and use it in GitHub Desktop.
Save Arafat245/5c5b0d0c0ded4db99f640f9b67e0c6ee to your computer and use it in GitHub Desktop.
def maxF(L):
maxlength = 0
for k, v in L.items():
l = len(v)
if l > maxlength:
maxlength = l
return maxlength
a = [1546, 89, 989, 1131, 289, 565, 10, 2, 54791]
L = {i:str(a[i]) for i in range(len(a))}
m = maxF(L)
for k, v in L.items():
v = (m - len(v)) * '0' + v
L[k] = v
for x in range(m-1,-1,-1): #iterating through all the digits
bucket = {key:[] for key in range(10)}
# for key in range(10):
# bucket.setdefault(key, [])
for k, v in L.items():
key = int(v[x])
bucket[key].append(v) #appending values in bucket using key as their digit
i = 0
for k, v in bucket.items():
for e in v:
L[i] = e #getting values back to list from bucket
i += 1
print(bucket)
print("Sorted list: ")
for k, v in L.items():
print(str(v) + " ") #printing sorted list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment