LINUX.ORG.RU

История изменений

Исправление lucentcode, (текущая версия) :

Развил немного вашу идею:

#!/usr/bin/python3

from itertools import groupby
import re

def compress(data):
    for k, g in groupby(data):
        yield len(list(g)), k
        
def decompress(data):
  m = re.findall('([0-9])([a-zA-Z])', data)
  return(''.join([int(d) * c for d, c in m]))

def test():
    result = compress('aaabbcdddd')
    compressed = ''.join('{}{}'.format(*r) for r in result)
    print(compressed)
    decompressed = decompress(compressed)
    print(decompressed)
    
test()
Может AleksJen сможет его как-то приспособить для своей лабораторки...

Исправление lucentcode, :

Развил немного вашу идею:

#!/usr/bin/python3

from itertools import groupby
import re

def compress(data):
    for k, g in groupby(data):
        yield len(list(g)), k
        
def decompress(data):
  m = re.findall('([0-9])([a-zA-Z])', data)
  return(''.join([int(d) * c for d, c in m]))

def test():
    result = compress('aaabbcdddd')
    compressed = ''.join('{}{}'.format(*r) for r in result)
    print(compressed)
    decompressed = decompress(output)
    print(decompressed)
    
test()
Может AleksJen сможет его как-то приспособить для своей лабораторки...

Исходная версия lucentcode, :

Развил немного вашу идею:

#!/usr/bin/python3

from itertools import groupby
import re

def compress(data):
    for k, g in groupby(data):
        yield len(list(g)), k
        
def decompress(data):
  m = re.findall('([0-9])([a-zA-Z])', data)
  return(''.join([int(d) * c for d, c in m]))

def test():
    result = compress('aaabbcdddd')
    compressed = ''.join('{}{}'.format(*r) for r in result)
    print(compressed)
    decompressed = decompress(output)
    print(decompressed)
    
test()
Может AleksJen сможет его как-то приспособить для своей лабораторки...