All problems
LZW Encode
Problem
Compress a string with the LZW algorithm and return the list of emitted integer codes.
The dictionary starts with the 256 single-character strings mapped to their character codes, new phrases get the next integer codes starting at 256, and the final phrase is always emitted. Return an empty list for empty input.
Hint
Only emit a code when the current phrase cannot be extended by the next character.
Test cases
case 1
in: ["ababab"]
expected: [97,98,256,256]
case 2
in: ["aaaa"]
expected: [97,256,97]
case 3
in: [""]
expected: []
case 4
in: ["abcabcabc"]
expected: [97,98,99,256,258,257]
case 5
in: ["TOBEORNOTTOBEORTOBEORNOT"]
expected: [84,79,66,69,79,82,78,79,84,256,258,260,265,259,261,263]
Starter codePython
def lzw_encode(s):
# Your code here
pass5 test cases · run your code against them in the editor