예제 #1
0
    def test_problem(self):
        """test the problem table"""
        # add problem type
        PROBLEM_TYPE_ARGS = {
            "name": "input/output",
            "eval_script": "#!/bin/bash\nexit 0",
        }
        problem_type = model.ProblemType(**PROBLEM_TYPE_ARGS)
        db_session.add(problem_type)
        db_session.commit()

        PROBLEM_ARGS = {
            "problem_type": problem_type,
            "slug": "ioprob",
            "name": "The Input/Output Problem",
            "problem_statement": "Print the string 'Hello, World!' n times",
            "sample_input": "3",
            "sample_output": "Hello, World!Hello, World!Hello, World!",
            "secret_input": "4",
            "secret_output": "Hello, World!Hello, World!Hello, World!Hello, World!",
        }

        # create and add contest
        problem = model.Problem(**PROBLEM_ARGS)
        db_session.add(problem)
        db_session.commit()

        # fetch contest
        results = model.Problem.query.filter_by(name=PROBLEM_ARGS["name"]).all()

        self.assertEqual(len(results), 1)
예제 #2
0
def problems_batch_upload():
    """
    Batch adds or updates problems from zip files.

    Params:
        None

    Returns:
        a redirect to the problem view page
    """
    if request.method == "POST":  # process added/edited problem
        input_output = model.ProblemType.query.filter_by(
            name="input-output").one()

        problem_zip_files = request.files.getlist("problems")

        for problem_zip_file in problem_zip_files:
            data = extract_problem_data(problem_zip_file)

            if is_dup_problem_slug(data["slug"]):  # update
                problem = model.Problem.query.filter_by(
                    slug=data["slug"]).one()
                problem.problem_type = input_output
                problem.name = data["name"]
                problem.problem_statement = data["problem_statement"]
                problem.sample_input = data["sample_input"]
                problem.sample_output = data["sample_output"]
                problem.secret_input = data["secret_input"]
                problem.secret_output = data["secret_output"]
            else:  # add
                problem = model.Problem(
                    problem_type=input_output,
                    slug=data["slug"],
                    name=data["name"],
                    problem_statement=data["problem_statement"],
                    sample_input=data["sample_input"],
                    sample_output=data["sample_output"],
                    secret_input=data["secret_input"],
                    secret_output=data["secret_output"],
                )
                db_session.add(problem)

        db_session.commit()

        return redirect(url_for("problems.problems_view"))
    else:
        current_app.logger.info(
            "invalid problem batch upload request method: %s", request.method)
        abort(400)
예제 #3
0
def setup_contest():
    roles = {x.name: x for x in model.UserRole.query.all()}
    test_contestant = model.User(
        "testuser", "Test User", "pass", user_roles=[roles["defendant"]]
    )
    test_executioner = model.User(
        "testexec", "Test Executioner", "epass", user_roles=[roles["executioner"]]
    )
    test_contest = model.Contest(
        "test_contest",
        util.str_to_dt("2017-02-05T22:04:00Z"),
        util.str_to_dt("2030-01-01T11:11:00Z"),
        True,
    )
    io_problem_type = model.ProblemType.query.filter_by(name="input-output").one()
    test_problem = model.Problem(
        io_problem_type,
        "fizzbuzz",
        "FizzBuzz",
        "## FizzBuzz\nPerform fizzbuzz up to the given number",
        "3",
        "1\n2\nFizz",
        "15",
        "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\n9\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n",
    )
    test_contest.problems.append(test_problem)
    test_contest.users.append(test_contestant)

    python = model.Language.query.filter_by(name="python").one()
    test_run = model.Run(
        test_contestant,
        test_contest,
        python,
        test_problem,
        util.str_to_dt("2017-02-05T23:00:00Z"),
        'import sys\nn=raw_input()\nfor i in range(1, n+1): print("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or i)',
        test_problem.secret_input,
        test_problem.secret_output,
        True,
    )

    db_session.add_all(
        [test_executioner, test_contestant, test_contest, test_problem, test_run]
    )
    db_session.commit()
예제 #4
0
def get_problem():
    """returns a test Problem"""
    problem_type_args, problem_type = get_problem_type()
    PROBLEM_ARGS = {
        "problem_type": problem_type,
        "slug": "ioprob",
        "name": "The Input/Output Problem",
        "problem_statement": "Print the string 'Hello, World!' n times",
        "sample_input": "3",
        "sample_output": "Hello, World!Hello, World!Hello, World!",
        "secret_input": "4",
        "secret_output": "Hello, World!Hello, World!Hello, World!Hello, World!",
    }

    problem = model.Problem(**PROBLEM_ARGS)
    db_session.add(problem)
    db_session.commit()

    return PROBLEM_ARGS, problem
예제 #5
0
    def test_clar_crud(self):
        test_prob = model.Problem(
            model.ProblemType.query.filter_by(name="input-output").one(),
            "initprob",
            "init_problem_name",
            "## is there a problem here",
            "1",
            "2",
            "1 2 3",
            "4 5 6",
        )
        db_session.add(test_prob)
        db_session.commit()

        init_clar_problem = model.Problem.query.first().name
        init_clar_subject = "the test"
        init_clar_user = model.User.query.first()

        self.login("admin", "pass")

        self._clar_add(init_clar_problem, init_clar_subject, init_clar_user)
        self._clar_answer(init_clar_problem, init_clar_subject)
        self._clar_del(init_clar_subject)
예제 #6
0
파일: web.py 프로젝트: a-hodges/code_court
def dev_populate_db():
    """Performs the initial database setup for the application
    """
    current_app.logger.info("Initializing tables with dev data")
    roles = {x.name: x for x in model.UserRole.query.all()}

    db_session.add_all([
        model.User(
            "*****@*****.**",
            "SuperUser",
            "pass",
            user_roles=list(roles.values())),
        model.User(
            "*****@*****.**",
            "ObserverUser",
            "pass",
            user_roles=[roles['observer']])
    ])

    contestants = []
    names = [
        "Fred", "George", "Jenny", "Sam", "Jo", "Joe", "Sarah", "Ben",
        "Josiah", "Micah"
    ]
    for i in range(1, 5):
        test_contestant = model.User(
            "testuser{}@example.org".format(i),
            names[i - 1],
            "pass",
            user_roles=[roles['defendant']])
        db_session.add(test_contestant)
        contestants.append(test_contestant)

    # create test contest
    now = datetime.datetime.utcnow()
    test_contest = model.Contest(
        name="test_contest",
        start_time=now,
        end_time=now + datetime.timedelta(minutes=30),
        is_public=True,
        activate_time=now,
        freeze_time=None,
        deactivate_time=None)
    test_contest.users += contestants
    db_session.add(test_contest)

    io_problem_type = model.ProblemType.query.filter_by(
        name="input-output").one()
    problems = []

    hello_world = model.Problem(io_problem_type, "hello-world",
                                "Hello, World!",
                                'Print the string "Hello, World!"', "",
                                "Hello, World!", "", "Hello, World!")
    problems.append(hello_world)
    test_contest.problems.append(hello_world)
    db_session.add(hello_world)

    hello_worlds = model.Problem(
        io_problem_type, "hello-worlds", "Hello, Worlds!",
        'Print the string "Hello, World!" n times', "2",
        "Hello, World!\nHello, World!", "5",
        "Hello, World!\nHello, World!\nHello, World!\nHello, World!\nHello, World!\n"
    )
    problems.append(hello_worlds)
    test_contest.problems.append(hello_worlds)
    db_session.add(hello_worlds)

    fizzbuzz = model.Problem(
        io_problem_type, "fizzbuzz", "FizzBuzz",
        "Perform fizzbuzz up to the given number\n\nMore info can be found [here](https://en.wikipedia.org/wiki/Fizz_buzz)",
        "3", "1\n2\nFizz", "15",
        "1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzBuzz\n"
    )
    problems.append(fizzbuzz)
    test_contest.problems.append(fizzbuzz)
    db_session.add(fizzbuzz)

    fibonacci = model.Problem(
        io_problem_type, "fibonoacci", "Fibonacci",
        "Give the nth number in the Fibonacci sequence", "4", "3", "5",
        "5")
    problems.append(fibonacci)
    test_contest.problems.append(fibonacci)
    db_session.add(fibonacci)

    ext_fibonacci = model.Problem(
        io_problem_type, "ext-fib", "Extended Fibonacci",
        "Give the the numbers of the Fibonacci sequence between 0 and n, inclusive.\nIf n is positive, the range is [0,n].\nIf n is negative, the range is [n,0].",
        "-3", "2\n-1\n1\n0", "-5", "5\n-3\n2\n-1\n1\n0")
    problems.append(ext_fibonacci)
    test_contest.problems.append(ext_fibonacci)
    db_session.add(ext_fibonacci)

    # insert submissions
    python = model.Language.query.filter_by(name="python").one()

    solutions = {
        "Hello, World!":
        "print('Hello, World!')",
        "Hello, Worlds!":
        "for i in range(int(input())):\n\tprint('Hello, World!')",
        "FizzBuzz":
        'print("\\n".join("Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i) for i in range(1,int(input())+1)))',
        "Fibonacci":
        "fib = lambda n: n if n < 2 else fib(n-1) + fib(n-2)\nprint(fib(int(input())))",
        "Extended Fibonacci":
        "print('5\\n-3\\n2\\n-1\\n1\\n0')"
    }

    problem_subs = []
    for problem in problems:
        for user in contestants:
            for _ in range(2):
                problem_subs.append((problem, user))

    random.shuffle(problem_subs)

    for problem, user in problem_subs:
        src_code = solutions[problem.name]
        is_submission = random.randint(1, 7) != 5

        is_priority = random.randint(1, 9) == 7
        is_correct = random.randint(1, 2) == 2
        if not is_correct:
            src_code = src_code + "\nprint('Wait this isn\\'t correct')"

        test_run = model.Run(user, test_contest, python, problem,
                             datetime.datetime.utcnow(), src_code,
                             problem.secret_input, problem.secret_output,
                             is_submission)
        test_run.is_correct = is_correct
        test_run.is_priority = is_priority
        test_run.state = "Judging"

        db_session.add(test_run)
    db_session.commit()
예제 #7
0
def add_problem():
    """
    Adds or edits a problem

    Note:
        must be called from within a request context

    Returns:
        a redirect to the problem view page
    """
    problem_type_id = request.form.get("problem_type_id")
    problem_type = model.ProblemType.query.filter_by(
        id=int(problem_type_id)).one()
    slug = request.form.get("slug")
    name = request.form.get("name")
    is_enabled = request.form.get("is_enabled")
    problem_statement = request.form.get("problem_statement")
    sample_input = request.form.get("sample_input")
    sample_output = request.form.get("sample_output")
    secret_input = request.form.get("secret_input")
    secret_output = request.form.get("secret_output")

    if problem_type is None:
        error = "Failed to add problem due to undefined problem_type."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    if name is None:
        error = "Failed to add problem due to undefined problem name."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    is_enabled_bool = util.checkbox_result_to_bool(is_enabled)
    if is_enabled_bool is None:
        error = "Failed to add problem \'{}\' due to invalid is_enabled check.".format(
            name)
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    if slug is None:
        error = "Failed to add problem due to undefined problem slug."
        current_app.logger.info(error)
        flash(error, "danger")
        return redirect(url_for("problems.problems_view"))

    problem_id = request.form.get('problem_id')
    if problem_id:  # edit
        problem = model.Problem.query.filter_by(id=int(problem_id)).one()
        problem.problem_type = problem_type
        problem.slug = slug
        problem.name = name
        problem.is_enabled = is_enabled_bool
        problem.problem_statement = problem_statement
        problem.sample_input = sample_input
        problem.sample_output = sample_output
        problem.secret_input = secret_input
        problem.secret_output = secret_output
    else:  # add
        # check if is duplicate
        if is_dup_problem_slug(slug):
            error = "Failed to add problem \'{}\' as problem already exists.".format(
                slug)
            current_app.logger.info(error)
            flash(error, "danger")
            return redirect(url_for("problems.problems_view"))

        problem = model.Problem(problem_type, slug, name, problem_statement,
                                sample_input, sample_output, secret_input,
                                secret_output)
        problem.is_enabled = is_enabled_bool
        db_session.add(problem)

    db_session.commit()

    return redirect(url_for("problems.problems_view"))
예제 #8
0
import sys
import display_master as disp
import model
import solver

if __name__ == "__main__":
    if len(sys.argv) > 0:
        pb = model.Problem("vols_3.txt")
        x, t = solver.solve_model_pulp(pb)
        pb.make_array_after_plne(x, t)

        print(pb.decision_t)
        print()

        for v in range(len(pb.vehicles)):
            print(pb.vehicles[v], '\n')
            print(pb.decision_x[v])
        pb.fleet_from_plne(x, t)
        disp.display_planning_per_vehicle(pb.vehicles, pb)