Exemplo n.º 1
0
def sample_exercise(author):
    exercise = Exercise(author=author,
                    title="#{} - New exercise".format(len(Exercise.objects)),
                    description=config.default_description,
                    boilerplate_code=config.default_boilerplate_code,
                    reference_code=config.default_boilerplate_code,
                    tags=['algorithms'])

    test = Test(input="1\n", output="42", cpu_time="100", memory_used="100").save()
    exercise.tests.append(test)
    test = Test(input="2\n", output="43", cpu_time="100", memory_used="100").save()
    exercise.published = True
    exercise.tests.append(test)

    return exercise
Exemplo n.º 2
0
def test_db():
    """ Wipes the database and initializes it with some dummy data """
    db = mongoengine.connect(config.db_name)
    db.drop_database(config.db_name)

    # Dummy users
    User.new_user(email="dummy@{}".format(config.email_domain),
                  username="******", password="******", editor=False).save()

    # Other dummmy users
    for i in xrange(8):
        User.new_user(email="dummy{}@{}".format(i + 1, config.email_domain),
                      username="******".format(i+1), password="******", editor=False).save()

    # Editor user
    editor = User.new_user(email="editor@{}".format(config.email_domain),
              username="******", password="******", editor=True).save()

    # Other editor users
    for i in xrange(8):
        User.new_user(email="editor{}@{}".format(i + 1, config.email_domain),
                      username="******".format(i+1), password="******", editor=True).save()


    # Dummy exercises
    for i in xrange(8):
        test1 = Test(input='1\n', output='1').save()
        test2 = Test(input='2\n', output='2').save()
        test3 = Test(input='2\n', output='2').save()

        exercise = Exercise(author=editor, title="Dummy exercise {}".format(i),
                            description="## This is an exercise\n\n" +
                                        "* You get a single number as an input\n" +
                                        "* Simply return that number as an output\n",
                            boilerplate_code=config.default_boilerplate_code, reference_code=config.default_boilerplate_code, tags=['sort','trees'])

        exercise.tests = [test1, test2, test3]
        exercise.published = True
        exercise.save()

    # "Doable" exercise
    exercise = Exercise(author=editor, title="Return n²",
                    description="### Return the given number to the 2 !\n\n" +
                                "* You get a\n" +
                                "* Print a²\n" +
                                "![Alt text](/static/img/cat.jpeg)",
                    boilerplate_code=config.default_boilerplate_code,
                    reference_code=config.default_boilerplate_code,
                    tags=['algorithms'])
    test1 = Test(input='1\n', output='1').save()
    test2 = Test(input='2\n', output='4').save()
    test3 = Test(input='-2\n', output='4').save()
    exercise.tests = [test1, test2, test3]
    exercise.published = True
    exercise.save()

    # Palindrome exercise
    reference_code = """
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;

struct node {
    node* next;
    int data;
    explicit node(int data):node(nullptr,data){}
    node(node* head,int data):next(head),data(data){}
};

node* insert(node* head,int data){
    return new node(head,data);
}

bool is_palindrome(node* head){
    auto temp = head;
    std::stack<node*> s;
    while(nullptr != temp){
        s.push(temp);
        temp = temp->next;
    }
    while(!s.empty()){
        if(head->data != s.top()->data){
            return false;
        }
        s.pop();
        head = head->next;
    }
    return true;
}

int main() {
    string line;
    while(getline(cin,line)){
        istringstream iss(line);
        int value;
        if(iss >> value){
            auto l = insert(nullptr,value);
            while(iss >> value){
                l = insert(l,value);
            }
            cout << is_palindrome(l) << endl;
        }
    }
    return 0;
}
    """

    boilerplate_code = """
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;

struct node {
    node* next;
    int data;
    explicit node(int data):node(nullptr,data){}
    node(node* head,int data):next(head),data(data){}
};

node* insert(node* head,int data){
    return new node(head,data);
}

bool is_palindrome(node* head){
    // Your code here
}

int main() {
    string line;
    while(getline(cin,line)){
        istringstream iss(line);
        int value;
        if(iss >> value){
            auto l = insert(nullptr,value);
            while(iss >> value){
                l = insert(l,value);
            }
            cout << is_palindrome(l) << endl;
        }
    }
    return 0;
}
    """

    exercise = Exercise(author=editor, title="Palindrome",
                description="### Trouver les palindromes !\n\n" +
                            "* Chaque ligne de l'entrée standard comprend des entiers séparés par des espaces.\n" +
                            "* Il faut transformer chaque ligne en une **liste chaînée** et déterminer si c'est un palindrome.\n" +
                            "* A chaque ligne de l'entrée standard doit correspondre une ligne de la sortie standard avec un 1 dans le cas d'un palindrome et un 0 sinon.\n" +
                            "![Alt text](http://onapo.files.wordpress.com/2009/04/palyndrome1.gif)\n\n" +
                            "**Exemples**\n\n" +
                            "Entrée :\n\n" +
                            "    1 2 3 3 2 1\n" +
                            "Sortie attendue :\n\n" +
                            "    1\n\n" +
                            "Entrée :\n\n" +
                            "    1 2 3 4 2 1\n" +
                            "    1 2 3 2 1\n\n" +
                            "Sortie attendue :\n\n" +
                            "    0\n" +
                            "    1\n\n",
                boilerplate_code=boilerplate_code,
                reference_code=reference_code,
                tags=['algorithms', 'strings', 'data-structures'],
                score=42)

    test1 = Test(input='1 2 3 3 2 1', output='1\n').save()
    test2 = Test(input='1 2 3 4 2 1\n1 2 3 2 1', output='0\n1\n').save()
    test3 = Test(input=(('1 2 3 2 1' * 10000 + '\n') * 15)[:-1], output='1\n' * 15).save()
    exercise.tests = [test1, test2, test3]
    exercise.published = True
    exercise.save()

    # Palindrome exercise with std
    reference_code = """
#include <iostream>
#include <stack>
#include <sstream>
using namespace std;

struct node {
    node* next;
    int data;
    explicit node(int data):node(nullptr,data){}
    node(node* head,int data):next(head),data(data){}
};

node* insert(node* head,int data){
    return new node(head,data);
}

bool is_palindrome(node* head){
    auto temp = head;
    std::stack<node*> s;
    while(nullptr != temp){
        s.push(temp);
        temp = temp->next;
    }
    while(!s.empty()){
        if(head->data != s.top()->data){
            return false;
        }
        s.pop();
        head = head->next;
    }
    return true;
}

int main() {
    string line;
    while(getline(cin,line)){
        istringstream iss(line);
        int value;
        if(iss >> value){
            auto l = insert(nullptr,value);
            while(iss >> value){
                l = insert(l,value);
            }
            cout << is_palindrome(l) << endl;
        }
    }
    return 0;
}
    """

    boilerplate_code = """
#include <iostream>
#include <sstream>

using namespace std;

int main() {
	string line;
	while(getline(cin,line)){
		istringstream iss(line);
		int value;
		if(iss >> value){

            /* Your code here */

			while(iss >> value){
                /* Your code here */
			}

			cout << /* Your code here */ endl;
		}
	}
	return 0;
}
    """

    exercise = Exercise(author=editor, title="Palindrome - Le retour",
                description="### Trouver les palindromes !\n\n" +
                            "* Chaque ligne de l'entrée standard comprend des entiers séparés par des espaces.\n" +
                            "* Il s'agit de déterminer si chaque liste d'entiers est un palindrome ou non.\n" +
                            "* A chaque ligne de l'entrée standard doit correspondre une ligne de la sortie standard avec un 1 dans le cas d'un palindrome et un 0 sinon.\n" +
                            "![Alt text](http://onapo.files.wordpress.com/2009/04/palyndrome1.gif)\n\n" +
                            "**Exemples**\n\n" +
                            "Entrée :\n\n" +
                            "    1 2 3 3 2 1\n" +
                            "Sortie attendue :\n\n" +
                            "    1\n\n" +
                            "Entrée :\n\n" +
                            "    1 2 3 4 2 1\n" +
                            "    1 2 3 2 1\n\n" +
                            "Sortie attendue :\n\n" +
                            "    0\n" +
                            "    1\n\n",
                boilerplate_code=boilerplate_code,
                reference_code=reference_code,
                tags=['algorithms', 'strings', 'data-structures'],
                score=42)

    test1 = Test(input='1 2 3 3 2 1', output='1\n').save()
    test2 = Test(input='1 2 3 4 2 1\n1 2 3 2 1', output='0\n1\n').save()
    test3 = Test(input=(('1 2 3 2 1' * 100000 + '\n') * 15)[:-1], output='1\n' * 15).save()
    exercise.tests = [test1, test2, test3]
    exercise.published = True
    exercise.save()

    return exercise



    return exercise