Created
March 30, 2011 20:19
-
-
Save Themaister/895213 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
def radix(arr): | |
# Finds max string length to calculate indexes to work on | |
max_len = 0 | |
for x in arr: | |
if len(x) > max_len: | |
max_len = len(x) | |
work_list = arr | |
# Works on indexes in reverse | |
for index in range(max_len, -1, -1): | |
# Set up one extra radix index to include the "0" character, for strings that are shorter than the longest, so we can sort accordingly | |
radix_list = [[] for x in range(0,27)] # Set up emtpy list | |
for x in work_list: | |
# We have a short string, or unknown character, push to 0 char spot | |
if index >= len(x) or not x[-1].isalpha(): | |
radix_list[0].append(x) | |
else: | |
radix_list[ord(x[index]) - ord('a') + 1 if x[index].islower() else ord(x[index]) - ord('A') + 1].append(x) | |
work_list = [] # Reset our work list | |
for x in radix_list: | |
work_list.extend(x) | |
return work_list | |
elems = ["rat", "mop", "cat", "map", "car", "top", "cot", "tar", "rap"] | |
elems = radix(elems) | |
print elems |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment