Skip to content

Extra python examples, more uniform code layout with tags, #2006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example_problems/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ manual repair: be careful!

You can either manually verify the results or browse to the "Judging
verifier" page from the admin web-interface to automatically verify
the results using the `@EXPECTED_RESULTS@:` header in the sources.
the results using the `@EXPECTED_RESULTS@: ` header in the sources.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
Sample solution in Python for the "boolfind" interactive problem.

@EXPECTED_RESULTS@: CORRECT
'''

import sys

ncases = int(sys.stdin.readline())
for i in range(ncases):
n = int(sys.stdin.readline())
lo = 0
hi = n
while (lo+1 < hi):
mid = (lo+hi)//2
print(f"READ {mid}")
answer = input()
if (answer == "true"):
lo = mid
elif answer=="false":
hi = mid
else:
raise Exception(f"invalid return value '{answer}'")
print(f"OUTPUT {lo}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Sample solution in Python for the "boolfind" interactive problem.
This will test that we also accept @EXPECTED_SCORE@ as tag.

@EXPECTED_SCORE@: CORRECT
'''

import sys

ncases = int(sys.stdin.readline())
for i in range(ncases):
n = int(sys.stdin.readline())
lo = 0
hi = n
while (lo+1 < hi):
mid = (lo+hi)//2
print(f"READ {mid}")
answer = input()
if (answer == "true"):
lo = mid
elif answer=="false":
hi = mid
else:
raise Exception(f"invalid return value '{answer}'")
print(f"OUTPUT {lo}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'''
Sample solution in Python for the "boolfind" interactive problem.
This will sometimes give an off by one answer. This is a demonstration
of how to use the @EXPECTED_RESULTS@ tag.

@EXPECTED_RESULTS@: CORRECT, WRONG-ANSWER
'''

import sys, random

ncases = int(sys.stdin.readline())
for i in range(ncases):
n = int(sys.stdin.readline())
lo = 0
hi = n
while (lo+1 < hi):
mid = (lo+hi)//2
print(f"READ {mid}")
answer = input()
if (answer == "true"):
lo = mid
elif answer=="false":
hi = mid
else:
raise Exception(f"invalid return value '{answer}'")
if bool(random.getrandbits(1)):
print(f"OUTPUT {lo}")
else:
print(f"OUTPUT {lo+1}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'''
Sample solution in Python for the "boolfind" interactive problem.

This waits for one more testcase which is not specified.

@EXPECTED_RESULTS@: TIMELIMIT
'''

import sys

ncases = int(sys.stdin.readline())
for i in range(ncases+1):
n = int(sys.stdin.readline())
lo = 0
hi = n
while (lo+1 < hi):
mid = (lo+hi)//2
print(f"READ {mid}")
answer = input()
if (answer == "true"):
lo = mid
elif answer=="false":
hi = mid
else:
raise Exception(f"invalid return value '{answer}'")
print(f"OUTPUT {lo}")
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
Sample solution in Python for the "boolfind" interactive problem.

@EXPECTED_RESULTS@: WRONG-ANSWER
'''

import sys

ncases = int(sys.stdin.readline())
for i in range(ncases):
n = int(sys.stdin.readline())
lo = 0
hi = n
while (lo+1 < hi):
mid = (lo+hi)/2
print(f"READ {mid}")
answer = input()
if (answer == "true"):
lo = mid
elif answer=="false":
hi = mid
else:
raise Exception(f"invalid return value '{answer}'")
print(f"OUTPUT {lo}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This should give RUN-ERROR on the default problem 'hello', since it
# This should give CORRECT on the default problem 'hello',
# since the random extra file will not be passed.
#
# @EXPECTED_RESULTS@: CORRECT
Expand Down
12 changes: 12 additions & 0 deletions example_problems/hello/submissions/accepted/multifile-py-2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
This is a multi-file submission which produces two public classes.
Note that to successfully compile, the source file names must be
preserved to match the public class names.

@EXPECTED_RESULTS@: CORRECT
'''

import message

foo = message.Message();
foo.myPrint();
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
This is an auxiliary source file that declares another public class 'message'.
'''

class Message:
msg = ""

def __init__(self):
self.msg = "Hello world!"

def myPrint(self):
print(self.msg)
16 changes: 16 additions & 0 deletions example_problems/hello/submissions/accepted/multifile-py/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
This is a multi-file submission which produces two public classes.
Note that to successfully compile, the source file names must be
preserved to match the public class names.

@EXPECTED_RESULTS@: CORRECT
'''

from message import *

class Hello:
def __init__(self):
foo = Message();
foo.myPrint();

Hello()
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
This is an auxiliary source file that declares another public class 'message'.
'''

class Message:
msg = ""

def __init__(self):
self.msg = "Hello world!"

def myPrint(self):
print(self.msg)
5 changes: 4 additions & 1 deletion example_problems/hello/submissions/accepted/test-hello.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
cat("Hello World!\n")
# This should give CORRECT on the default problem 'hello'.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether we should remove these comments overall for correct submissions. They made sense when everything was in a single directory, but now not we are using the standard format.

I am also not sure that we should add the @EXPECTED_RESULTS@ if there exactly one. It is a relict from the pre-problem package format times.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put some in a non-standard folder just to make sure that this also does work with 1 result, and I changed this mostly to be consistent so I'm fine with removing it everywhere for the standard folders (but will than move some solutions to mixed-verdicts for the check)

Removing the comment is fine by me, I might have a slight preference for the comment as it gives more information than having to look in the file explorer if this is a failing submission without comment or that this is a correct submission so it does not have a comment. I don't care much for it though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these @EXPECTED_RESULTS@ tags are useful, both when viewing the file as @vmcj already mentioned, but also because it means that this meta-data is now included in the source, so you can separately submit/import the file as jury and it will still pick up this information.

#
# @EXPECTED_RESULTS@: CORRECT

cat("Hello World!\n")
7 changes: 6 additions & 1 deletion example_problems/hello/submissions/accepted/test-hello.swift
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
print("Hello World!")
/*
* This should give CORRECT on the default problem 'hello'.
*
* @EXPECTED_RESULTS@: CORRECT
*/

print("Hello World!")
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* This should fail submission because of multiple @EXPECTED_RESULTS@ tags.
* This should fail submission because of multiple @EXPECTED_RESULTS@: tags.
*
* @EXPECTED_RESULTS@: CORRECT, NO-OUTPUT
*
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This should fail with NO-OUTPUT.
* The EXPECTED_RESULT tag in expected_result.txt file should be used.
* The @EXPECTED_RESULTS@ tag in expected_result.txt file should be used.
*
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'''
This should fail with RUN-ERROR and tests if we're properly in a chroot.
This tries to escape the chroot, which works when we run as root.
@EXPECTED_RESULTS@: RUN_TIME_ERROR
'''

import os

def is_chroot():
sl_stat = os.stat('/')
pr_stat = os.stat('/proc/1/root/.')

return (sl_stat.st_ino != pr_stat.st_ino ) and (sl_stat.st_dev != pr_stat.st_dev )

# We should be in the chroot
if is_chroot():
# We run as non-root, this should fail
os.chroot('/proc/1/root')
if is_chroot():
# We should still be in the chroot
os.exit(1)

# If we get here we failed
print("Hello world!")
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

# This should fail with RUN-ERROR and tests if we're properly in a chroot.
# This tries to escape the chroot, which works when we run as root.
# @EXPECTED_RESULTS@: RUN_TIME_ERROR

ischroot
if [ ischroot ]; then
# We expect to be in the jail
chroot /proc/1/root
if [ ischroot ]; then
# We expect to still be in the jail
exit 1
else
echo "Hello world!"
fi
else
echo "Hello world!"
fi
13 changes: 13 additions & 0 deletions example_problems/hello/submissions/run_time_error/test-chroot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'''
This should fail with RUN-ERROR and tests if we're properly in a chroot
@EXPECTED_RESULTS@: RUN_TIME_ERROR
'''

import os

sl_stat = os.stat('/')
pr_stat = os.stat('/proc/1/root/.')

if (sl_stat.st_ino != pr_stat.st_ino ) and (sl_stat.st_dev != pr_stat.st_dev ):
os.exit(1)
print("Hello world!")
10 changes: 10 additions & 0 deletions example_problems/hello/submissions/run_time_error/test-chroot.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh

# This should fail with RUN-ERROR and tests if we're properly in a chroot
# @EXPECTED_RESULTS@: RUN_TIME_ERROR

if [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then
exit 1
else
echo "Hello world!"
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'''
@EXPECTED_RESULTS@: RUN-ERROR
'''

exit(1)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'''
@EXPECTED_RESULTS@: RUN-ERROR
'''

raise Exception("Expected failure")
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'''
@EXPECTED_RESULTS@: RUN-ERROR
'''
storage = []

while True:
some_str = ' ' * bytearray(512000000)
storage.append(some_str)
10 changes: 10 additions & 0 deletions example_problems/hello/submissions/run_time_error/test-sigfpe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'''
This should fail with RUN-ERROR due to integer division by zero.

@EXPECTED_RESULTS@: RUN-ERROR
'''

a = 0
b = 10 / a

print(b)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'''
This should fail with TIMELIMIT as the program is left running.
This tests that a currently correct output does not lead to a CORRECT.

@EXPECTED_RESULTS@: TIMELIMIT
'''

import time

print("Hello world!")
while True:
time.sleep(60*60*24)