예제 #1
0
def test_union_joins_objects_in_set():
    disjoint_set = DisjointSet()
    disjoint_set.make_set({'name': 'banana'})
    disjoint_set.make_set({'name': 'strawberry'})
    disjoint_set.make_set({'name': 'blueberry'})
    disjoint_set.union(disjoint_set.data[0], disjoint_set.data[1])
    assert disjoint_set.data[0]['name'] == 'blueberry'
    assert disjoint_set.data[0]['parent']['name'] == 'strawberry'
    assert disjoint_set.data[0]['parent']['rank'] == 1
    assert disjoint_set.data[0]['rank'] == 0
예제 #2
0
    def create_union_finds_for_chars(self):
        disjoint_set = DisjointSet()
        for char in self.unique_characters:
            disjoint_set.make_set({'name': char})

        for pair in self.kin_list:
            parsed_pair = pair.replace('\n', '').split('\tkin\t')
            index1 = find_index_of_object_with_value(disjoint_set.data,
                                                     parsed_pair[0])
            index2 = find_index_of_object_with_value(disjoint_set.data,
                                                     parsed_pair[1])
            disjoint_set.union(disjoint_set.data[index1],
                               disjoint_set.data[index2])

        self.set = disjoint_set.data
예제 #3
0
    def create_union_finds_for_chars(self):
        disjoint_set = DisjointSet()
        for char in self.unique_characters:
            disjoint_set.make_set({'name': char})

        for pair in self.kin_list:
            parsed_pair = pair.replace('\n', '').split('\tkin\t')
            index1 = find_index_of_object_with_value(disjoint_set.data, parsed_pair[0])
            index2 = find_index_of_object_with_value(disjoint_set.data, parsed_pair[1])
            disjoint_set.union(disjoint_set.data[index1], disjoint_set.data[index2])

        # do a group by
        # Hash table - one entry for each parent, and when you encounter an entry in
        # that group, concatenate their names together.

        # print("\n")
        # print("***************************************************************")
        # for datum in disjoint_set.data:
        #     if (datum['parent']['name'] != datum['name']):
        #         print(datum['name'] + " : " + datum['parent']['name'])
        #     else:
        #         print(datum['name'])
        # print("***************************************************************")
        self.set = disjoint_set.data
예제 #4
0
def test_disjoint_set_can_be_created():
    result = DisjointSet()
    assert result.data == []
예제 #5
0
def test_find_returns_obj_when_it_is_parent_obj():
    disjoint_set = DisjointSet()
    disjoint_set.make_set({'name': 'banana'})
    result = disjoint_set.find(disjoint_set.data[0])
    assert result == disjoint_set.data[0]
예제 #6
0
def test_make_set_creates_a_set():
    disjoint_set = DisjointSet()
    disjoint_set.make_set({'name': 'banana'})
    disjoint_set.make_set({'name': 'strawberry'})
    assert len(disjoint_set.data) == 2