Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add python getDef helper #98

Merged
merged 1 commit into from
Nov 24, 2023
Merged

feat: add python getDef helper #98

merged 1 commit into from
Nov 24, 2023

Conversation

ShaunSHamilton
Copy link
Member

Checklist:

Adds a helper I think we might want to use in some Python curriculum tests. As such, I created a separate module for the code.

Example Code to Test
NUMBER_OF_DISKS = 3
number_of_moves = 2 ** NUMBER_OF_DISKS - 1
rods = {
    'A': list(range(NUMBER_OF_DISKS, 0, -1)),
    'B': [],
    'C': []
}

def move(n, source, target, auxiliary):
    # display starting configuration
    for i in range(number_of_moves):
        if (i + 1) % 3 == 1:
            print(f'Move {i + 1} allowed between {source} and {target}')
            forward = False
            if len(rods[target]) == 0:
                forward = True
        elif (i + 1) % 3 == 2:
            print(f'Move {i + 1} allowed between {source} and {auxiliary}')
        elif (i + 1) % 3 == 0:
            print(f'Move {i + 1} allowed between {auxiliary} and {target}')
Example test
// Test if `forward` is actually set to `True` when `rods[target]` is `[]`
({ test: () => 
  {
    const move = __helpers.python.getDef(code, "move");
    const {function_body, function_indentation} = move;
    const c = `
NUMBER_OF_DISKS = 3
number_of_moves = 2 ** NUMBER_OF_DISKS - 1
rods = {
    'A': list(range(NUMBER_OF_DISKS, 0, -1)),
    'B': [],
    'C': []
}

forward = False

def move(n, source, target, auxiliary):
${function_body.match(/\s+/)[0]}global forward
${function_body}

print(move(NUMBER_OF_DISKS, "A", "C", "B"))
assert forward == True
`;
    const out = __pyodide.runPython(c); // If this does not throw, code is correct
  }
})

I tried to prevent the formatting changes, but failed

@ShaunSHamilton ShaunSHamilton requested a review from a team as a code owner November 23, 2023 13:18
@raisedadead raisedadead merged commit a15c8a4 into main Nov 24, 2023
3 checks passed
@raisedadead raisedadead deleted the feat_python branch November 24, 2023 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants