Пример #1
0
def show_parens():
    """

    Returns:

    """
    message = "Make sure you add parenthesis to <code>plt.show</code>"
    code = "show_parens"
    tldr = "Incorrect Show"
    match = find_match("plt.show")
    match2 = find_match("plt.show()")

    if match and not match2:
        return gently(message, label=code, title=tldr)
    return False
Пример #2
0
def wrong_duplicate_var_in_add():
    match = find_match("_item_ + _item_")
    if match:
        explain('You are adding the same variable twice; you need two different variables in your addition.'
                '<br><br><i>(dup_var)<i></br>')
        return True
    return False
Пример #3
0
def show_parens():
    message = "Make sure you add parenthesis to <code>plt.show</code>"
    code = "show_parens"
    tldr = "Incorrect Show"
    if not find_match("plt.show"):
        return gently_r()
    return False
Пример #4
0
def wrong_print_9_1():
    """

    std_ast = parse_program()
    for_loops = std_ast.find_all('For')
    # has_for = len(for_loops) > 0
    for_loc = []
    wrong_print_placement = True
    for loop in for_loops:
        end_node = loop.next_tree
        if end_node is not None:
            for_loc.append(end_node.lineno)
    calls = std_ast.find_all('Call')
    for call in calls:
        if call.func.id == 'print':
            for loc in for_loc:
                if call.func.lineno >= loc:
                    wrong_print_placement = False
                    break
            if not wrong_print_placement:
                break
    if wrong_print_placement:
        explain('The output of the total rainfall amount is not in the correct place. The total rainfall should be '
                'output only once after the total rainfall has been computed.<br><br><i>(print_9.1)<i></br>')
    Returns:
    """
    match = find_match("for _item_ in _list_:\n" "    pass\n" "print(_total_)")
    if not match:
        explain(
            'The output of the total rainfall amount is not in the correct place. The total rainfall should be '
            'output only once after the total rainfall has been computed.<br><br><i>(print_9.1)<i></br>'
        )
        return True
    return False
Пример #5
0
def wrong_accumulator_initialization_9_1():
    match = find_match("rainfall_sum = 0")
    if not match:
        explain('The variable to hold the total value of the rainfall amounts (<code>rainfall_sum</code>) is not '
                'initialized properly.<br><br><i>(accu_init_9.1)<i></br>')
        return True
    return False
Пример #6
0
def wrong_print_9_1():
    """

    std_ast = parse_program()
    for_loops = std_ast.find_all('For')
    # has_for = len(for_loops) > 0
    for_loc = []
    wrong_print_placement = True
    for loop in for_loops:
        end_node = loop.next_tree
        if end_node is not None:
            for_loc.append(end_node.lineno)
    calls = std_ast.find_all('Call')
    for call in calls:
        if call.func.id == 'print':
            for loc in for_loc:
                if call.func.lineno >= loc:
                    wrong_print_placement = False
                    break
            if not wrong_print_placement:
                break
    if wrong_print_placement:
        explain('The output of the total rainfall amount is not in the correct place. The total rainfall should be '
                'output only once after the total rainfall has been computed.<br><br><i>(print_9.1)<i></br>')
    Returns:
    """
    match = find_match("for _item_ in _list_:\n"
                       "    pass\n"
                       "print(_total_)")
    if not match:
        explain('The output of the total rainfall amount is not in the correct place. The total rainfall should be '
                'output only once after the total rainfall has been computed.<br><br><i>(print_9.1)<i></br>')
        return True
    return False
Пример #7
0
def missing_no_print():
    prints = find_match('print(___)', cut=True)
    if not prints:
        explain(
            'Program does not output anything.<br><br><i>(no_print)<i></br>')
        return True
    return False
Пример #8
0
def wrong_list_initialization_9_1():
    match = find_match('rainfall_list = weather.get("Precipitation","Location","Blacksburg, VA")')
    if not match:
        explain('The list of rainfall amounts (<code>rainfall_list</code>) is not initialized properly.'
                '<br><br><i>(list_init_9.1)<i></br>')
        return True
    return False
Пример #9
0
def wrong_iteration_body_8_3():
    match = find_match("for _item_ in _list_:\n"
                       "    sum_length = ___ + ___\n")
    if not match:
        explain('The addition of each episode length to the total length is not in the correct place.<br><br><i>'
                '(iter_body_8.3)<i></br>')
        return True
    return False
Пример #10
0
def wrong_accumulator_initialization_9_1():
    match = find_match("rainfall_sum = 0")
    if not match:
        explain(
            'The variable to hold the total value of the rainfall amounts (<code>rainfall_sum</code>) is not '
            'initialized properly.<br><br><i>(accu_init_9.1)<i></br>')
        return True
    return False
Пример #11
0
def wrong_duplicate_var_in_add():
    message = "You are adding the same variable twice; you need two different variables in your addition."
    code = "dup_var"
    tldr = "Duplicate Division"
    match = find_match("_item_ + _item_")
    if match:
        return explain_r(message, code, label=tldr)
    return False
Пример #12
0
def wrong_for_inside_if():
    match = find_match("if ___:\n" "    for ___ in ___:\n" "        pass")
    if match:
        explain(
            'The iteration should not be inside the decision block.<br><br><i>(for_in_if)<i></br>'
        )
        return True
    return False
Пример #13
0
def wrong_duplicate_var_in_add():
    match = find_match("_item_ + _item_")
    if match:
        explain(
            'You are adding the same variable twice; you need two different variables in your addition.'
            '<br><br><i>(dup_var)<i></br>')
        return True
    return False
Пример #14
0
def missing_no_print():
    message = "Program does not output anything."
    code = "no_print"
    tldr = "Missing Output"
    prints = find_match('print(___)', cut=True)
    if not prints:
        return explain_r(message, code, label=tldr)
    return False
Пример #15
0
def wrong_for_inside_if():
    match = find_match("if ___:\n"
                       "    for ___ in ___:\n"
                       "        pass")
    if match:
        explain('The iteration should not be inside the decision block.<br><br><i>(for_in_if)<i></br>')
        return True
    return False
Пример #16
0
def wrong_list_initialization_9_1():
    message = "The list of rainfall amounts (<code>rainfall_list</code>) is not initialized properly."
    code = "list_init_9.1"
    tldr = "Incorrect List Initialization"
    match = find_match('rainfall_list = weather.get("Data.Precipitation","Station.Location","Blacksburg, VA")')
    if not match:
        return explain_r(message, code, label=tldr)
    return False
Пример #17
0
def missing_target_slot_empty():
    match = find_match("for _item_ in ___:\n    pass")
    if match:
        _item_ = match["_item_"].astNode
        if _item_.id == "___":
            explain("You must fill in the empty slot in the iteration.<br><br><i>(target_empty)<i></br>")
            return True
    return False
Пример #18
0
def list_not_initialized_on_run():
    match = find_match("for ___ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"][0].astNode
        if def_use_error(_item_):
            explain("The list in your for loop has not been initialized<br><br><i>(no_list_init)<i></br>")
            return True
    return False
Пример #19
0
def wrong_iteration_body_8_3():
    match = find_match("for _item_ in _list_:\n"
                       "    sum_length = ___ + ___\n")
    if not match:
        explain(
            'The addition of each episode length to the total length is not in the correct place.<br><br><i>'
            '(iter_body_8.3)<i></br>')
        return True
    return False
Пример #20
0
def wrong_iterator_not_list():
    match = find_match("for ___ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"].astNode
        if not data_state(_item_).was_type('list'):
            explain("The variable <code>{0!s}</code> has been set to something that is not a list but is placed in the "
                    "iteration block that must be a list.<br><br><i>(iter_not_list)<i></br>".format(_item_.id))
            return True
    return False
Пример #21
0
def wrong_print_8_3():
    match = find_match("for _item_ in _list_:\n" "    pass\n" "print(_total_)")
    if not match:
        explain(
            'The output of the total length of time is not in the correct place. The total length of time should be'
            ' output only once after the total length of time has been computed.<br><br><i>(print_8.3)<i></br>'
        )
        return True
    return False
Пример #22
0
def wrong_list_repeated_in_for():
    match = find_match("for _item_ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"].astNode
        if data_state(_item_).was_type('list'):
            explain('The <code>{0!s}</code> variable can only appear once in the "for" block <br><br><i>'
                    '(list_repeat)<i></br>'.format(_item_.id))
            return True
    return False
Пример #23
0
def wrong_target_is_list():
    match = find_match("for _item_ in ___:\n    pass")
    if match:
        _item_ = match["_item_"].astNode
        if data_state(_item_).was_type('list'):
            explain('The variable <code>{0!s}</code> is a list and should not be placed in the iteration variable slot'
                    ' of the "for" block<br><br><i>(target_is_list)<i></br>.'.format(_item_.id))
            return True
    return False
Пример #24
0
def missing_for_slot_empty():
    match = find_match("for _item_ in _list_:\n    pass")
    if match:
        _item_ = match["_item_"][0].astNode
        _list_ = match["_list_"][0].astNode
        if _item_.id == "___" or _list_.id == "___":
            explain("You must fill in the empty slot in the iteration.<br><br><i>(for_incomplete)<i></br>")
            return True
    return False
Пример #25
0
def wrong_print_8_3():
    match = find_match("for _item_ in _list_:\n"
                       "    pass\n"
                       "print(_total_)")
    if not match:
        explain('The output of the total length of time is not in the correct place. The total length of time should be'
                ' output only once after the total length of time has been computed.<br><br><i>(print_8.3)<i></br>')
        return True
    return False
Пример #26
0
def all_labels_present():  # TODO: make sure it's before the show, maybe check for default values
    """
    plt.title("Distribution of Number of Sentences in Long Books")
    plt.xlabel("Number of Sentences")
    plt.ylabel("Number of Long Books")
    plt.show()
    Returns:
    """

    match = find_match("plt.title(___)\nplt.show()")
    match02 = find_match("plt.xlabel(___)\nplt.show()")
    match03 = find_match("plt.ylabel(___)\nplt.show()")

    if (not match) or (not match02) or (not match03):
        gently('Make sure you supply labels to all your axes and provide a title and then call show'
               '<br><br><i>(labels_present)<i></br>')
        return True
    return False
Пример #27
0
def wrong_print_9_2():
    match = find_match("for _item_ in _list_:\n" "    pass\n" "print(_total_)")
    if not match:
        explain(
            'The output of the total number of days with rainfall is not in the correct place. The total number of '
            'days should be output only once after the total number of days has been computed.<br><br><i>'
            '(print_9.2)<i></br>')
        return True
    return False
Пример #28
0
def list_initialization_misplaced():
    match = find_match("for ___ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"][0].astNode
        if data_state(_item_).was_type('list') and def_use_error(_item_):
            explain("Initialization of <code>{0!s}</code> is a list but either in the wrong place or redefined"
                    "<br><br><i>(list_init_misplaced)<i></br>".format(_item_.id))
            return True
    return False
Пример #29
0
def wrong_accumulator_initialization_9_1():
    message = ("The variable to hold the total value of the rainfall amounts (<code>rainfall_sum</code>) "
               "is not initialized properly.")
    code = "accu_init_9.1"
    tldr = "Incorrect Accumulation Variable initialization"
    match = find_match("rainfall_sum = 0")
    if not match:
        return explain_r(message, code, label=tldr)
    return False
Пример #30
0
def wrong_list_initialization_placement_9_1():
    match = find_match("rainfall_list = ___\n"
                       "for _item_ in _list_:\n"
                       "    pass")
    if not match:
        explain('The list of rainfall amount (<code>rainfall_list</code>) must be initialized before the iteration that'
                ' uses this list.<br><br><i>(list_init_place_9.1)<i></br>')
        return True
    return False
Пример #31
0
def all_labels_present():  # TODO: make sure it's before the show, maybe check for default values
    """
    plt.title("Distribution of Number of Sentences in Long Books")
    plt.xlabel("Number of Sentences")
    plt.ylabel("Number of Long Books")
    plt.show()
    Returns:
    """
    message = "Make sure you supply labels to all your axes and provide a title and then call show"
    code = "labels_present"
    tldr = "Missing Label(s)"
    match = find_match("plt.title(___)\nplt.show()")
    match02 = find_match("plt.xlabel(___)\nplt.show()")
    match03 = find_match("plt.ylabel(___)\nplt.show()")

    if (not match) or (not match02) or (not match03):
        return gently_r(message, code, label=tldr)
    return False
Пример #32
0
def wrong_iteration_body_8_3():
    message = "The addition of each episode length to the total length is not in the correct place."
    code = "iter_body_8.3"
    tldr = "Accumulation Misplaced"
    match = find_match("for _item_ in _list_:\n"
                       "    sum_length = ___ + ___\n")
    if not match:
        return explain_r(message, code, label=tldr)
    return False
Пример #33
0
def wrong_list_repeated_in_for():
    message = 'The <code>{0!s}</code> variable can only appear once in the "for" block.'
    code = "list_repeat"
    tldr = "Duplicate Iteration Variable"
    match = find_match("for _item_ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"].astNode
        if data_state(_item_).was_type('list'):
            return explain_r(message.format(_item_.id), code, label=tldr)
    return False
Пример #34
0
def missing_target_slot_empty():
    message = "You must fill in the empty slot in the iteration."
    code = "target_empty"
    tldr = "Missing Iteration Variable"
    match = find_match("for _item_ in ___:\n    pass")
    if match:
        _item_ = match["_item_"].astNode
        if _item_.id == "___":
            return explain_r(message, code, label=tldr)
    return False
Пример #35
0
def wrong_list_initialization_9_1():
    match = find_match(
        'rainfall_list = weather.get("Precipitation","Location","Blacksburg, VA")'
    )
    if not match:
        explain(
            'The list of rainfall amounts (<code>rainfall_list</code>) is not initialized properly.'
            '<br><br><i>(list_init_9.1)<i></br>')
        return True
    return False
Пример #36
0
def wrong_print_9_2():
    match = find_match("for _item_ in _list_:\n"
                       "    pass\n"
                       "print(_total_)")
    if not match:
        explain('The output of the total number of days with rainfall is not in the correct place. The total number of '
                'days should be output only once after the total number of days has been computed.<br><br><i>'
                '(print_9.2)<i></br>')
        return True
    return False
Пример #37
0
def list_not_initialized_on_run():
    message = "The list in your for loop has not been initialized."
    code = "no_list_init"
    tldr = "List Variable Uninitialized"
    match = find_match("for ___ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"][0].astNode
        if def_use_error(_item_):
            return explain_r(message, code, label=tldr)
    return False
Пример #38
0
def list_initialization_misplaced():
    message = "Initialization of <code>{0!s}</code> is a list but either in the wrong place or redefined"
    code = "list_init_misplaced"
    tldr = "Iterating over Non-list"
    match = find_match("for ___ in _item_:\n    pass")
    if match:
        _item_ = match["_item_"][0].astNode
        if data_state(_item_).was_type('list') and def_use_error(_item_):
            return explain_r(message.format(_item_.id), code, label=tldr)
    return False
Пример #39
0
def wrong_for_inside_if():
    message = "The iteration should not be inside the decision block."
    code = "for_in_if"
    tldr = "For inside if"
    match = find_match("if ___:\n"
                       "    for ___ in ___:\n"
                       "        pass")
    if match:
        return explain_r(message, code, label=tldr)
    return False
Пример #40
0
def wrong_list_initialization_placement_9_1():
    match = find_match("rainfall_list = ___\n"
                       "for _item_ in _list_:\n"
                       "    pass")
    if not match:
        explain(
            'The list of rainfall amount (<code>rainfall_list</code>) must be initialized before the iteration that'
            ' uses this list.<br><br><i>(list_init_place_9.1)<i></br>')
        return True
    return False
Пример #41
0
def missing_iterator_initialization():
    match = find_match("for ___ in _list_:\n    pass")
    if match:
        _list_ = match["_list_"].astNode
        if _list_.id == "___":
            explain("The slot to hold a list in the iteration is empty.<br><br><i>(no_iter_init-blank)<i></br>")
            return True
        elif not data_state(_list_).was_type('list'):
            explain("The variable <code>{0!s}</code> is in the list slot of the iteration but is not a list."
                    "<br><br><i>(no_iter_init)<i></br>".format(_list_.id))
            return True
    return False
Пример #42
0
def wrong_modifying_list_8_6():
    """
    std_ast = parse_program()
    list_init = std_ast.find_all('List')
    true_sum = 0
    for value in list_init[0].elts:
        true_sum = value.n + true_sum
    if true_sum != sum([2.9, 1.5, 2.3, 6.1]):
        explain('Don\'t modify the list<br><br><i>(mod_list_8.6)<i></br>')
    Returns:
    """
    match = find_match("_list_ = [2.9, 1.5, 2.3, 6.1]")
    if not match:
        explain('Don\'t modify the list<br><br><i>(mod_list_8.6)<i></br>')
        return True
    return False
Пример #43
0
def wrong_debug_10_7():
    match = find_match("filtered_sentence_counts = []\n"
                       "book_sentence_counts = classics.get('sentences','(None)','')\n"
                       "for book in book_sentence_counts:\n"
                       "    if book >= 5000:\n"
                       "        filtered_sentence_counts.append(book)\n"
                       "plt.hist(filtered_sentence_counts)\n"
                       "plt.title('Distribution of Number of Sentences in Long Books')\n"
                       "plt.xlabel('Number of Sentences')\n"
                       "plt.ylabel('Number of Long Books')\n"
                       "plt.show()\n")

    if not match:
        explain('This is not the change needed. Undo the change and try again.<br><br><i>(debug_10.7)<i></br>')
        return True
    return False
Пример #44
0
def plot_show_missing():
    """
    Name: plot_show_missing
    Pattern:
    Missing
       plt.show()

    Feedback: The plot must be explicitly shown to appear in the Printer area.

    Returns:
    """
    match = find_match("plt.show()")
    if not match:
        explain("The plot must be explicitly shown to appear in the Printer area."
                "<br><br><i>(plot_show_missing)<i></br>")
        return True
    return False
Пример #45
0
def histogram_missing():
    """
    Name: histogram_missing
    Pattern:

    Missing
       plt.hist(___)

    Feedback: The program should display a histogram.

    Returns:
    """
    match = find_match("plt.hist(___)")
    if not match:
        explain("The program should display a histogram.<br><br><i>(histo_missing)<i></br>")
        return True
    return False
Пример #46
0
def wrong_modifying_list_8_5():
    """

    # old code for record keeping because significantly different semantics
    std_ast = parse_program()
    list_init = std_ast.find_all('List')
    true_sum = 0
    if len(list_init) != 0:
        for value in list_init[0].elts:
            true_sum = value.n + true_sum
    if true_sum != sum([20473, 27630, 17849, 19032, 16378]) or len(list_init) == 0:
        explain('Don\'t modify the list<br><br><i>(mod_list_8.5)<i></br>')
        return True
    return  False

    Returns:
    """
    match = find_match("[20473, 27630, 17849, 19032, 16378]")
    if not match:
        explain('Don\'t modify the list<br><br><i>(mod_list_8.5)<i></br>')
        return True
    return False
Пример #47
0
def missing_no_print():
    prints = find_match('print(___)', cut=True)
    if not prints:
        explain('Program does not output anything.<br><br><i>(no_print)<i></br>')
        return True
    return False