Created
February 11, 2014 08:11
-
-
Save noskill/8930944 to your computer and use it in GitHub Desktop.
generate strings in alphabetic order between two input strings
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
| 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