-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscramble_codes.py
59 lines (46 loc) · 1.36 KB
/
scramble_codes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from __future__ import print_function
# STDLIB
import os
import random
def scramble_codes(code, print_to_screen=True, seed=None):
"""
Scramble code for Software Carpentry instructors to give
to students for exercise.
This function can also print the scrambled code directly on screen
for copy-and-paste into teaching material (e.g., Jupyter notebook).
Parameters
----------
code : str
A string literal of the code snippet.
seed : int, optional
Give a random seed if you want reproducible result.
Returns
-------
scrambled_code : str
Scrambled code snippet with indentation removed.
"""
s_list = []
for s in code.split(os.linesep):
s2 = s.lstrip() # Remove indentation
if len(s2) > 0: # Exclude blank lines
s_list.append(s2)
# Shuffle and rejoin the lines
rng = random.Random(seed)
rng.shuffle(s_list)
scrambled_code = os.linesep.join(s_list)
if print_to_screen:
print(scrambled_code)
return scrambled_code
def test_scramble_codes():
"""
Test for :func:`scramble_codes`.
"""
code = """
i = 0
while i < 10:
print(i)
i += 1
"""
answer = 'while i < 10:\nprint(i)\ni = 0\ni += 1'
scrambled_code = scramble_codes(code, print_to_screen=False, seed=1234)
assert scrambled_code == answer