Last active
August 29, 2015 13:56
-
-
Save sidequestboy/9205174 to your computer and use it in GitHub Desktop.
Little script that takes user input, and on the fly prints the corresponding LZ78 codeword pairs
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
#!/usr/bin/env python3 | |
""" | |
LZ78 toy, reads characters from stdin and encodes on the fly. | |
""" | |
from sys import stdout | |
"""import getch, or define my own""" | |
try: | |
from msvcrt import getch | |
except ImportError: | |
"""Not on Windows, try Unix-like approach""" | |
def getch(): | |
import sys, tty, termios | |
fd = sys.stdin.fileno() | |
old_settings = termios.tcgetattr(fd) | |
try: | |
tty.setraw(fd) | |
ch = sys.stdin.read(1) | |
finally: | |
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) | |
return ch | |
ch = "" | |
current_str = "" | |
# s is a set of seen strings | |
s = {""} | |
# d is dict of seen strings to their index | |
d = {"": 0} | |
i = 1 | |
while True: | |
# loop until we see new pattern | |
while current_str in d: | |
# add another character | |
ch = getch() | |
current_str += ch | |
# new pattern | |
if ch not in ('', ''): | |
s.add(current_str) | |
d[current_str] = i | |
print("({}, {})".format(d[current_str[:-1]], ch), end='') | |
stdout.flush() | |
current_str = "" | |
i += 1 | |
else: | |
# terminate | |
if ch == '': | |
print("({}, EOF)".format(d[current_str[:-1]]), end='') | |
else: # ch == '' | |
print("({}, ^C)".format(d[current_str[:-1]]), end='') | |
print("") | |
exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
then type some characters, and watch the LZ78 codepairs be generated :D
Note:
end
kwarg to theprint
function, so use Python 3.