|
| 1 | +# This checks if we can write extra files |
| 2 | +# between different testcase runs. R uses |
| 3 | +# a temporary directory which DOMjudge should |
| 4 | +# make unavailable between runs and in case this |
| 5 | +# is forgotten teams can precalculate extra files for later |
| 6 | +# testcases. We need a working solution to make sure |
| 7 | +# we get to all testcases. |
| 8 | +# |
| 9 | +# This submission is symlinked to make sure we submit it twice |
| 10 | +# to verify that we can't see the TMPDIR of the earlier submission. |
| 11 | +# |
| 12 | +# The script will be WRONG-ANSWER if any of the assumptions |
| 13 | +# are wrong. |
| 14 | +# The script will be NO-OUTPUT if there is a security violation. |
| 15 | +# The verdict RUN-ERROR is expected if we correctly detect the |
| 16 | +# violation and would be the correct verdict if we don't need a |
| 17 | +# TMPDIR. |
| 18 | +# |
| 19 | +# @EXPECTED_RESULTS@: CORRECT |
| 20 | + |
| 21 | +# For R the TMPDIR needs to be set, if it's not |
| 22 | +# we can skip this whole test. |
| 23 | +tmpdir<-Sys.getenv(c("TMPDIR")) |
| 24 | +if (tmpdir == '') { |
| 25 | + print("TMPDIR not set, update installation instructions.") |
| 26 | + quit() |
| 27 | +} |
| 28 | + |
| 29 | +# We had 3 testcases in the past, try with double for when new testcases get added. |
| 30 | +testcaseIds<-rep(1:6) |
| 31 | +possibleTempDirs <- (function (x) sprintf("/testcase%05d/write_tmp", x))(testcaseIds) |
| 32 | +if (!(tmpdir %in% possibleTempDirs)) { |
| 33 | + print("Either TMPDIR format has changed, or too many testcases.") |
| 34 | + print(sprintf("Current TMPDIR: %s", tmpdir)) |
| 35 | + quit() |
| 36 | +} |
| 37 | + |
| 38 | +for (possibleTempDir in possibleTempDirs) { |
| 39 | + teamFile<-sprintf("%s/fileFromTeam.txt", possibleTempDir) |
| 40 | + if (file.exists(teamFile)) { |
| 41 | + # File should not be here |
| 42 | + quit() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +# Try to write to our TMPDIR to read it in the next testcase. |
| 47 | +currentTeamFile<-sprintf("%s/fileFromTeam.txt", tmpdir) |
| 48 | +fileConn<-file(currentTeamFile) |
| 49 | +writeLines(c("Calculate","all","floats"), fileConn) |
| 50 | +close(fileConn) |
| 51 | +# Make sure our test from earlier does work for this testcase |
| 52 | +if (!(file.exists(currentTeamFile))) { |
| 53 | + # File should be here |
| 54 | + quit() |
| 55 | +} |
| 56 | + |
| 57 | +# Now try the actual problem to advance to the next testcase. |
| 58 | +input<-file("stdin") |
| 59 | +lines<-readLines(input) |
| 60 | + |
| 61 | +# https://evolutionarygenetics.github.io/Chapter10.html |
| 62 | +# a simple reciprocal example |
| 63 | +reciprocal <- function(x){ |
| 64 | + # calculate the reciprocal |
| 65 | + y <- 1/x |
| 66 | + return(y) |
| 67 | +} |
| 68 | + |
| 69 | +for (l in lines[-1]) { |
| 70 | + l<-as.numeric(l) |
| 71 | + r<-reciprocal(l) |
| 72 | + cat(r,"\n") |
| 73 | +} |
0 commit comments