Commit 1257c37a authored by Taddeus Kroes's avatar Taddeus Kroes

Implemented and tested filtering of duplicate possibilities.

parent a1c9eecc
...@@ -28,30 +28,26 @@ class Possibility(object): ...@@ -28,30 +28,26 @@ class Possibility(object):
and self.args == other.args and self.args == other.args
def filter_duplicates(items): def filter_duplicates(possibilities):
""" """
Filter duplicated possibilities. Duplicated possibilities occur in n-ary Filter duplicated possibilities. Duplicated possibilities occur in n-ary
nodes, the root-level node and a lower-level node will both recognize a nodes, the root-level node and a lower-level node will both recognize a
reqrite possibility within their sscope, whereas only the root-level one rewrite possibility within their sscope, whereas only the root-level one
matters. matters.
Example: 1 + 2 + 3 Example: 1 + 2 + 3
The addition of 1 and 2 is recognized bij n-ary additions "1 + 2" and The addition of 1 and 2 is recognized bij n-ary additions "1 + 2" and
"1 + 2 + 3". The "1 + 2" addition should be removed by this function. "1 + 2 + 3". The "1 + 2" addition should be removed by this function.
""" """
# TODO: Finish according to docstrings features = []
unique = [] unique = []
for item in items: for p in reversed(possibilities):
found = False feature = (p.handler, p.args)
for compare in unique: if feature not in features:
if item == compare: features.append(feature)
found = True unique.insert(0, p)
break
if not found:
unique.append(item)
return unique return unique
......
import unittest import unittest
from src.possibilities import MESSAGES, Possibility as P, filter_duplicates from src.possibilities import MESSAGES, Possibility as P, filter_duplicates
from src.rules.numerics import add_numerics
from tests.test_rules_poly import tree from tests.test_rules_poly import tree
...@@ -36,7 +37,22 @@ class TestPossibilities(unittest.TestCase): ...@@ -36,7 +37,22 @@ class TestPossibilities(unittest.TestCase):
assert self.p0 != self.p1 assert self.p0 != self.p1
def test_filter_duplicates(self): def test_filter_duplicates(self):
a, b = ab = tree('a + b')
p0 = P(a, dummy_handler, (1, 2))
p1 = P(ab, dummy_handler, (1, 2))
p2 = P(ab, dummy_handler, (1, 2, 3))
p3 = P(ab, dummy_handler_msg, (1, 2))
self.assertEqual(filter_duplicates([]), []) self.assertEqual(filter_duplicates([]), [])
self.assertEqual(filter_duplicates([1, 2]), [1, 2]) self.assertEqual(filter_duplicates([p0, p1]), [p1])
self.assertEqual(filter_duplicates([1, 2, 2]), [1, 2]) self.assertEqual(filter_duplicates([p1, p2]), [p1, p2])
self.assertEqual(filter_duplicates([1, 2, 3, 2]), [1, 2, 3]) self.assertEqual(filter_duplicates([p1, p3]), [p1, p3])
self.assertEqual(filter_duplicates([p0, p1, p2, p3]), [p1, p2, p3])
# Docstrings example
(l1, l2), l3 = left, l3 = right = tree('1 + 2 + 3')
p0 = P(left, add_numerics, (1, 2, 1, 2))
p1 = P(right, add_numerics, (1, 2, 1, 2))
p2 = P(right, add_numerics, (1, 3, 1, 3))
p3 = P(right, add_numerics, (2, 3, 2, 3))
self.assertEqual(filter_duplicates([p0, p1, p2, p3]), [p1, p2, p3])
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