Commit 604c42eb by Alexander Kryklia

added anyof flag

parent 37e3181c
...@@ -47,13 +47,13 @@ class PositionsCompare(list): ...@@ -47,13 +47,13 @@ class PositionsCompare(list):
return False return False
# check correct input types # check correct input types
if (not isinstance(self[0], (str, unicode, list, int)) or if (not isinstance(self[0], (str, unicode, list, int, float)) or
not isinstance(other[0], (str, unicode, list, int))): not isinstance(other[0], (str, unicode, list, int, float))):
print 'Incorrect input type' print 'Incorrect input type'
return False return False
if (isinstance(self[0], (list, int)) and if (isinstance(self[0], (list, int, float)) and
isinstance(other[0], (list, int))): isinstance(other[0], (list, int, float))):
print 'Numerical position compare' print 'Numerical position compare'
return self.coordinate_positions_compare(other) return self.coordinate_positions_compare(other)
elif (isinstance(self[0], (unicode, str)) and elif (isinstance(self[0], (unicode, str)) and
...@@ -81,7 +81,6 @@ class PositionsCompare(list): ...@@ -81,7 +81,6 @@ class PositionsCompare(list):
Returns: bool. Returns: bool.
""" """
print 'I am called', self, other print 'I am called', self, other
# get max radius of forgiveness # get max radius of forgiveness
if isinstance(self[0], list): # [(x, y), r] case if isinstance(self[0], list): # [(x, y), r] case
r = max(self[1], r) r = max(self[1], r)
...@@ -136,9 +135,8 @@ class DragAndDrop(object): ...@@ -136,9 +135,8 @@ class DragAndDrop(object):
for groupname, draggable_ids in self.correct_groups.items(): for groupname, draggable_ids in self.correct_groups.items():
if sorted(draggable_ids) != sorted(self.user_groups[groupname]): if sorted(draggable_ids) != sorted(self.user_groups[groupname]):
return False return False
# from now self.correct_groups and self.user_groups are equal if
# from now self.groups and self.user_groups are equal # order is ignored
assert self.correct_groups == self.user_groups
# Check fo every group that positions of every group element are equal # Check fo every group that positions of every group element are equal
# with positions # with positions
...@@ -153,7 +151,7 @@ class DragAndDrop(object): ...@@ -153,7 +151,7 @@ class DragAndDrop(object):
all_user_positions, flag='denied'): all_user_positions, flag='denied'):
return False return False
no_exact, no_allowed = False, False no_exact, no_allowed, no_anyof = False, False, False
# 'exact' rule # 'exact' rule
for groupname in self.correct_groups: for groupname in self.correct_groups:
if self.correct_positions[groupname].get('exact', []): if self.correct_positions[groupname].get('exact', []):
...@@ -174,29 +172,50 @@ class DragAndDrop(object): ...@@ -174,29 +172,50 @@ class DragAndDrop(object):
else: else:
no_allowed = True no_allowed = True
if no_allowed and no_exact: # 'anyof' rule
for groupname in self.correct_groups:
if self.correct_positions[groupname].get('anyof', []):
if not self.compare_positions(
self.correct_positions[groupname]['anyof'],
self.user_positions[groupname]['user'], flag='anyof'):
return False
else:
no_anyof = True
if no_allowed and no_exact and no_anyof:
return False return False
return True return True
def compare_positions(self, list1, list2, flag): def compare_positions(self, correct, user, flag):
""" order of correct/user is matter only in anyof_flag"""
# import ipdb; ipdb.set_trace() # import ipdb; ipdb.set_trace()
if flag == 'denied': if flag == 'denied':
for el1 in list1: for el1 in correct:
for el2 in list2: for el2 in user:
if PositionsCompare(el1) == PositionsCompare(el2): if PositionsCompare(el1) == PositionsCompare(el2):
return False return False
if flag == 'allowed': if flag == 'allowed':
for el1, el2 in zip(sorted(list1), sorted(list2)): for el1, el2 in zip(sorted(correct), sorted(user)):
if PositionsCompare(el1) != PositionsCompare(el2): if PositionsCompare(el1) != PositionsCompare(el2):
return False return False
if flag == 'exact': if flag == 'exact':
for el1, el2 in zip(list1, list2): for el1, el2 in zip(correct, user):
if PositionsCompare(el1) != PositionsCompare(el2): if PositionsCompare(el1) != PositionsCompare(el2):
return False return False
if flag == 'anyof':
count = 0
for u_el in user:
for c_el in correct:
if PositionsCompare(u_el) == PositionsCompare(c_el):
count += 1
continue
if count != len(user):
return False
return True return True
def populate(self, correct_answer, user_answer): def populate(self, correct_answer, user_answer):
...@@ -232,6 +251,8 @@ def grade(user_input, correct_answer): ...@@ -232,6 +251,8 @@ def grade(user_input, correct_answer):
""" Support 2 interfaces""" """ Support 2 interfaces"""
if isinstance(correct_answer, dict): if isinstance(correct_answer, dict):
dnd = DragAndDrop() dnd = DragAndDrop()
else:
dnd = correct_answer
dnd.populate(correct_answer=correct_answer, user_answer=user_input) dnd.populate(correct_answer=correct_answer, user_answer=user_input)
return dnd.grade() return dnd.grade()
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