def lzw_encode(seq): seq = list(seq) # make a copy seq.reverse( ) # For efficiency, we will reverse the list and get data from the end output = [] dictionary = {str(s): s for s in range(256)} i = 256 try: s = seq.pop() except IndexError: return int_seq_to_bin_seq(output, 255) # dictionary = [] # TODO: Set a default set of values for the dictionary while True: try: c = seq.pop() except IndexError: break if (s + c) in dictionary: s = s + c else: output.append(dictionary[s]) dictionary[s + c] = i i += 1 s = c if s in dictionary: output.append(dictionary[s]) else: output.append(i) return int_seq_to_bin_seq(output, len(dictionary))
def lzw_encode(seq): seq = list(seq) # make a copy seq.reverse() # For efficiency, we will reverse the list and get data from the end output = [] dictionary = {str(s) : s for s in range(256)} i = 256 try: s = seq.pop() except IndexError: return int_seq_to_bin_seq(output, 255) # dictionary = [] # TODO: Set a default set of values for the dictionary while True: try: c = seq.pop() except IndexError: break if (s + c) in dictionary: s = s + c else: output.append(dictionary[s]) dictionary[s + c] = i i += 1 s = c if s in dictionary: output.append(dictionary[s]) else: output.append(i) return int_seq_to_bin_seq(output, len(dictionary))
def encode( chan, opcode): if( opcode == ENCODING_SCHEME_VARLEN): return shanfan_encode(chan)[0] if( opcode == ENCODING_SCHEME_DICT): chan = [str(s) for s in chan] return lzw_encode(chan) else: return int_seq_to_bin_seq(chan, 255)