#!/usr/bin/python3

import sys
import numpy
import time

def apply_codes_for_coding(arg1, arg2):
    arg1_coded_vector = numpy.array([arg1, arg1]) #vector obtained when multipying arg1 by code [1 1]
    arg2_coded_vector = numpy.array([arg2, -arg2]) #vector obtained when multipying arg2 by code [1 -1]
    result_vector = (arg1_coded_vector + arg2_coded_vector)/2.0
    return result_vector[0], result_vector[1]

def overlap(arg1, arg2, divide_overlap_result_by=1.0):
    overlap_result = ""
    arg1_as_string = str(arg1)
    arg2_as_string = str(arg2)
    #print("arg1", arg1_as_string, "arg2", arg2_as_string)
    arg1_integer_part, arg1_decimal_part = arg1_as_string.split(".")
    arg2_integer_part, arg2_decimal_part = arg2_as_string.split(".")
    #print("arg1 : interger part", arg1_integer_part, "and decimal part", arg1_decimal_part)
    #print("arg2 : interger part", arg2_integer_part, "and decimal part", arg2_decimal_part)
    #print("===============================================")
    if not(arg1_integer_part.startswith("-")) and arg2_integer_part.startswith("-"): #arg1 positive - arg2 negative
        arg2_integer_part = arg2_integer_part.replace("-", "")
        overlap_result += "1"
    elif not(arg1_integer_part.startswith("-")) and not(arg2_integer_part.startswith("-")): #arg1 positive - arg2 positive
        overlap_result += "2"
    elif arg1_integer_part.startswith("-") and not(arg2_integer_part.startswith("-")): #arg1 negative - arg2 positive
        arg1_interger_part = arg1_integer_part.replace("-", "")
        overlap_result += "3"
    elif arg1_integer_part.startswith("-") and arg2_integer_part.startswith("-"): #arg1 negative - arg2 negative
        arg1_integer_part = arg1_integer_part.replace("-", "")
        arg2_integer_part = arg2_integer_part.replace("-", "")
        overlap_result += "4"
    #print("arg1 : interger part", arg1_integer_part, "and decimal part", arg1_decimal_part)
    #print("arg2 : interger part", arg2_integer_part, "and decimal part", arg2_decimal_part)
    #print("overlap start", overlap_result)
    #print("===============================================")
    max_integer_part_size = max(len(arg1_integer_part),len(arg2_integer_part))
    max_decimal_part_size = max(len(arg1_decimal_part),len(arg2_decimal_part))
    arg1_integer_part = "0"*(max_integer_part_size-len(arg1_integer_part)) + arg1_integer_part
    arg2_integer_part = "0"*(max_integer_part_size-len(arg2_integer_part)) + arg2_integer_part
    arg1_decimal_part = arg1_decimal_part + "0"*(max_decimal_part_size-len(arg1_decimal_part))
    arg2_decimal_part = arg2_decimal_part + "0"*(max_decimal_part_size-len(arg2_decimal_part))
    #print("arg1 : interger part", arg1_integer_part, "and decimal part", arg1_decimal_part)
    #print("arg2 : interger part", arg2_integer_part, "and decimal part", arg2_decimal_part)
    #print("===============================================")
    for index in range(max_integer_part_size):
        overlap_result += arg1_integer_part[index]
        overlap_result += arg2_integer_part[index]
    overlap_result += "."
    for index in range(max_decimal_part_size):
        overlap_result += arg1_decimal_part[index]
        overlap_result += arg2_decimal_part[index]
    #print("string overlap start", overlap_result, "string size in memory", sys.getsizeof(overlap_result))
    #print("float overlap start", float(overlap_result), "float size in memory", sys.getsizeof(float(overlap_result)))
    return float(overlap_result)/float(divide_overlap_result_by)

def unoverlap(arg, before_unoverlap_multiply_by=1.0):
    arg1 = "" #for stocking unoverlapped values
    arg2 = ""
    arg_as_string = str(arg*before_unoverlap_multiply_by)
    arg_integer_part, arg_decimal_part = arg_as_string.split(".")
    if arg_integer_part[0]=="1": #arg1 must be positive - arg2 must be negative
        arg2 += "-"
    elif arg_integer_part[0]=="2": #arg1 must be positive - arg2 must be positive
        pass
    elif arg_integer_part[0]=="3": #arg1 must be negative - arg2 must be positive
        arg1 += "-"
    elif arg_integer_part[0]=="4": #arg1 must be negative - arg2 must be negative
        arg1 += "-"
        arg2 += "-"
    arg_integer_part = arg_integer_part[1:]
    for index in range(len(arg_integer_part)):
        if index%2==0: arg1 += arg_integer_part[index]
            #arg1 += arg_integer_part[index]
        else: arg2 += arg_integer_part[index]
            #arg2 += arg_integer_part[index]
    arg1 += "."
    arg2 += "."
    for index in range(len(arg_decimal_part)):
        if index%2==0:
            arg1 += arg_decimal_part[index]
        else:
            arg2 += arg_decimal_part[index]
    #print("arg1", arg1, "arg2", arg2)
    return float(arg1), float(arg2)

def apply_codes_for_decoding(arg1, arg2):
    coded_vector = numpy.array([arg1, -arg2])
    code_one = numpy.array([1.0, 1.0])
    code_two = numpy.array([1.0, -1.0])
    return numpy.sum(coded_vector*code_one), numpy.sum(coded_vector*code_two)

#data = [2.55 2.55 0.01]
if __name__=='__main__':
    r1,r2 = apply_codes_for_coding(2.55, 2.55)
    print("r1 r2", r1, r2)
    r = overlap(r1, r2, divide_overlap_result_by=1.0)
    print("r", r)

    r1,r2 = apply_codes_for_coding(r, 2.55)
    print("r1 r2", r1, r2)
    r = overlap(r1, r2, divide_overlap_result_by=1.0)
    print("r", r)

    """r1,r2 = apply_codes_for_coding(r, 1.27)
    r = overlap(r1, r2, divide_overlap_result_by=1.0)"""

    #------------------------------------

    d,d2 = unoverlap(r)
    d,d2 = apply_codes_for_decoding(d,d2)
    print("d d2", d, d2)

    d,d2 = unoverlap(d)
    d,d2 = apply_codes_for_decoding(d2,d)
    print("d d2", d, d2)
    """print(d)
    print(d2)"""

    """d,d2 = unoverlap(d)
    d,d2 = apply_codes_for_decoding(d,d2)
    print(d)
    print(d2)"""


    pass