Advanced Exercise 7: The same answer was both not passed and accepted as correct
Hi, I have written code as an answer to the Advanced level of Exercise 7 “Flip the coin”, and I have carefully checked it for errors. The result varies:
1318, 1377, 1309, 1270, 1281, 1337, 1212, 1227, 1383, 1307, …
But I think this looks acceptable since the mean of the above sequence is 1302 which is fairly close to 1316.9.
However, when I tested if the code meets the criteria for a correct answer, the response was that the code was not correct.
This was a surprise that I could not believe, and I therefore chose to test the code again. The response from the second test was that the answer is correct!
Can the course staff please check the below error message and explain how this could happen? Was it just bad luck for me, or is something wrong with the critera?
I can of course provide my code if needed.
= Error message after the first Test attempt =
The answer is not correct - Test cases: 0 / 1
File "/usr/local/lib/python3.8/unittest/case.py", line 60, in testPartExecutor
yield
File "/usr/local/lib/python3.8/unittest/case.py", line 676, in run
self._callTestMethod(testMethod)
File "/usr/local/lib/python3.8/unittest/case.py", line 633, in _callTestMethod
method()
File "/app/test/test_main.py", line 29, in test_answer
self.assertLessEqual(main(p1), I1[1], higherErrorMessage)
File "/usr/local/lib/python3.8/unittest/case.py", line 1304, in assertLessEqual
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/local/lib/python3.8/unittest/case.py", line 753, in fail
raise self.failureException(msg)
Error: 384 not less than or equal to 382.0 : Your count is higher than expected. Is your code counting some additional sequences? (Tip: Check your upper bound and/or constraints)
The probability of "11111" at any given position in the sequence can be calculated as (2/3)^5 ≈ 0.13169. The number of occurrences is close to 10000 times this: 1316.9. To be more precise, the expected number of occurrences is about 0.13169 x 9996 ≈ 1316.3, because there are only 9996 places for a subsequence of length five in a sequence of 10000. The actual number will usually (in fact, with over 99% probability) be somewhere between 1230 and 1404. We check the solution allowing for an even wider margin that covers 99.99% of the cases.