Skip to content

Commit d036c6b

Browse files
authored
test to read exercises
1 parent f6bc0e9 commit d036c6b

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

easylist.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
2+
3+
'''
4+
List all questions and hints from 100exercices.txt
5+
6+
The file is a bit of a mess, so it's not easy to split it.
7+
3. Questions
8+
9+
Questions are divided by #-----
10+
Sometimes end with #---
11+
They should contain:
12+
Question:
13+
Hints:
14+
Example:
15+
Solution:
16+
'''
17+
18+
filename = '100exercices.txt'
19+
allquestions = []
20+
21+
def main():
22+
start = False
23+
question = []
24+
with open(filename ,'r') as f:
25+
for line in f:
26+
if not start:
27+
if '3. Questions' in line:
28+
#start here:
29+
start = True
30+
else:
31+
if '#--------------------' in line:
32+
if len(question) >2:
33+
allquestions.append(question)
34+
question = []
35+
else:
36+
line = line.strip()
37+
if len(line) > 2:
38+
question.append(line)
39+
40+
while True:
41+
sel = input('Pick a question number (1,{})or (q)uit: '.format(len(allquestions)))
42+
if sel == 'q':
43+
exit()
44+
else:
45+
try:
46+
val = int(sel)-1
47+
result = getQuestion(val)
48+
playQuestion(result)
49+
except ValueError:
50+
print("That's not an int!")
51+
52+
53+
def playQuestion(question):
54+
while True:
55+
print('(q)uestion, (h)int, (e)xample, (s)olution, (a)ll, e(x)it')
56+
sel = input('Pick one: ')
57+
if sel == 'q':
58+
printPart(question,'Question:')
59+
elif sel == 'h':
60+
printPart(question,'Hints:')
61+
elif sel == 'e':
62+
printPart(question,'Example:')
63+
elif sel == 's':
64+
printPart(question,'Solution:')
65+
elif sel == 'a':
66+
printQuestion(question)
67+
elif sel == 'x':
68+
return
69+
70+
def printPart(question,part):
71+
try:
72+
mypart = question[part]
73+
print()
74+
print(part)
75+
for l in mypart:
76+
print(l)
77+
print()
78+
except KeyError:
79+
print('{} missing'.format(part))
80+
81+
def printQuestion(question) :
82+
for k,v in question.items():
83+
print(k)
84+
for l in v:
85+
print(l)
86+
print("--")
87+
88+
def getQuestion(q_number):
89+
if q_number > len(allquestions):
90+
return False
91+
q = allquestions[q_number]
92+
93+
list_block_titles = ['Question:','Hints:','Example:','Solution:']
94+
myblock = []
95+
96+
question = {}
97+
block_name = 'noname'
98+
for line in q:
99+
# https://stackoverflow.com/questions/3389574/check-if-multiple-strings-exist-in-another-string
100+
match = next((x for x in list_block_titles if x in line), False)
101+
if match:
102+
question[block_name] = myblock
103+
block_name = match
104+
myblock = []
105+
else:
106+
myblock.append(line)
107+
question[block_name] = myblock
108+
109+
return question
110+
111+
if __name__ == '__main__':
112+
main()

0 commit comments

Comments
 (0)