1 from __future__
import absolute_import
2 from __future__
import unicode_literals
4 from random
import randint
, choice
5 from hashlib
import sha1
7 from django
.conf
import settings
8 from django
.utils
import six
14 MULTIPLY
: lambda a
, b
: a
* b
,
15 ADD
: lambda a
, b
: a
+ b
,
16 SUBTRACT
: lambda a
, b
: a
- b
,
18 OPERATORS
= tuple(CALCULATIONS
)
21 def hash_answer(value
):
22 answer
= six
.text_type(value
)
23 to_encode
= (settings
.SECRET_KEY
+ answer
).encode('utf-8')
24 return sha1(to_encode
).hexdigest()
28 return choice(OPERATORS
)
31 def get_numbers(start_int
, end_int
, operator
):
32 x
= randint(start_int
, end_int
)
33 y
= randint(start_int
, end_int
)
35 #avoid negative results for subtraction
36 if y
> x
and operator
== SUBTRACT
:
42 def calculate(x
, y
, operator
):
43 func
= CALCULATIONS
[operator
]