I have been trying to make a compression system, but my strings keep on changing from length 1: turns to length 3 length 2: turns to length 5 length 3: turns to length 7 length 4: turns to length 9 and so on. The code is below:
text = list(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYSZ#1234567890-_!@.+=$%:;^&*(){}[]\\|'\",<>/?`~\n\t "
)
import random
import pickle as pic
import itertools
import tools
class A:
def __init__(self):
self.wmap = {}
self.rs = []
def save(data, code):
try:
with open(code + ".pickle", "wb") as f:
pic.dump(data, f)
return True
except Exception as e:
return e
def load(code):
picklein = open(code + ".pickle", "rb")
data = pic.load(picklein)
return data
def makemap(text, num):
for i in range(num[0], num[1]):
save(a, "wmap")
print(i)
result=0
for ir in itertools.permutations(text, i):
l = ''.join(ir)
if l not in a.wmap:
while True:
result=random.randint(100, 1000000)
try:
print(chr(result))
except:
continue
if result not in a.rs and str(chr(result)) not in a.wmap and str(chr(result)) not in text and len(str(chr(result)))==1:
a.rs.append(result)
a.wmap.update({l:chr(result)})
break
a = A()
def do():
makemap(text, (0, 3))
save(a, "wmap")
print("Saved")
def compress(text):
for i in a.wmap:
x = a.wmap[i]
text = text.replace(i, x)
return text
def decompress(text):
for i in a.wmap:
x = a.wmap[i]
text = text.replace(x, i)
if text == i or text == x:
break
return text
do()
while True:
comp=compress(input("> "))
print(comp)
print("Length:",len(comp))
I think the problem is in makemap(), but everything I have tried in makemap does not fix it. I have been searching for hours, but have found no answer.
Aucun commentaire:
Enregistrer un commentaire