from random import *
from os import path, mkdir

seed("ololo")
test000 = 0


def printTest(tests):
    global test000
    test000 += 1
    if not (path.exists('../tests')):
        mkdir('../tests')
    ouf = open("../tests/%02d" % test000, "w")
    ouf = open('../tests/%02d' % test000, 'w')
    n = len(tests[0]) - 1
    m = len(tests)
    print(str(n) + ' ' + str(m), file=ouf)
    for i in range(m):
        for j in range(n):
            print(str(tests[i][j]) + " ", file=ouf, end='')
        print(str(tests[i][n]), file=ouf)
    ouf.close()


def shuffled(a):
    shuffle(a)
    return a

# groups[i] - number of bacteries in i-th group of results
# m - number of correct tests, m >= sum(groups[1]) + 2
# fail - number of incorrect tests
def genTest(groups, m, fail):
    n = sum(groups)
    bacteries = shuffled([i for i in range(n)])
    if (groups[0] == 0):
        m_1 = 0
    else:
        m_1 = randint(1, m - groups[1])
    m_2 = groups[1]
    if (groups[1] + groups[2] < 2):
        m_3 = 0
    else:
        m_3 = m - m_1 - m_2
    m = m_1 + m_2 + m_3
    print(m)
    if (groups[1] == 0):
        fail = 0
    tests = [[0] * (n + 1) for i in range(m + fail)]

    #generate tests for first group
    for i in range(groups[0]):
        test_num = randint(1, m_1) # number of test where bacteries[i] bacteria sets
        for j in range(test_num):
            test = randint(0, m_1 - 1)
            tests[test][bacteries[i]] = 1

    # this loop is used to prevent empty tests
    for i in range(m_1):
        bact_num = bacteries[randint(0, groups[0] - 1)]
        tests[i][bact_num] = 1

    # generate tests for second group
    for i in range(groups[1]):
        tests[m_1 + i][bacteries[groups[0] + i]] = 1
        tests[m_1 + i][n] = 1
        false_test_num = randint(0, groups[0])
        for j in range(false_test_num):
            bact_num = bacteries[randint(0, groups[0] - 1)]
            tests[m_1 + i][bacteries[bact_num]] = 1

    # generate tests for third group
    for i in range(m_3):
        test_num = m_1 + m_2 + i
        tests[test_num][n] = 1
        maybe_test_num = 0
        good_test_num = 0
        while(maybe_test_num + good_test_num < 2):
            maybe_test_num = randint(0, groups[2])
            good_test_num = randint(0, groups[1])

        false_test_num = randint(0, groups[0])
        for j in range(maybe_test_num):
            bact_num = bacteries[randint(groups[0] + groups[1], n - 1)]
            tests[test_num][bact_num] = 1
        for j in range(good_test_num):
            bact_num = bacteries[randint(groups[0], groups[0] + groups[1] - 1)]
            tests[test_num][bact_num] = 1
        for j in range(false_test_num):
            bact_num = bacteries[randint(0, groups[0] - 1)]
            tests[test_num][bact_num] = 1

    for i in range(fail):
        tests[m + i][n] = 1
        fail_test_num = randint(1, groups[0])
        for j in range(fail_test_num):
            tests[m + i][bacteries[randint(0, groups[0] - 1)]] = 1

    shuffle(tests)
    return tests                                    

#sample tests
printTest([[1, 0, 0, 0], [1, 1, 1, 1], [0, 1, 0, 0]])
printTest([[1, 1, 0], [1, 0, 1]])

printTest(genTest([2, 1, 0], 3, 0))
printTest(genTest([2, 1, 0], 20, 0))
printTest(genTest([10, 0, 0], 2, 0))
printTest(genTest([0, 10, 0], 20, 0))
printTest(genTest([0, 0, 10], 5, 0))
printTest(genTest([10, 10, 0], 12, 5))
printTest(genTest([10, 0, 10], 6, 0))
printTest(genTest([10, 0, 10], 5000, 0))
printTest(genTest([10, 0, 0], 2, 0))
printTest(genTest([15, 0, 15], 2, 0))
printTest(genTest([15, 15, 0], 40, 0))
printTest(genTest([15, 0, 15], 10, 0))
printTest(genTest([15, 15, 0], 40, 0))
printTest(genTest([15, 15, 4], 40, 0))
printTest(genTest([15, 15, 0], 40, 6))
printTest(genTest([15, 15, 5], 10000, 0))
printTest(genTest([15, 5, 15], 3000, 0))
printTest(genTest([5, 15, 15], 3000, 0))
printTest(genTest([30, 40, 30], 100, 0))
printTest(genTest([40, 30, 30], 50, 0))
printTest(genTest([30, 30, 40], 50, 0))
printTest(genTest([30, 30, 40], 10000, 0))
printTest(genTest([30, 30, 40], 9999, 1))
printTest(genTest([100, 0, 0], 10000, 0))
printTest(genTest([0, 100, 0], 10000, 0))
printTest(genTest([0, 0, 100], 10000, 0))
printTest(genTest([0, 0, 100], 1, 0))