Advertisement
Catsher

0101

May 22nd, 2024
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. class CodeVector:
  2.     def __init__(self, symbols: list[int]) -> None:
  3.         self.positionsList: list[int] = symbols
  4.  
  5.     def __str__(self) -> str:
  6.         return "".join(list(map(str, self.positionsList)))    
  7.  
  8.  
  9. def sum_code_vectors(v1: CodeVector, v2: CodeVector) -> CodeVector:
  10.     if len(str(v1)) != len(str(v2)):
  11.         raise Exception("Нельзя складывать разной длины")
  12.  
  13.     result_symbols: list[int] = []
  14.     for i in range(len(str(v1))):
  15.         symbol_1 = int(str(v1)[i])
  16.         symbol_2 = int(str(v2)[i])
  17.  
  18.         res_symb = sum_symbols(symbol_1, symbol_2)
  19.         result_symbols.append(res_symb)
  20.  
  21.     return CodeVector(symbols=result_symbols)
  22.  
  23. def sum_symbols(s1: int, s2: int) -> int:
  24.     return (s1 + s2) % 2
  25.  
  26. def string_to_code_vector(s: str) -> CodeVector:
  27.     symbs: list[int] = []
  28.     for c in s:
  29.         if c not in ['0', '1']:
  30.             raise Exception("Нельзя")
  31.         symb = int(c)
  32.         symbs.append(symb)
  33.  
  34.     return CodeVector(symbols=symbs)
  35.  
  36. def number_to_code_vector(n: int, leng: int) -> CodeVector:
  37.     b = bin(n)[2:]
  38.     if len(str(b)) < leng:
  39.         b = "".join(['0' for i in range(leng - len(str(b)))]) + b
  40.     return string_to_code_vector(s = b)
  41.  
  42. def ntcv(n: int, leng: int = 4) -> CodeVector:
  43.     return number_to_code_vector(n=n, leng=leng)
  44.  
  45. def sum_list_of_vectors(vectors: list[CodeVector]) -> CodeVector:
  46.     summ: CodeVector = vectors[0]
  47.     for i in range(1, len(vectors)):
  48.         summ = sum_code_vectors(summ, vectors[i])
  49.  
  50.     return summ
  51.  
  52. codes = list(map(ntcv, [2, 4, 5, 9, 12, 13, 14]))
  53. print(sum_list_of_vectors(codes))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement