Skip to content

Instantly share code, notes, and snippets.

@potpath

potpath/grader.sh

Last active Aug 29, 2015
Embed
What would you like to do?
Simple grader written in shell script
#!/bin/bash
ext=sol
if [ "$#" -lt 1 ]
then
echo "Usage: $0 [executable] code [input_output_solution_folder]"
exit 1
fi
if [ -x "`which "$1"`" ]
then
bin=$1
shift
else
bin=java
fi
code=$1
if [ "$#" -lt 2 ]
then
folder=.
else
folder=$2
fi
THR=1 #Time Limit
sum=0
nCase=0
nT=0
nWrong=0
max=0
IFS=$'\n'
for input in `find "$folder" -name '*.in' | sort --version-sort`
do
let nCase++
sol="${input%in}$ext"
time=`{ timeout "$THR" /usr/bin/time -f%e "$bin" "$code" < "$input" > /tmp/a; } 2>&1`
if [ ! "$time" ]
then
echo -n T
let nT++
else
if ! diff --ignore-trailing-space "$sol" /tmp/a >/dev/null
then
echo -n -
let nWrong++
else
#basename "$input"
#echo $time
sum=`bc <<< "$sum+$time"`
if [ `bc <<< "$max < $time"` -eq 1 ]
then
max="$time"
fi
if [ `bc <<< "$THR < $time"` -eq 0 ]
then
echo -n P
fi
fi
fi
done
rm /tmp/a 2>/dev/null
echo
#echo sum: $sum
echo mean: `bc -l <<< "$sum /( $nCase - $nT )"`
echo max: $max
echo number of test case: $nCase
echo number of T case: $nT
echo number of - case: $nWrong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment