#


def grayscale(lst):
    return  [ list(map(mean, row)) for row in lst ]


def sum_(lst):
    res = [] 
    for i in lst:
        if i < 0: res.append(0)
        elif i > 255: res.append(255)
        else: res.append(i)
    return sum(res)


def mean(lst):
    average = round(sum_(lst) / len(lst))
    return [average] * len(lst)


#

class Test:
    def assert_equals(a,b):
        try:
            assert a == b
        except AssertionError:
            print(f"{a}\n > should equal \n\t{b}")
TestsConsoleTest = Test            

TestsConsoleTest.assert_equals(grayscale([
	[[0,0,0], [0,0,157]],
	[[1,100,0], [0,10,0]]
]), [
	[[0,0,0], [52,52,52]],
	[[34,34,34], [3,3,3]]
])

Test.assert_equals(grayscale([
	[[0,0,0], [0,0,157], [100,229,4]],
	[[1,100,0], [0,10,0], [0,168,0]],
	[[0,125,0], [15,0,9], [0,139,0]],
	[[0,125,0], [0,0,9], [0,200,0]]
]), [
	[[0,0,0], [52,52,52], [111,111,111]],
	[[34,34,34], [3,3,3], [56,56,56]],
	[[42,42,42], [8,8,8], [46,46,46]],
	[[42,42,42], [3,3,3], [67,67,67]]
])

Test.assert_equals(grayscale([
	[[0,0,255], [0,0,0], [0,0,157], [100,229,4]],
	[[100,0,3], [1,100,0], [0,10,0], [0,168,0]],
	[[16,30,0], [0,125,0], [15,0,9], [0,139,0]],
	[[200,2,0], [0,125,0], [0,0,9], [0,200,0]]
]), [
	[[85,85,85], [0,0,0], [52,52,52], [111,111,111]],
	[[34,34,34], [34,34,34], [3,3,3], [56,56,56]],
	[[15,15,15], [42,42,42], [8,8,8], [46,46,46]],
	[[67,67,67], [42,42,42], [3,3,3], [67,67,67]]
])

Test.assert_equals(grayscale([
	[[0,-1,-120], [300,0,157]],
	[[1,100,0], [256,10,0]]
]), [
	[[0,0,0], [137,137,137]],
	[[34,34,34], [88,88,88]]
])