Skip to content

Instantly share code, notes, and snippets.

@noskill
Created February 11, 2014 08:11
Show Gist options
  • Select an option

  • Save noskill/8930944 to your computer and use it in GitHub Desktop.

Select an option

Save noskill/8930944 to your computer and use it in GitHub Desktop.
generate strings in alphabetic order between two input strings
import sys
def next_word(start, current, end, diff_index):
for i in range(len(current) - 1, diff_index - 1, -1):
if current[i] != end[i]:
if i < len(start) - 1:
return current[:i] + chr(ord(current[i]) + 1) + start[i]
else:
return current[:i] + chr(ord(current[i]) + 1) + current[i+1:]
def get_diff_index(a, b):
diff_index = 0
for i in range(len(a), len(b)):
if a[i] == b[i]:
diff_index = i + 1
return diff_index
def word_loop(start, end):
print (start)
result = start
diff_index = get_diff_index(start, end)
while result is not None:
result = next_word(start, result, end, diff_index)
if result:
print (result)
def main():
start, end = sys.argv[1:]
if len(start) == len(end):
if end < start:
start, end = end, start
word_loop(start, end)
else:
start, end = (start,end) if start < end else (end, start)
word_loop('a' * len(start), start)
word_loop(end, 'z' * len(end))
if __name__ == '__main__':
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment