def test_encode(): message_bits = "011011111010000001100101101111100000" # Example 1 from page 67 A = numpy.array([[1, 0, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 1]]) x = PS2.encode(A, message_bits) encoded_bits = "011011000001110101110100000101001100101100011011110101010000010100" if not isinstance(x, str): print "encode() should return", type(""), "not", type(x) return False if (x != encoded_bits): print "Encode incorrect\nA:\n", A, "\nMessage bits:\n", message_bits, "\nYour encoding:\n", x, "\nCorrect encoding:\n", encoded_bits return False # Example 2 from page 67 A = numpy.array([[1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]]) x = PS2.encode(A, message_bits) encoded_bits = "011011011111111010101000000001101100101010101101011100000000000" if not isinstance(x, str): print "encode() should return", type(""), "not", type(x) return False if (x != encoded_bits): print "Encode incorrect\nA:\n", A, "\nMessage bits:\n", message_bits, "\nYour encoding:\n", x, "\nCorrect encoding:\n", encoded_bits return False return True
def test_G(): # Example 1 from page 67 A = numpy.array([[1, 0, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 1]]) try: G = PS2.compute_G(A) except AttributeError: print "compute_G() is unimplemented -- not testing" return True if G is None: print "compute_G() is unimplemented -- not testing" return True if not isinstance(G, numpy.ndarray): print "compute_G() returns type other than numpy.ndarray -- not testing" # If you want to inspect your output visually, print G and compare to correct_G return True correct_G = numpy.array([[1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0], [0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0], [0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1], [0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0], [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1]]) if not numpy.array_equal(G, correct_G): print "G matrix is incorrect" print "A:\n", A, "\nYour G:\n", G, "\nCorrect G:\n", correct_G return False # Example 2 from page 67 A = numpy.array([[1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]]) G = PS2.compute_G(A) correct_G = numpy.array([[1, 0, 0, 0, 1, 1, 0], [0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 0, 0, 1, 1], [0, 0, 0, 1, 1, 1, 1]]) if not numpy.array_equal(G, correct_G): print "G matrix is incorrect" print "A:\n", A, "\nYour G:\n", G, "\nCorrect G:\n", correct_G return False return True
def test_decode(errors=False): message_bits = "011011111010000001100101101111100000" # Example 1 from page 67 A = numpy.array([[1, 0, 1, 0, 0], [1, 0, 0, 1, 0], [1, 0, 0, 0, 1], [0, 1, 1, 0, 0], [0, 1, 0, 1, 0], [0, 1, 0, 0, 1]]) encoded_bits = "011011000001110101110100000101001100101100011011110101010000010100" # Insert random errors if errors: (k, m) = A.shape encoded_bits = insert_errors(encoded_bits, k + m, 1) x = PS2.decode(A, encoded_bits) if not isinstance(x, str): print "decode() should return", type(""), "not", type(x) return False if (x != message_bits): print "Decode incorrect\nA:\n", A, "\nEncoded bits:\n", encoded_bits, "\nYour decoding:\n", x, "\nCorrect decoding:\n", message_bits return False # Example 2 from page 67 A = numpy.array([[1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]]) encoded_bits = "011011011111111010101000000001101100101010101101011100000000000" # Insert random errors if errors: (k, m) = A.shape encoded_bits = insert_errors(encoded_bits, k+m, 1) x = PS2.decode(A, encoded_bits) if not isinstance(x, str): print "encode() should return", type(""), "not", type(x) return False if (x != message_bits): print "Decode incorrect\nA:\n", A, "\nEncoded bits:\n", encoded_bits, "\nYour decoding:\n", x, "\nCorrect decoding:\n", message_bits return False return True
def test_H(): # Example 1 from page 71 A = numpy.array([[1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1]]) try: H = PS2.compute_H(A) except AttributeError: print "compute_H() is unimplemented -- not testing" return True if H is None: print "compute_H() is unimplemented -- not testing" return True if not isinstance(H, numpy.ndarray): print "compute_H() returns type other than numpy.ndarray -- not testing" # If you want to inspect your output visually, print H and compare to correct_H return True correct_H = numpy.array([[1, 1, 0, 1, 1, 0, 0], [1, 0, 1, 1, 0, 1, 0], [0, 1, 1, 1, 0, 0, 1]]) if not numpy.array_equal(H, correct_H): print "H matrix is incorrect" print "A:\n", A, "\nYour H:\n", H, "\nCorrect H:\n", correct_H return False return True
# main.py -- put your code here! import PS2 while True: ps=PS2.PS2KEY('X18','X19','X20','X21') a=ps.ps2_key() if(a==13): pyb.LED(1).on() elif(a==14): pyb.LED(2).on() elif(a==15): pyb.LED(3).on() elif(a==16): pyb.LED(4).on() elif(a==5): pyb.LED(1).off() elif(a==6): pyb.LED(2).off() elif(a==7): pyb.LED(3).off() elif(a==8): pyb.LED(4).off()