Created
July 2, 2011 18:01
-
-
Save serialhex/1061466 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
/* ok, heres what i'm trying to do... | |
* i'm trying to group all the words of equal length together, | |
* and then group them all by the first letter they start with | |
* so i can do something like: | |
* - for all five-letter d-words | |
* foo := words[5]["d"] | |
* i know my nested structures are kind of wonky, but i'm not sure how else to represent it... | |
* any help is welcome! | |
*/ | |
// this works with the awesomeness!!! | |
func wordSort(dict []string) (map[int] map[uint8] []string) { | |
words := make(map[int] map[uint8] []string) | |
for _, str := range dict { | |
if words[len(str)] == nil { | |
words[len(str)] = make(map[uint8] []string) | |
} | |
words[len(str)][str[0]] = append(words[len(str)][str[0]], str) | |
} | |
return words | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here are two versions of the same deal. First one is the same map approach you use. The second one uses slices of integers. First level holds string length's. Second level holds the first character. The last level holds an index into the dict slice instead of the actual word.
The slice version is consistently 4 times faster than the map version. I am not entirely sure about the memory usage though, because the slice version creates a lot of empty array slots.