Adding encode and decode functions

This commit is contained in:
chabisik 2022-03-28 01:23:08 +02:00
parent 88775ec09f
commit 6b728695cb

View file

@ -1,8 +1,7 @@
#!/usr/bin/python3 #!/usr/bin/python3
import sys import sys
from typing import Sequence from typing import List, Sequence
from unittest import result
import numpy import numpy
import time import time
@ -65,7 +64,9 @@ def unoverlap(arg: str, apply_format: bool=True) -> Sequence[str]:
arg1 = '' #for stocking unoverlapped values arg1 = '' #for stocking unoverlapped values
arg2 = '' arg2 = ''
arg_as_string = str(arg) arg_as_string = str(arg)
# MUST TRY TO PAD THE STRING HERE IN CASE good_format were applied on it during operations # MUST TRY TO PAD THE STRING HERE IN CASE good_format were applied on it during operations (I verified and not need for the moment)
arg_integer_part = arg_as_string
arg_decimal_part = ''
arg_is_full_integer = True arg_is_full_integer = True
try: try:
arg_integer_part, arg_decimal_part = arg_as_string.split(".") arg_integer_part, arg_decimal_part = arg_as_string.split(".")
@ -375,6 +376,33 @@ def sb_division(arg1: str, arg2: str, digits_after_decimal_point: int=5, apply_f
#when encoding, verify overlap_result being even as #when encoding, verify overlap_result being even as
def encode(data: List[str]):
assert len(data) >= 2
code = overlap( sb_addition(data[0], data[1]), sb_substract(data[0], data[1]) )
if len(data)>2:
for element in data[2:]:
code = overlap( sb_addition(code, element), sb_substract(code, element) )
return code
def decode(code: str, size):
data = []
r1 = ''
r2 = ''
while len(data)!=(size-2):
r1, r2 = unoverlap(code)
code = sb_division( sb_addition(r1,r2), '2' )
element = sb_division( sb_substract(r1,r2), '2' )
data.append(element)
r1, r2 = unoverlap(code)
data.append(sb_division( sb_substract(r1,r2), '2' ))
data.append(sb_division( sb_addition(r1,r2), '2' ))
result = []
for index in range(len(data)-1,-1,-1): result.append(data[index])
return result
if __name__=='__main__': if __name__=='__main__':
#print("add", sb_addition("2222222222222222222222222222222222222222.55", "2.55") ) #print("add", sb_addition("2222222222222222222222222222222222222222.55", "2.55") )
@ -395,6 +423,9 @@ if __name__=='__main__':
#ov_r = overlap('-12512.252','-125') #ov_r = overlap('-12512.252','-125')
#print(unoverlap(ov_r)) #print(unoverlap(ov_r))
#print("mult", sb_multiply('-125.32564','100000.0')) #print("mult", sb_multiply('-125.32564','100000.0'))
print(sb_division('0.0001','-12.25', digits_after_decimal_point=15)) #print(sb_division('22','7', digits_after_decimal_point=15))
print(sb_multiply('-10.00000','-0.0001')) #print(sb_multiply('100000.00000','-0111111.0001'))
pass #print(unoverlap('132.1230010'))
c = encode(['2.55','1.27','0.50','0.25','0.01','1.27','0','0.78','0.99','0.54','2.31','2.55','1.27','0.50','0.50','0.50'])
print("c",c)
print(decode(c, size=16))