Commit 38444763 by Alexander Kryklia

fixes in algorithm

parent dd9cdc1c
...@@ -28,7 +28,6 @@ values are (x,y) coordinates of centers of dragged images. ...@@ -28,7 +28,6 @@ values are (x,y) coordinates of centers of dragged images.
""" """
import json import json
import math
def grade(user_input, correct_answer): def grade(user_input, correct_answer):
...@@ -58,28 +57,29 @@ def grade(user_input, correct_answer): ...@@ -58,28 +57,29 @@ def grade(user_input, correct_answer):
if len(correct_answer.keys()) != len(user_answer['draggables']): if len(correct_answer.keys()) != len(user_answer['draggables']):
return False return False
key = ['position']
def is_equal(user_answer, correct_answer): def is_equal(user_answer, correct_answer):
""" Checks if user_answer is equal to correct_answer inside radius """ Checks if user_answer is equal to correct_answer inside radius
of forgiveness (default 10 px). of forgiveness (default 10 px).
Args: Args:
user_answer: (x, y) tuple of floats; user_answer: [x, y] - list of floats;
correct_answer is one of variants: correct_answer: [x, y] or [[x, y], r], where
- (x, y) tuple of floats r is radius of forgiveness;
- [(x, y), r], r - radius of forgiveness;
Returns: bool. Returns: bool.
""" """
if not isinstance(correct_answer, list) or \
not isinstance(user_answer, list):
return False
r = 10 r = 10
if type(correct_answer) is type(()): # (x, y) case if isinstance(correct_answer[0], list): # [(x, y), r] case
corr_x = correct_answer[0]
corr_y = correct_answer[1]
else: # [(x, y), r] case
r = correct_answer[1] r = correct_answer[1]
corr_x = correct_answer[0][0] corr_x = correct_answer[0][0]
corr_y = correct_answer[0][1] corr_y = correct_answer[0][1]
else: # (x, y) case
corr_x = correct_answer[0]
corr_y = correct_answer[1]
if ((user_answer[0] - corr_x) ** 2 + if ((user_answer[0] - corr_x) ** 2 +
(user_answer[1] - corr_y) ** 2) > r * r: (user_answer[1] - corr_y) ** 2) > r * r:
...@@ -88,11 +88,12 @@ def grade(user_input, correct_answer): ...@@ -88,11 +88,12 @@ def grade(user_input, correct_answer):
return True return True
if user_answer["use_targets"]: if user_answer["use_targets"]:
key = ['target_id'] is_equal = lambda user, correct: user == correct if (
is_equal = lambda x, y: x == y isinstance(user, unicode) and isinstance(correct, str)) else False
for draggable in user_answer['draggables']: for draggable in user_answer['draggables']:
if not is_equal(draggable[key], correct_answer[key]): if not is_equal(draggable.values()[0],
correct_answer[draggable.keys()[0]]):
return False return False
return True return True
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment