0

I have a beginner Python program that generates random output and does not take any input. For example, it simulates rolling a dice 5 times and prints the results, their sum, and average:

import random

total = 0
rolls = ""

for i in range(5):
    roll = random.randint(1, 6)
    rolls += str(roll) + " "

print(rolls)
print(total)
print(total / 5)

How can I create valid test cases in Polygon for a Python program that:

Does not read input

Produces random output

…without modifying thecode?

3
  • 1
    In case this is not on purpose (e.g. for making your test failing): you are missing the sum of each roll into total variable. Commented Oct 2 at 13:29
  • I removed the polygon tag because that refers to the geometric notion of polygon. What is "Polygon" in this context? Is there some testing framework for Python called "Polygon"? (I'm not seeing anything that looks relevant on PyPI.) Commented Oct 2 at 13:38
  • 1
    Ah, I see. It's likely polygon.codeforces.com Commented Oct 2 at 13:39

2 Answers 2

2

When randomness is involved in output of a method, instead of checking values, you can validate the structure of the output. For your example, the validation method would check that:

  1. 3 lines are returned
  2. 1st line contains exactly 5 numbers which all are from 1 to 6
  3. 2nd line is total of each 5 numbers from previous line
  4. 3rd line is 2nd line divided by 5
Sign up to request clarification or add additional context in comments.

Comments

0

You can do it in 2 ways, one is simpler and another one is advanced.

so 1st, fix randomness with seed.

like you can add line in code that is,

random.seed(0)

below import random.

Now the output is always the same, so Polygon can compare it against a fixed .ans file.

Then:

  • You create one empty .in file (since your code takes no input).

  • Run your program to get .out file.

  • Upload both to Polygon.

2nd, use checker

If you truly want randomness to remain, you can:

  • Create a custom checker that verifies that the output is valid, not identical to expected.

For example, for a dice roll program:

  • The checker ensures there are 5 integers between 1–6,

  • and the average and sum are consistent.

This means your .ans file can be empty (since the output varies), but the checker validates correctness logically.

In Polygon:

  1. Leave input files empty (.in).

  2. Don’t upload .ans (or keep it empty).

  3. Write a checker program in C++/Python that parses the contestant’s output and validates the format and logic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.