Example #1
0
    def evaluate(self, history: List[Pairs], members: Members) -> List[Pairs]:
        if not history:
            return PairTree.possible_pairs(members.combinations())

        filtered_pair_trees = self.pair_must_have_only_either_member_of_last_pair(
            history[-1], members.combinations())

        if not filtered_pair_trees:
            filtered_pair_trees = members.combinations()

        possible_pairs: List[Pairs] = PairTree.possible_pairs(
            filtered_pair_trees)

        possible_pairs = self.member_is_must_not_in_same_position_at_three_times(
            history, possible_pairs)

        good_pairs = possible_pairs
        for i in range(1, len(history) + 1):
            tmp = self.pair_should_not_exist_same_pair_in_near_history(
                history[-i:], possible_pairs)
            if not tmp:
                break
            else:
                good_pairs = tmp

        return good_pairs
Example #2
0
 def test_filter(self):
     tree = self.members3.combinations()
     filtered_tree = EvaluationService(
     ).pair_must_have_only_either_member_of_last_pair(
         self.last_pairs3, tree)
     self.assertEqual(filtered_tree, [
         PairTree(Pair(Member('a'), Member('c')),
                  [PairTree(Pair(Member('b'), None), [])]),
         PairTree(Pair(Member('b'), Member('c')),
                  [PairTree(Pair(Member('a'), None), [])])
     ])
Example #3
0
 def test_filter_same_pair2(self):
     history = [
         Pairs([Pair(Member('a'), Member('b')),
                Pair(Member('c'), None)]),
         Pairs([Pair(Member('a'), Member('c')),
                Pair(Member('b'), None)])
     ]
     pairs = PairTree.possible_pairs(self.members3.combinations())
     filtered_pairs = EvaluationService(
     ).pair_should_not_exist_same_pair_in_near_history(history, pairs)
     self.assertEqual(
         filtered_pairs,
         [Pairs([Pair(Member('b'), Member('c')),
                 Pair(Member('a'), None)])])
Example #4
0
 def test_combinations(self):
     self.assertEqual(self.members.combinations(), [
         PairTree(Pair(Member('a'), Member('b')),
                  [PairTree(Pair(Member('c'), None), [])]),
         PairTree(Pair(Member('a'), Member('c')),
                  [PairTree(Pair(Member('b'), None), [])]),
         PairTree(Pair(Member('b'), Member('c')),
                  [PairTree(Pair(Member('a'), None), [])])
     ])
Example #5
0
    def pair_must_have_only_either_member_of_last_pair(
            self, last_pairs: Pairs,
            pair_trees: List[PairTree]) -> List[PairTree]:
        if not last_pairs:
            return pair_trees

        return [
            PairTree(
                tree.pair,
                self.pair_must_have_only_either_member_of_last_pair(
                    last_pairs.tail(), tree.remainder)) for tree in pair_trees
            if tree.pair.only_has_either(last_pairs[0]) and (
                not last_pairs.tail() or (last_pairs.tail(
                ) and self.pair_must_have_only_either_member_of_last_pair(
                    last_pairs.tail(), tree.remainder)))
        ]
Example #6
0
 def test_position_filter(self):
     history = [
         Pairs([Pair(Member('a'), Member('b')),
                Pair(Member('c'), None)]),
         Pairs([Pair(Member('a'), Member('c')),
                Pair(Member('b'), None)])
     ]
     tree = self.members3.combinations()
     possible_pairs = PairTree.possible_pairs(tree)
     filtered_tree = EvaluationService(
     ).member_is_must_not_in_same_position_at_three_times(
         history, possible_pairs)
     self.assertEqual(
         filtered_tree,
         [Pairs([Pair(Member('b'), Member('c')),
                 Pair(Member('a'), None)])])
Example #7
0
 def test_filter_same_pair3(self):
     history = [
         Pairs([
             Pair(Member('a'), Member('c')),
             Pair(Member('d'), Member('e')),
             Pair(Member('b'), None)
         ]),
         Pairs([
             Pair(Member('a'), Member('b')),
             Pair(Member('c'), Member('d')),
             Pair(Member('e'), None)
         ])
     ]
     tree = self.members5.combinations()
     s = EvaluationService()
     filtered_pairs = s.pair_should_not_exist_same_pair_in_near_history(
         history,
         PairTree.possible_pairs(
             s.pair_must_have_only_either_member_of_last_pair(
                 history[-1], tree)))
     self.assertEqual(filtered_pairs, [
         Pairs([
             Pair(Member('a'), Member('e')),
             Pair(Member('b'), Member('c')),
             Pair(Member('d'), None)
         ]),
         Pairs([
             Pair(Member('a'), Member('e')),
             Pair(Member('b'), Member('d')),
             Pair(Member('c'), None)
         ]),
         Pairs([
             Pair(Member('b'), Member('d')),
             Pair(Member('c'), Member('e')),
             Pair(Member('a'), None)
         ]),
         Pairs([
             Pair(Member('b'), Member('e')),
             Pair(Member('a'), Member('c')),
             Pair(Member('d'), None)
         ]),
         Pairs([
             Pair(Member('b'), Member('e')),
             Pair(Member('a'), Member('d')),
             Pair(Member('c'), None)
         ])
     ])
Example #8
0
 def test_fold2(self):
     tree = PairTree(Pair(Member('a'), Member('b')), [
         PairTree(Pair(Member('c'), Member('d')),
                  [PairTree(Pair(Member('e'), None), [])]),
         PairTree(Pair(Member('c'), Member('e')),
                  [PairTree(Pair(Member('d'), None), [])])
     ])
     self.assertEqual([
         Pairs([
             Pair(Member('a'), Member('b')),
             Pair(Member('c'), Member('d')),
             Pair(Member('e'), None)
         ]),
         Pairs([
             Pair(Member('a'), Member('b')),
             Pair(Member('c'), Member('e')),
             Pair(Member('d'), None)
         ])
     ], tree.fold())