Skip to content

Instantly share code, notes, and snippets.

@diegoquintanav
Last active September 21, 2019 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save diegoquintanav/ae9be568e08b6eb4130c72d12faf765e to your computer and use it in GitHub Desktop.
Save diegoquintanav/ae9be568e08b6eb4130c72d12faf765e to your computer and use it in GitHub Desktop.
working around Jutge's evaluation method

Given the requirements.txt file

pytest
jutge

Consider the palindrome problem in palindrome.py

from jutge import read

"""Write a function that tells whether the natural number n is a palindromic number or not.

    is_palindromic(n: int) -> bool

Precondition

n is a natural number.

Observation You only need to submit the required procedure; your main program will be ignored. 

is_palindromic(12321) → true
is_palindromic(0) → true
is_palindromic(4004) → true
is_palindromic(12) → false
is_palindromic(100201) → false 
"""

# ─── METHODS ────────────────────────────────────────────────────────────────────


def is_palindromic(n: int) -> bool:
    reverse = 0
    t = n
    while t > 0:
        reverse = reverse * 10
        reverse = reverse + t % 10
        t = t // 10
    return reverse == n


# ─── JUTGE ENTRYPOINT ──────────────────────────────────────────────────────────

if __name__ == "__main__":
    n = read(int)
    while n is not None:
        print("true" if is_palindromic(n) else "false")
        n = read(int)

We can then write a test module, test_palindromes.py that imports is_palindromic from palindromes.py. Remember to add a __init__.py to your folder.

import sys
from io import StringIO

import pytest
from jutge import read

from .palindromes import is_palindromic


def palindrome_jutge():
    """Method that prints stuff and that is consumed by jutge"""
    n = read(int)
    while n is not None:
        print("true" if is_palindromic(n) else "false")
        n = read(int)

# ─── BEGIN TESTS ────────────────────────────────────────────────────────────────


@pytest.mark.parametrize(
    "n, result", [(12321, True), (0, True), (4004, True), (12, False), (100201, False)]
)
def test_is_palindromic(n, result):
    assert is_palindromic(n) is result


def test_with_jutge_format(monkeypatch, capsys):
    """Test the plain text format used by Jutge
    
    based on pytest's native fixtures, `monkeypatch` and `capsys`

    <https://docs.pytest.org/en/latest/monkeypatch.html>
    <https://docs.pytest.org/en/latest/capture.html>

    and this
    <https://www.linuxjournal.com/content/testing-your-code-pythons-pytest-part-ii>
    """

    str_inputs = StringIO("12321\n0\n4004\n12\n100201\n")
    expected_output = StringIO("true\ntrue\ntrue\nfalse\nfalse\n")
    monkeypatch.setattr("sys.stdin", str_inputs)

    # run the method, simulating an input from stdin
    palindrome_jutge()

    captured_stdout, captured_stderr = capsys.readouterr()
    assert captured_stdout == expected_output.read()

Here we test both the method and the script as required by jutge. This lets you test your scripts locally without the fuss of piping and redirecting stdin and stdout from the shell.

Test your file locally

$ pytest test_palindromes.py 
================================================================== test session starts ===================================================================
platform linux -- Python 3.7.4, pytest-5.1.2, py-1.8.0, pluggy-0.13.0
rootdir: /home/diego/ADSDB/ADS
collected 6 items                                                                                                                                        

palindromes.py ......                                                                                                        [100%]

=================================================================== 6 passed in 0.13s ====================================================================

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment