Ejemplo n.º 1
0
 def check(self):
     return (search_ast(self.function_tree,
                        (ast.Mod, ast.Add, ast.Mult, ast.JoinedStr))
             or search_ast(self.function_tree, ast.Name,
                           lambda node: node.id == "format")
             or search_ast(self.function_tree, ast.Attribute,
                           lambda node: node.attr == "format"))
Ejemplo n.º 2
0
 def check(self):
     # Basically the above solution is the only option
     # The only reason this isn't a VerbatimStep is to allow strings to be a loose parameter
     return super().check() and \
         search_ast(
             self.tree,
             ast.parse("print(strings[-2][-1])").body[0],
         )
Ejemplo n.º 3
0
        def check(self):
            try:
                return search_ast(self.stmt, (ast.Mult, ast.Div, ast.Sub))
            except SyntaxError:
                if "x" in self.input:
                    return dict(message=dedent("""
            I see an 'x'. If you're trying to multiply, use an asterisk, e.g:

                3 * 4
            """))
Ejemplo n.º 4
0
 def check(self):
     return search_ast(self.function_tree,
                       (ast.Mult, ast.Add, ast.Num))
Ejemplo n.º 5
0
 def check(self):
     return not search_ast(
         self.stmt,
         ast.BinOp(left=ast.Str(), op=ast.Add(), right=ast.Str()),
     )
Ejemplo n.º 6
0
 def check(self):
     try:
         return search_ast(self.tree, (ast.Mult, ast.Div, ast.Sub))
     except SyntaxError:
         if "x" in self.input:
             return self.special_messages.multiply_with_x
Ejemplo n.º 7
0
    class name_box_2(ExerciseStep):
        """
You're getting good at this! Looks like you need more of a challenge...maybe instead of putting a name in a box, the name should be the box? Write a program that outputs this:

    +World+
    W     W
    o     o
    r     r
    l     l
    d     d
    +World+
        """

        hints = """
You will need two separate for loops over `name`.
Each line except for the first and last has the same characters in the middle. That means you can reuse something.
Create a variable containing the spaces in the middle and use it many times.
Use one loop to create a bunch of spaces, and a second loop to print a bunch of lines using the previously created spaces.
        """

        parsons_solution = True

        def solution(self, name: str):
            line = '+' + name + '+'
            spaces = ''
            for _ in name:
                spaces += ' '

            print(line)
            for char in name:
                print(char + spaces + char)
            print(line)

        tests = {
            "World": """\
+World+
W     W
o     o
r     r
l     l
d     d
+World+
""",
            "Bob": """\
+Bob+
B   B
o   o
b   b
+Bob+
""",
        }

        disallowed = [
            Disallowed(
                ast.For,
                predicate=lambda outer: search_ast(outer, ast.For),
                message="""
                    Well done, this solution is correct!
                    And you used a nested loop (a loop inside a loop) which we haven't even covered yet!
                    However, in this case a nested loop is inefficient.
                    You can make a variable containing spaces and reuse that in each line.
                """
            ),
        ]
Ejemplo n.º 8
0
 def check(self):
     return search_ast(
         self.tree,
         ast.Subscript(value=ast.Subscript()),
     )
Ejemplo n.º 9
0
 def check(self):
     return search_ast(self.tree, ast.NotEq)