Skip to content

Instantly share code, notes, and snippets.

@chowells79
Created October 19, 2020 23:48
Show Gist options
  • Save chowells79/a1b7bbd2d58078e462fc9655d9ee08c6 to your computer and use it in GitHub Desktop.
Save chowells79/a1b7bbd2d58078e462fc9655d9ee08c6 to your computer and use it in GitHub Desktop.
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