aydarbiktimirov

Untitled

Nov 25th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import itertools
  4. import random
  5. import functools
  6. import operator
  7.  
  8.  
  9. def polynomial_value(polynomial, x):
  10.     return functools.reduce(operator.xor, [polynomial[monomial] * functools.reduce(operator.mul, [x[i] for i in monomial], 1) for monomial in polynomial], 0)
  11.  
  12.  
  13. def generate_test(r, m):
  14.     random_polynomial = dict((multi_index, round(random.uniform(0, 1))) for l in range(r + 1) for multi_index in itertools.combinations(range(m), l))
  15.     correct_word = [polynomial_value(random_polynomial, x[::-1]) for x in itertools.product(*[[0, 1]] * m)]
  16.     corrupted_word = correct_word[:]
  17.     for _ in range((2 ** (m - r - 1)) - 1):
  18.         corrupted_word[round(random.uniform(0, len(correct_word) - 1))] ^= 1
  19.     return corrupted_word, correct_word
  20.  
  21.  
  22. if __name__ == "__main__":
  23.     print(generate_test(2, 4))
Advertisement
Add Comment
Please, Sign In to add comment