Skip to content

Instantly share code, notes, and snippets.

@DreamLinuxer
Created September 27, 2017 17:47
Show Gist options
  • Save DreamLinuxer/675d257720659c237665e96884af39c9 to your computer and use it in GitHub Desktop.
Save DreamLinuxer/675d257720659c237665e96884af39c9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import subprocess32 as subprocess
import sys
import os
test_inputs = [[4,1,1],[6,2,3],[8,4,4],[11,9,8],[15,7,2]]
def get_name(s):
return s[0:s.index('_')]
def check_board(board, N):
for row in board:
if len(row) != N:
return True
for col in row:
if not (col == 'Q' or col == '_' or col == 'X'):
return True
return False
def inboard(i,N):
return 0 <= i and i < N
def check_rook(board,r,c,N):
for i in range(0,N):
if i != c and board[r][i] != '_':
return True
elif i !=r and board[i][c] != '_':
return True
def check_queen(board,r,c,N):
if check_rook(board,r,c,N):
return True
for i in range(1,N):
if inboard(r+i,N) and inboard(c+i,N) and board[r+i][c+i] != '_':
return True
elif inboard(r-i,N) and inboard(c-i,N) and board[r-i][c-i] != '_':
return True
elif inboard(r-i,N) and inboard(c+i,N) and board[r-i][c+i] != '_':
return True
elif inboard(r+i,N) and inboard(c-i,N) and board[r+i][c-i] != '_':
return True
return False
def check(board,N):
if N != sum([row.count('Q') for row in board]):
return 'F'
for r in range(0,N):
for c in range(0,N):
if board[r][c] == 'Q' and check_queen(board,r,c,N):
return 'F'
return 'P'
# return value
# I : Illegal format, execution error
# P : Pass
# F : Fail
# T : Time limit exceeded
def run_test(DIR,f,t):
arg = ["./"+DIR+"/"+f, "nqueen", str(t[0]) , str(t[1]) , str(t[2])]
print(arg)
try:
proc = subprocess.Popen(arg, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
try:
proc.wait(timeout=30)
ans = []
log = open("./"+DIR+"/"+f+".output."+str(t[0]), "w+")
for line in proc.stdout:
line = line.decode()
ans.append(line)
log.write(line)
log.close()
board =[]
for line in ans:
if line != "\n":
row = line.strip().decode().split(" ")
board.append(row)
if len(board) < t[0]:
return 'I'
board = board[-(t[0]):]
if check_board(board, t[0]):
return 'I'
if board[t[1]-1][t[2]-1] != 'X':
return 'F'
board[t[1]-1][t[2]-1] = '_'
return check(board, t[0])
except subprocess.TimeoutExpired:
proc.kill()
return 'T'
except:
return 'I'
def main():
DIR = sys.argv[1]
files = sorted([x for x in os.listdir(DIR) if ("a0" in x) and (x[-3:] == ".py")])
output = open("grade-"+DIR+".csv", "w+")
for f in files:
result = [run_test(DIR,f,t) for t in test_inputs]
print(get_name(f) + "," + ''.join(result) + "," + str(result.count('P')))
output.write(get_name(f) + "," + ''.join(result) + "," + str(result.count('P')) + "\n")
output.flush()
output.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment