Download code

Jump to: navigation, search

Back to Lossless_compression_(Python)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

build.log

1 SyntaxError: ('invalid syntax', ('/tmp/litprog5052369/lz78.py', 19, 59, "\t            for w in [dodict(d,tok)] if not w] + [(w,'')]@ text\n"))
2 


lz78.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Lossless_compression_(Python)?oldid=16450
14 
15 def encode(cs):
16 	d,w = {0:''}, 0
17 	dodict = lambda d,k: d.get(k) or d.__setitem__(k,len(d)) or 0
18 	return [tok for c in cs for tok in [(w,c)]
19 	            for w in [dodict(d,tok)] if not w] + [(w,'')]@ text
20 
21 def decode(ts):
22 	d,j = {0:''}, ''.join
23 	dodict = lambda d,v: d.__setitem__(len(d),v) or v
24 	return j([dodict(d,d[w]+c) for (w,c) in ts])
25 if __name__ == '__main__':
26 	test = "wow! how now brown plow cow"
27 	print encode(test)
28 	print decode(encode(test))


hijacker
hijacker
hijacker
hijacker