From 6b728695cb73e4249439dbd39b355e4520eb1071 Mon Sep 17 00:00:00 2001 From: chabisik Date: Mon, 28 Mar 2022 01:23:08 +0200 Subject: [PATCH] Adding encode and decode functions --- CTPLICA/comproject.py | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/CTPLICA/comproject.py b/CTPLICA/comproject.py index 7144808..3e5a26b 100644 --- a/CTPLICA/comproject.py +++ b/CTPLICA/comproject.py @@ -1,8 +1,7 @@ #!/usr/bin/python3 import sys -from typing import Sequence -from unittest import result +from typing import List, Sequence import numpy import time @@ -65,7 +64,9 @@ def unoverlap(arg: str, apply_format: bool=True) -> Sequence[str]: arg1 = '' #for stocking unoverlapped values arg2 = '' 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 try: 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 +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__': #print("add", sb_addition("2222222222222222222222222222222222222222.55", "2.55") ) @@ -395,6 +423,9 @@ if __name__=='__main__': #ov_r = overlap('-12512.252','-125') #print(unoverlap(ov_r)) #print("mult", sb_multiply('-125.32564','100000.0')) - print(sb_division('0.0001','-12.25', digits_after_decimal_point=15)) - print(sb_multiply('-10.00000','-0.0001')) - pass \ No newline at end of file + #print(sb_division('22','7', digits_after_decimal_point=15)) + #print(sb_multiply('100000.00000','-0111111.0001')) + #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)) \ No newline at end of file