Problem p02825 (python)

Fixed submission:
import sys
N = int(input())
ans = [['.']*N for _ in range(N)]
def make4(a):
    for j in range(4):
        ans[a][a] = ans[a+1][a] = 'a'
        ans[a][a+1] = ans[a+1][a+1] = 'b'
        ans[a][a+2] = ans[a][a+3] = 'c'
        ans[a+1][a+2] = ans[a+1][a+3] = 'd'

        ans[a+2][a] = ans[a+2][a+1] = 'e'
        ans[a+3][a] = ans[a+3][a+1] = 'f'
        ans[a+2][a+2] = ans[a+3][a+2] = 'g'
        ans[a+2][a+3] = ans[a+3][a+3] = 'h'
def make6(a):
    ans[a+3][a] = ans[a+4][a] = 'a'
    ans[a][a+3] = ans[a][a+4] = 'a'
    ans[a+5][a+1] = ans[a+5][a+2] = 'b'
    ans[a+1][a+5] = ans[a+2][a+5] = 'b'

    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'

    ans[a+3][a+3] = ans[a+4][a+3] = 'c'
    ans[a+3][a+4] = ans[a+3][a+5] = 'd'
    ans[a+4][a+5] = ans[a+5][a+5] ='e'
    ans[a+5][a+3] = ans[a+5][a+4]  = 'f'
def make5(a):
    ans[a+2][a+1] = ans[a+3][a+1] = 'a'
    ans[a+1][a+2] = ans[a+1][a+3] = 'a'

    ans[a][a] = ans[a+1][a] = 'b'
    ans[a+2][a] = ans[a+3][a] = 'c'

    ans[a+4][a] = ans[a+4][a+1] = 'd'
    ans[a+4][a+2] = ans[a+4][a+3] = 'e'

    ans[a+4][a+4] = ans[a+3][a+4] = 'f'
    ans[a+2][a+4] = ans[a+1][a+4] = 'g'

    ans[a][a+1] = ans[a][a+2] = 'h'
    ans[a][a+3] = ans[a][a+4] = 'i'




def answer():
    for i in range(N):
        print(*ans[i], sep = '')      

if N == 1 or N == 2:
    print(-1)
elif N == 3:
    a = 0
    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'
    answer()
elif N ==7:
    print(".aabbcc",
"e.hhii.",
"ek....p",
"fk....p",
"fl....q",
"gl....q",
"g.mmnn.", sep = '\n')


elif N%2 == 0:
    if N%4 == 0:
        for i in range(0, N, 4):
            make4(i)
        answer()
    else:
        make6(0)
        for i in range(6, N, 4):
            make4(i)
        answer()
else:
    make5(0)
    if (N-5)%4 == 0:
        for i in range(5, N, 4):
            make4(i)
    else:
        make6(5)
        for i in range(5+6, N, 4):
            make4(i)
    answer()

Buggy submission (verdict: Runtime Error):
import sys
N = int(input())
ans = [['.']*N for _ in range(N)]
def make4(a):
    for j in range(4):
        ans[a][a] = ans[a+1][a] = 'a'
        ans[a][a+1] = ans[a+1][a+1] = 'b'
        ans[a][a+2] = ans[a][a+3] = 'c'
        ans[a+1][a+2] = ans[a+1][a+3] = 'd'

        ans[a+2][a] = ans[a+2][a+1] = 'e'
        ans[a+3][a] = ans[a+3][a+1] = 'f'
        ans[a+2][a+2] = ans[a+3][a+2] = 'g'
        ans[a+2][a+3] = ans[a+3][a+3] = 'h'
def make6(a):
    ans[a+3][a] = ans[a+4][a] = 'a'
    ans[a][a+3] = ans[a][a+4] = 'a'
    ans[a+5][a+1] = ans[a+5][a+2] = 'b'
    ans[a+1][a+5] = ans[a+2][a+5] = 'b'

    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'

    ans[a+3][a+3] = ans[a+4][a+3] = 'c'
    ans[a+3][a+4] = ans[a+3][a+5] = 'd'
    ans[a+4][a+5] = ans[a+5][a+5] ='e'
    ans[a+5][a+3] = ans[a+5][a+4]  = 'f'
def make5(a):
    ans[a+2][a+1] = ans[a+3][a+1] = 'a'
    ans[a+1][a+2] = ans[a+1][a+3] = 'a'

    ans[a][a] = ans[a+1][a] = 'b'
    ans[a+2][a] = ans[a+3][a] = 'c'

    ans[a+4][a] = ans[a+4][a+1] = 'd'
    ans[a+4][a+2] = ans[a+4][a+3] = 'e'

    ans[a+4][a+4] = ans[a+3][a+4] = 'f'
    ans[a+2][a+4] = ans[a+1][a+4] = 'g'

    ans[a][a+1] = ans[a][a+2] = 'h'
    ans[a][a+3] = ans[a][a+4] = 'i'




def answer():
    for i in range(N):
        print(*ans[i], sep = '')      

if N == 1 or N == 2 or N == 7:
    print(-1)
elif N%2 == 0:
    if N%4 == 0:
        for i in range(0, N, 4):
            make4(i)
        answer()
    else:
        make6(0)
        for i in range(6, N, 4):
            make4(i)
        answer()
else:
    make5(0)
    if (N-5)%4 == 0:
        for i in range(5, N, 4):
            make4(i)
    else:
        make6(5)
        for i in range(5+6, N, 4):
            make4(i)
    answer()

Buggy submission (verdict: Runtime Error):
import sys
N = int(input())
ans = [['.']*N for _ in range(N)]
def make4(a):
    for j in range(4):
        ans[a][a] = ans[a+1][a] = 'a'
        ans[a][a+1] = ans[a+1][a+1] = 'b'
        ans[a][a+2] = ans[a][a+3] = 'c'
        ans[a+1][a+2] = ans[a+1][a+3] = 'd'

        ans[a+2][a] = ans[a+2][a+1] = 'e'
        ans[a+3][a] = ans[a+3][a+1] = 'f'
        ans[a+2][a+2] = ans[a+3][a+2] = 'g'
        ans[a+2][a+3] = ans[a+3][a+3] = 'h'
def make6(a):
    ans[a+3][a] = ans[a+4][a] = 'a'
    ans[a][a+3] = ans[a][a+4] = 'a'
    ans[a+5][a+1] = ans[a+5][a+2] = 'b'
    ans[a+1][a+5] = ans[a+2][a+5] = 'b'

    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'

    ans[a+3][a+3] = ans[a+4][a+3] = 'c'
    ans[a+3][a+4] = ans[a+3][a+5] = 'd'
    ans[a+4][a+5] = ans[a+5][a+5] ='e'
    ans[a+5][a+3] = ans[a+5][a+4]  = 'f'
def make5(a):
    ans[a+2][a+1] = ans[a+3][a+1] = 'a'
    ans[a+1][a+2] = ans[a+1][a+3] = 'a'

    ans[a][a] = ans[a+1][a] = 'b'
    ans[a+2][a] = ans[a+3][a] = 'c'

    ans[a+4][a] = ans[a+4][a+1] = 'd'
    ans[a+4][a+2] = ans[a+4][a+3] = 'e'

    ans[a+4][a+4] = ans[a+3][a+4] = 'f'
    ans[a+2][a+4] = ans[a+1][a+4] = 'g'

    ans[a][a+1] = ans[a][a+2] = 'h'
    ans[a][a+3] = ans[a][a+4] = 'i'




def answer():
    for i in range(N):
        print(*ans[i])      

if N == 1 or N == 2 or N == 7:
    print(-1)
elif N%2 == 0:
    if N%4 == 0:
        for i in range(0, N, 4):
            make4(i)
        answer()
    else:
        make6(0)
        for i in range(6, N, 4):
            make4(i)
        answer()
else:
    make5(0)
    if (N-5)%4 == 0:
        for i in range(5, N, 4):
            make4(i)
    else:
        make6(5)
        for i in range(5+6, N, 4):
            make4(i)
    answer()

Buggy submission (verdict: Runtime Error):
import sys
N = int(input())
ans = [['.']*N for _ in range(N)]
def make4(a):
    for j in range(4):
        ans[a][a] = ans[a+1][a] = 'a'
        ans[a][a+1] = ans[a+1][a+1] = 'b'
        ans[a][a+2] = ans[a][a+3] = 'c'
        ans[a+1][a+2] = ans[a+1][a+3] = 'd'

        ans[a+2][a] = ans[a+2][a+1] = 'e'
        ans[a+3][a] = ans[a+3][a+1] = 'f'
        ans[a+2][a+2] = ans[a+3][a+2] = 'g'
        ans[a+2][a+3] = ans[a+3][a+3] = 'h'
def make6(a):
    ans[a+3][a] = ans[a+4][a] = 'a'
    ans[a][a+3] = ans[a][a+4] = 'a'
    ans[a+5][a+1] = ans[a+5][a+2] = 'b'
    ans[a+1][a+5] = ans[a+2][a+5] = 'b'

    ans[a][a] = ans[a+1][a] = 'c'
    ans[a][a+1] = ans[a][a+2] = 'd'
    ans[a+1][a+2] = ans[a+2][a+2] ='e'
    ans[a+2][a] = ans[a+2][a+1]  = 'f'

    ans[a+3][a+3] = ans[a+4][a+3] = 'c'
    ans[a+3][a+4] = ans[a+3][a+5] = 'd'
    ans[a+4][a+5] = ans[a+5][a+5] ='e'
    ans[a+5][a+3] = ans[a+5][a+4]  = 'f'
def make5(a):
    ans[a+2][a+1] = ans[a+3][a+1] = 'a'
    ans[a+1][a+2] = ans[a+1][a+3] = 'a'

    ans[a][a] = ans[a+1][a] = 'b'
    ans[a+2][a] = ans[a+3][a] = 'c'

    ans[a+4][a] = ans[a+4][a+1] = 'd'
    ans[a+4][a+2] = ans[a+4][a+3] = 'e'

    ans[a+4][a+4] = ans[a+3][a+4] = 'f'
    ans[a+2][a+4] = ans[a+1][a+4] = 'g'

    ans[a][a+1] = ans[a][a+2] = 'h'
    ans[a][a+3] = ans[a][a+4] = 'i'




def answer():
    for i in range(N):
        print(*ans[i])      

if N == 1 or N == 2 or N == 7:
    print(-1)
    sys.exit()
if N%2 == 0:
    if N%4 == 0:
        for i in range(0, N, 4):
            make4(i)
        answer()
    else:
        make6(0)
        for i in range(6, N, 4):
            make4(i)
        answer()
else:
    make5(0)
    if (N-5)%4 == 0:
        for i in range(5, N, 4):
            make4(i)
    else:
        make6(5)
        for i in range(5+6, N, 4):
            make4(i)
    answer()