Skip to content

Instantly share code, notes, and snippets.

@Arafat245
Created September 16, 2016 10:46
Show Gist options
  • Save Arafat245/c80ec9d8ca674c657f101347d8659ca2 to your computer and use it in GitHub Desktop.
Save Arafat245/c80ec9d8ca674c657f101347d8659ca2 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]
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 = {}
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