-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge2.py
More file actions
executable file
·131 lines (108 loc) · 4.13 KB
/
challenge2.py
File metadata and controls
executable file
·131 lines (108 loc) · 4.13 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python
from twisted.web.server import Site, Session
from twisted.web.resource import Resource
from twisted.internet import reactor
import random
import json
class ShortSession(Session):
sessionTimeout = 2
class Challenge2(Resource):
sessions = set()
solution = ''
equation = ''
session = None
num = {
1: 'one', 16: 'sixteen',
2: 'two', 17: 'seventeen',
3: 'three', 18: 'eighteen',
4: 'four', 19: 'nineteen',
5: 'five', 20: 'twenty',
6: 'six', 21: 'twenty-one',
7: 'seven', 22: 'twenty-two',
8: 'eight', 23: 'twenty-three',
9: 'nine', 24: 'twenty-four',
10: 'ten', 25: 'twenty-five',
11: 'eleven', 26: 'twenty-six',
12: 'twelve', 27: 'twenty-seven',
13: 'thirteen', 28: 'twenty-eight',
14: 'fourteen', 29: 'twenty-nine',
15: 'fifteen', 30: 'thirty',
0: 'zero'
}
sym = {
-1: 'minus', 1: 'plus'
}
jokes = [
'What do mathematicians eat on Halloween? Pumpkin Pi.',
'Why did the math book look so sad? Because it had so many problems.',
'A circle is just a round straight line with a hole in the middle.',
'Decimals have a point.',
'Why do plants hate math? Because it gives them square roots.',
'Why did the boy eat his math homework? Because the teacher told him it was a piece of cake.',
'Have you heard the latest statistics joke? Probably.',
'What did the acorn say when it grew up? Geometry.',
'What do you call an empty parrot cage? Polygon.',
'Cakes are round but Pi are square.',
'How can you make time fly? Throw a clock out the window!',
'Without geometry, life is pointless.'
]
def get_answer(self, challenge):
answers = open('answers.json')
data = json.load(answers)
answers.close()
return str(data[challenge])
def get_symbol(self):
x = random.getrandbits(1)
if x == 0:
return -1
else:
return 1
def get_equation(self):
x, y, z = 1, 1, 1
a1, a2 = -1, -1
while (x + a1 * y + a2 * z) < 0:
x = random.randint(1, 10)
y = random.randint(1, 10)
z = random.randint(1, 10)
a1 = self.get_symbol()
a2 = self.get_symbol()
return x, y, z, a1, a2
def make_equation(self):
x, y, z, a1, a2 = self.get_equation()
solution = self.num[(x + a1 * y + a2 * z)]
equation = self.num[x] + ' ' + self.sym[a1] + ' ' + self.num[y] + ' ' + \
self.sym[a2] + ' ' + self.num[z] + ' equals ?'
return equation, solution
def render_GET(self, request):
if 'answer' in str(request):
return 'You should have learned how to submit answers by now...'
self.equation, self.solution = self.make_equation()
self.session = request.getSession()
if self.session.uid not in self.sessions:
self.sessions.add(self.session.uid)
self.session.notifyOnExpire(lambda: self._expired(self.session.uid))
return 'I am Jack\'s unsolved equation: "' + self.equation + '"'
def render_POST(self, request):
if self.session.uid not in self.sessions:
self.sessions.add(self.session.uid)
self.session.notifyOnExpire(lambda: self._expired(self.session.uid))
return 'Too slow...'
try:
answer = json.loads(request.content.getvalue())['answer']
except ValueError:
return 'You should be submitting as JSON'
if answer == self.solution:
return self.get_answer('challenge2')
else:
return 'Try again, Here is a joke for your troubles:\n' + self.jokes[random.randint(0, 11)]
def _expired(self, uid):
try:
self.sessions.remove(uid)
except KeyError:
pass
rootResource = Resource()
rootResource.putChild("challenge2", Challenge2())
factory = Site(rootResource)
factory.sessionFactory = ShortSession
reactor.listenTCP(8082, factory)
reactor.run()