# MCS 275 Q2(a) Tue 17 Jan 2017
"""
To answer the questions in this quiz, you must use the array type.
(1) Define a function which takes on input a natural number
    and which returns an array of randomly generated zeros and ones.
    The size of the array equals the number on input.
(2) An array is symmetric if it reads the same from left to right
    as from right to left.  Define a function which returns True
    if the array is symmetric, and False otherwise.
Use the prototypes of the functions as given below.
"""

from array import array

def random_bits(nbr):
    """
    Returns an array with as many random bits
    as the value of nbr.
    """
    from random import randint
    bits = [randint(0, 1) for _ in range(nbr)]
    return array('b', bits)

def is_symmetric(arr):
    """
    We say that an array is symmetric
    if the i-th entry is the same as the (n-i-1)-th entry
    where n is the length of the array.
    This function returns True if the array arr is symmetric
    and False otherwise.
    """
    dim = arr.buffer_info()[1]
    for i in range(dim//2):
        if arr[i] != arr[dim-1-i]:
            return False
    return True

def main():
    """
    Prompts the user for the number of bits.
    Generates an array of random bits.
    """
    nbr = int(input("Give the number of bits : "))
    bits = random_bits(nbr)
    print(bits, end='')
    if is_symmetric(bits):
        print(' is symmetric')
    else:
        print(' is not symmetric')

if __name__ == "__main__":
    main()
