Skip to content

Commit 631c58f

Browse files
author
Philip Guo
committed
started trying to parse simple questions file format
1 parent 8e4a40a commit 631c58f

2 files changed

Lines changed: 87 additions & 19 deletions

File tree

cgi-bin/parse_questions.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Online Python Tutor
2+
# Copyright (C) 2010 Philip J. Guo
3+
# https://github.com/pgbovine/OnlinePythonTutor/
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
# Defines a function that parses an Online Python Tutor 'questions file'
19+
# into a dict, which can easily be converted into JSON
20+
21+
import os, sys
22+
23+
delimiters = set(['Name:', 'Question:', 'Hint:', 'Solution:',
24+
'Skeleton:', 'Test:', 'Expect:'])
25+
26+
def parseQuestionsFile(filename):
27+
ret = {}
28+
29+
curParts = []
30+
curDelimiter = None
31+
32+
for line in open(filename):
33+
# only strip TRAILING spaces and not leading spaces
34+
line = line.rstrip()
35+
36+
if line in delimiters:
37+
if curDelimiter == 'Name:':
38+
ret['name'] = '\n'.join(curParts).strip()
39+
elif curDelimiter == 'Question:':
40+
ret['question'] = ' '.join(curParts).strip()
41+
elif curDelimiter == 'Hint:':
42+
ret['hint'] = ' '.join(curParts).strip()
43+
elif curDelimiter == 'Solution:':
44+
ret['solution'] = ' '.join(curParts).strip()
45+
elif curDelimiter == 'Skeleton:':
46+
ret['skeleton'] = '\n'.join(curParts).strip()
47+
48+
curDelimiter = line
49+
curParts = []
50+
else:
51+
curParts.append(line)
52+
53+
if curDelimiter:
54+
ret[curDelimiter] = '\n'.join(curParts)
55+
56+
return ret
57+
58+
59+
if __name__ == '__main__':
60+
import pprint
61+
pp = pprint.PrettyPrinter(indent=2)
62+
print pp.pprint(parseQuestionsFile(sys.argv[1]))

questions/inplace-reverse.txt

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,53 @@
1-
Name: In-place reverse
1+
Name:
2+
In-place reverse
23

3-
Problem: Write a function to reverse a list
4-
in-place. For example, it should mutate ['a', 'b, 'c', 'd', 'e'] into
5-
['e', 'd', 'c', 'b', 'a'].
4+
Question:
5+
Write a function to reverse a list in-place. For example, it
6+
should mutate ['a', 'b, 'c', 'd', 'e'] into ['e', 'd', 'c', 'b', 'a'].
67

7-
Hint: Think about swapping pairs of elements.
8+
Hint:
9+
Think about swapping pairs of elements.
810

9-
Solution: Swap the first and last elements, then the second and
10-
second-to-last elements, etc.
11+
Solution:
12+
Swap the first and last elements, then the second and second-to-last
13+
elements, etc.
1114

1215
Skeleton:
1316

1417
def reverse(lst):
1518
# write your solution code here
1619

17-
Tests:
18-
20+
Test:
1921
input = ['a', 'b', 'c', 'd', 'e']
2022
reverse(input)
2123

22-
Expect: input = ['e', 'd', 'c', 'b', 'a']
23-
24+
Expect:
25+
input = ['e', 'd', 'c', 'b', 'a']
2426

27+
Test:
2528
input = ['a', 'b', 'c', 'd']
2629
reverse(input)
2730

28-
Expect: input = ['d', 'c', 'b', 'a']
29-
31+
Expect:
32+
input = ['d', 'c', 'b', 'a']
3033

34+
Test:
3135
input = ['a', 'b', 'c']
3236
reverse(input)
3337

34-
Expect: input = ['c', 'b', 'a']
35-
38+
Expect:
39+
input = ['c', 'b', 'a']
3640

41+
Test:
3742
input = ['a', 'b']
3843
reverse(input)
3944

40-
Expect: input = ['b', 'a']
41-
45+
Expect:
46+
input = ['b', 'a']
4247

48+
Test:
4349
input = ['a']
4450
reverse(input)
4551

46-
Expect: input = ['a']
47-
52+
Expect:
53+
input = ['a']

0 commit comments

Comments
 (0)