Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CodeVector:
- def __init__(self, symbols: list[int]) -> None:
- self.positionsList: list[int] = symbols
- def __str__(self) -> str:
- return "".join(list(map(str, self.positionsList)))
- def sum_code_vectors(v1: CodeVector, v2: CodeVector) -> CodeVector:
- if len(str(v1)) != len(str(v2)):
- raise Exception("Нельзя складывать разной длины")
- result_symbols: list[int] = []
- for i in range(len(str(v1))):
- symbol_1 = int(str(v1)[i])
- symbol_2 = int(str(v2)[i])
- res_symb = sum_symbols(symbol_1, symbol_2)
- result_symbols.append(res_symb)
- return CodeVector(symbols=result_symbols)
- def sum_symbols(s1: int, s2: int) -> int:
- return (s1 + s2) % 2
- def string_to_code_vector(s: str) -> CodeVector:
- symbs: list[int] = []
- for c in s:
- if c not in ['0', '1']:
- raise Exception("Нельзя")
- symb = int(c)
- symbs.append(symb)
- return CodeVector(symbols=symbs)
- def number_to_code_vector(n: int, leng: int) -> CodeVector:
- b = bin(n)[2:]
- if len(str(b)) < leng:
- b = "".join(['0' for i in range(leng - len(str(b)))]) + b
- return string_to_code_vector(s = b)
- def ntcv(n: int, leng: int = 4) -> CodeVector:
- return number_to_code_vector(n=n, leng=leng)
- def sum_list_of_vectors(vectors: list[CodeVector]) -> CodeVector:
- summ: CodeVector = vectors[0]
- for i in range(1, len(vectors)):
- summ = sum_code_vectors(summ, vectors[i])
- return summ
- codes = list(map(ntcv, [2, 4, 5, 9, 12, 13, 14]))
- print(sum_list_of_vectors(codes))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement