-
Notifications
You must be signed in to change notification settings - Fork 271
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
Changes from all commits
49c1e35
cf235b1
156ead6
7eb505b
c32cc22
af3d0c3
696df6b
8850f98
a44d2ff
880c362
cc3cd60
748abe1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 @@ | ||
../accepted/boolfind-test-correct.c |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../accepted/boolfind-test-correct.java |
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 |
---|---|---|
@@ -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) |
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) |
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'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that these |
||
# | ||
# @EXPECTED_RESULTS@: CORRECT | ||
|
||
cat("Hello World!\n") |
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 |
---|---|---|
@@ -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 |
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!") |
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) |
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) |
Uh oh!
There was an error while loading. Please reload this page.