Created
October 19, 2020 23:48
-
-
Save chowells79/a1b7bbd2d58078e462fc9655d9ee08c6 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
def suffixes(string): | |
return [ string[i:] for i in range(len(string)) ] | |
def common_prefix(string1, string2): | |
for i, (c1, c2) in enumerate(zip(string1, string2)): | |
if c1 != c2: | |
return i | |
return min(len(string1), len(string2)) | |
def sublist_rank(target): | |
running_total = 0 | |
for suffix in sorted(suffixes(target)): | |
if suffix <= target: | |
running_total += len(suffix) | |
else: | |
prefix_len = common_prefix(suffix, target) | |
if prefix_len == 0: | |
break | |
running_total += prefix_len | |
return running_total |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment