Created
August 14, 2011 04:08
-
-
Save basicxman/1144561 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def resolve_key(key) | |
| bucket = key[0] | |
| return key if @item_buckets[bucket].include? key | |
| shortest_diff = nil | |
| shortest_key = nil | |
| @item_buckets[bucket].each do |test_key| | |
| if test_key.length > key.length | |
| diff = test_key.length - key.length if test_key.index(key) | |
| else | |
| diff = key.length - test_key.length if key.index(test_key) | |
| end | |
| next if diff.nil? | |
| if shortest_diff.nil? or diff < shortest_diff | |
| shortest_key = key | |
| shortest_diff = diff | |
| end | |
| end | |
| return shortest_key | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment