If you feel you know some funny problem and you want it to be added to our site - welcome :)
Here are general consideration and then follow some advanced technical details (for those who want to contribute data generator / checker code).
PHP
but additionally could be done in Python
and Perl
. If you would like to
participate in writing this, check the details below.SVG
format or small raster images hosted outside (though SVG is preferred);Generally it is best to start with small forum post outlining your suggestion so we discuss feasibility of the idea and what is to be done.
Oldest way the "checkers" for problems work is that when the page is loaded by user, the code for the task is executed and gives two values as output:
There are some obvious limitations - code should not run more than about 1 second (and be sure, server is probably not as fast as your personal computer). Also most probably there is some memory limitation but usually we don't hit it.
Currently supported languages are:
PHP
at version 7.4
Python
at 2.7
and 3.5
at most (preferably use 3-rd version of course)Perl
at 5.10.1
With Python and Perl your code should simply produce output (e.g. with print
s) of which all lines except last
are considered input data
for the problem and the last is the expected answer
. For example:
#python3
import random
n = random.randint(4, 9)
print(n) # number of testcases
ans = []
while len(ans) != n:
a = random.randint(100, 999)
b = random.randint(100, 999)
ans.append(str(a + b))
print(a, b) # next testcase
print(' '.join(ans))
Note the comment in the first line - website uses it to choose the language.
With PHP compose a function which returns array of two string values (input and answer). If the code is large, feel free to add some class with a fancy name and hide any necessary internals inside it.
function checker() {
$n = rand(20, 30);
$q = array($n);
$r = array();
for ($i = 0; $i < $n; $i++) {
$a = rand(-9999999, 9999999);
$b = rand(-9999999, 9999999);
array_push($q, "$a $b");
array_push($r, min($a, $b));
}
return array(implode("\n", $q) , implode(' ', $r));
}
If the problem has possible alternative answers, just ask for additional instructions :)
In some cases it doesn't work to simply compare the submitted answer with precalculated one. For example, problem could have several valid answers and we need to verify validity with some algorithm. Or we may want to check that answer is submitted within specific amount of time.
For such cases there is quite versatile approach. If the checker produces answer starting with #pyx
(5 characters, including space), then answer verification is done with calling the same code, but providing
it with two additional global variables:
expected
holds the line the checker gave out for answer (i.e. one starting with #pyx
and followed by
any arbitrary payload you would like (e.g. initial data or expected answer with timestamp).answer
- whatever user have submitted for an answer.The code should print out the single string in response to this. If the string is ok
then problem is
accepted. Otherwise string is regarded as error message and is shown to user with failed attempt.
Here is code example to demonstrate the approach:
#python3
# suppose the task is to print square of single number given as input
# but within about 1 minute limit
from random import randrange
import time
#=== this part is used when checking the answer really
if "expected" in vars():
parts = expected.split(" ", 2)
if answer != parts[2]:
print("Answer does not match, expected: " + parts[2]); quit()
t0 = int(parts[1])
if time.time() - t0 > 65:
print("Sorry, answer should be submitted within 1 minute limit"); quit()
print("ok")
quit()
#=== below normal data "generator" follows
q = randrange(10, 100)
ans = q * q
t0 = int(time.time())
print(q)
print("#pyx %s %s" % (t0, ans))