Exemplo n.º 1
0
def evaluate_test_case(N):
    x = [random.randint(0, 10) for _ in range(N)]
    y = [random.randint(0, 10) for _ in range(N)]
    m = len(x)
    n = len(y)
    with run_algorithm(submission.source) as process:
        l = process.functions.compute(m, x, n, y)
        sol = [process.functions.element(i) for i in range(l)]
        return is_valid_solution(x, y, sol)
Exemplo n.º 2
0
def compute_fixed(correct):
    with run_algorithm(submission.source) as process:

        number_of_guess = 0

        def guess(n):
            nonlocal number_of_guess, correct  # clusure with local variable
            number_of_guess += 1
            if n == correct:
                return 0  # correct
            elif n < correct:
                return -1  # too low
            else:
                return 1  # too high

        player_answer = process.functions.play(N, callbacks=[guess])
        return player_answer == correct, number_of_guess
Exemplo n.º 3
0
def compute_moving():
    with run_algorithm(submission.source) as process:
        number_of_guess = 0
        min_value, max_value = 1, N

        def guess(n):
            nonlocal number_of_guess, min_value, max_value  # closure
            number_of_guess += 1
            if min_value == max_value and n == min_value:
                return 0  # correct, only a single possibility
            elif n < min_value or (n - min_value) <= (max_value - n):
                min_value = max(min_value, n + 1)
                return -1  # too low
            elif n > max_value or (n - min_value) > (max_value - n):
                max_value = min(max_value, n - 1)
                return 1  # too high

        player_answer = process.functions.play(N, callbacks=[guess])
        return min_value == max_value and player_answer == min_value, number_of_guess
Exemplo n.º 4
0
import random

from turingarena import *
from turingarena.evallib.algorithm import run_algorithm

all_passed = True
for _ in range(10):
    n = 20
    a = random.choices(range(10**4, 10**5), k=n)

    try:
        with run_algorithm(submission.source) as process:
            process.procedures.sort(n, a)
            b = [process.functions.get_element(i) for i in range(n)]
    except AlgorithmError as e:
        print(e)
        all_passed = False
    if b == sorted(a):
        print("correct!")
    else:
        print("WRONG!")
        all_passed = False
Exemplo n.º 5
0
n_moves = 10

move_names = {
    0: ("   rock", "\u270a"),
    1: ("  paper", "\u270b"),
    2: ("scissor", "\u270c"),
}

outcome_messages = {
    0: "draws :|",
    1: "wins! :)",
    2: "loses :(",
}

with run_algorithm(submission.player1) as p1, run_algorithm(
        submission.player2) as p2:
    players = (p1, p2)

    for p in players:
        p.procedures.start(n_moves)

    for i in range(n_moves):
        moves = [None, None]

        for j, p in enumerate(players):
            moves[j] = p.functions.play(i)

        for j, p in enumerate(players):
            outcome = (moves[j] - moves[1 - j]) % 3
            message = outcome_messages[outcome]
Exemplo n.º 6
0
def main():
    with run_algorithm(submission.source) as process:
        maxAttempts = 10
        #you can set here the maximum number of attempts the challenger can make
        att = 0
        Nguess = 0
        code = random.choices(
            range(0, 6),
            k=4)  #you can set here your random or customized secret code
        print()
        print('SECRET CODE:', code[0], '-', code[1], '-', code[2], '-',
              code[3])
        print()

        #Return the number of black pegs:
        def blackScore(c1, c2, c3, c4):
            nonlocal Nguess, code, maxAttempts
            print('Is the code', c1, '-', c2, '-', c3, '-', c4, '?')
            Nguess += 0.5
            prov = [c1, c2, c3, c4]
            ok = 0
            for i in range(4):
                if prov[i] == code[i]:
                    ok += 1
            print('Black pegs: ', ok)
            if ok == 4:
                ta.goals["correct"] = True
                #Other tasks:
                if Nguess <= 12:
                    ta.goals["correct"] = True
                    if Nguess <= 6:
                        ta.goals["perfect"] = True
                print('Right! the code is', c1, '-', c2, '-', c3, '-', c4,
                      ', you made ', Nguess, ' attempts!')
                exit()
            if Nguess >= maxAttempts:
                print('You didnt find the code!')
                exit()
            return ok

        #Return the number of white pegs:
        def whiteScore(c1, c2, c3, c4):
            nonlocal Nguess, code
            Nguess += 0.5
            prov = [c1, c2, c3, c4]
            contato = [0, 0, 0, 0]
            ok = 0
            for i in range(4):
                if prov[i] == code[i]:
                    contato[i] = 2
            for i in range(4):
                if contato[i] != 2:
                    for j in range(4):
                        if contato[j] == 0 and i != j:
                            if prov[i] == code[j]:
                                ok += 1
                                contato[j] = 1
                                break
            print('White pegs: ', ok)
            print()
            return ok

        #Call to challenger's program:
        process.functions.play(callbacks=[blackScore, whiteScore])
        print(
            'For some reason your program stopped before you find the code...')