compressionproject/CTPLICA/statistics.py
2022-03-31 19:38:40 +02:00

51 lines
No EOL
1.8 KiB
Python

#!/usr/bin/python3
import matplotlib as plt
def good_format(result: str) -> str:
# order of following instructions is very important
while result.startswith('0') and len(result)>=2 and result[1] != '.': result = result[1:]
while result.startswith('-0') and len(result)>=3 and result[2] != '.': result = result.replace('-0','-')
if '.' in result and result.split('.')[1]=='0'*len(result.split('.')[1]): result = result.split('.')[0]
while '.' in result and result.endswith('0'): result = result[:-1]
return result
n_visu = 20
if __name__=='__main__':
#start generating codes
v1 = [i for i in range(256)]
v2 = [i for i in range(256)]
codes = []
for e1 in v1:
for e2 in v2:
codes.append( (e1+e2,e1-e2) )
#format codes
formatted_codes = []
#f = open('formatted_codes.txt', 'w')
for code in codes:
current_code = ''
#arranging to have 3 digits for each element in tuple
X = str(code[0])
Y = str(abs(code[1])) #to avoid converting the negative sign into string
if code[0] < 10: X = '00'+X
elif code[0] < 100: X = '0'+X
if abs(code[1]) < 10: Y = '00'+Y
elif abs(code[1]) < 100: Y = '0'+Y
#creating current formatted code
if code[1] < 0: #e1-e2<0
current_code += '1'+X+Y
else:
current_code += '0'+X+Y
#visualization purpose
if n_visu>0:
print("X", X, "Y", Y, "formatted code:", current_code)
n_visu -= 1
#put current code into formatted_codes and file
formatted_codes.append( current_code )
#print("verification:", '1510255' in formatted_codes)
#f.write(current_code+'\n')
#f.close()
#giving statistics
print("Number of codes:", len(formatted_codes))
print("Probability to choose a code:", 1.0/len(formatted_codes))