コード例 #1
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_intersection(self):
     first_set = HashSet(['One', 'Two', 'Three', 'Four'])
     second_set = HashSet(['Red', 'Two', 'Three', 'Orange'])
     intersection = first_set.intersection(second_set)
     assert intersection.size == 2
     assert intersection.contains('Two')
     assert intersection.contains('Three')
コード例 #2
0
    def test_is_subset(self):
        hs = HashSet([1, 2, 3, 4, 5, 6, 7, 8])

        assert HashSet([1, 2, 3]).is_subset(hs)
        assert not HashSet([11]).is_subset(hs)

        assert not hs.is_subset([11])
コード例 #3
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_difference(self):
     first_set = HashSet(['One', 'Two', 'Three', 'Four'])
     second_set = HashSet(['Red', 'Two', 'Three', 'Orange'])
     difference = first_set.difference(second_set)
     print(difference.size)
     assert difference.contains('One') == True
     assert difference.contains('Two') == False
     assert difference.contains('Three') == False
コード例 #4
0
    def test_add(self):
        items = ['A', 'B', 'C']
        hs = HashSet()
        for item in items:
            hs.add(item)

        for item in items:
            assert item in hs

        assert len(hs) == len(items)
コード例 #5
0
    def test_difference(self):
        set_1 = HashSet([1, 2, 3, 4])
        set_2 = HashSet([1, 2, 5, 6])

        diff = set_1 - set_2

        assert len(diff) == 2
        assert 3 in diff
        assert 1 not in diff
        assert 2 not in diff
コード例 #6
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_union(self):
     first_set = HashSet(['One', 'Two', 'Three', 'Four'])
     second_set = HashSet(['Red', 'Green', 'Blue'])
     union = first_set.union(second_set)
     assert union.size == 7
     assert union.contains('Two') == True
     assert union.contains('Three') == True
     assert union.contains('Blue') == True
     assert union.contains('Purple') == False
     assert union.contains('Six') == False
コード例 #7
0
    def test_intersection(self):
        set_1 = HashSet([1, 2, 3, 4])
        set_2 = HashSet([1, 2, 5, 6])

        intersection = set_1 & set_2

        assert 1 in intersection
        assert len(intersection) == 2
        assert 2 in intersection
        assert 3 not in intersection
コード例 #8
0
 def test_size(self):
     hs = HashSet()
     assert hs.size == 0
     hs.add('A')
     assert hs.size == 1
     hs.add('B')
     assert hs.size == 2
     hs.remove('A')
     assert hs.size == 1
     hs.remove('B')
     assert hs.size == 0
コード例 #9
0
    def test_union(self):
        set_1 = HashSet([1, 2, 3, 4])
        set_2 = HashSet([1, 5, 6])

        union = set_1 + set_2

        assert len(union) == 6
        assert 5 in union
        assert 7 not in union
        union.remove(1)
        assert 1 not in union
        assert len(union) == 5
コード例 #10
0
    def test_contains(self):
        items = ['A', 'B', 'C']
        hs = HashSet(items)
        for item in items:
            assert item in hs

        assert 'D' not in hs
        assert 'E' not in hs
コード例 #11
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_contains(self):
     new_set = HashSet(['This', 'is', 'my', 'set'])
     assert new_set.contains('This') == True
     assert new_set.contains('my') == True
     assert new_set.contains('set') == True
     assert new_set.contains('Willowwww') == False
     assert new_set.contains('your') == False
コード例 #12
0
class HashMap:
    class __KVPair:
        def __init__(self, key, value):
            self.key = key
            self.value = value

        def __eq__(self, other):
            if type(self) != type(other):
                return False

            return self.key == other.key

        def getKey(self):
            return self.key

        def getValue(self):
            return self.value

        def __hash__(self):
            return hash(self.key)

    def __init__(self):
        self.hSet = HashSet()

    def __len__(self):
        return len(self.hSet)

    def __contains__(self, item):
        return HashMap.__KVPair(item, None) in self.hSet

    def __notcontains__(self, item):
        return item not in self.hSet

    def __setitem__(self, key, value):
        self.hSet.add(HashMap.__KVPair(key, value))

    def __getitem__(self, key):
        if HashMap.__KVPair(key, None) in self.hSet:
            val = self.hSet[HashMap.__KVPair(key, None)].getValue()
            return val
        raise KeyError("Key " + str(key) + "not in HashMap")

    def __iter__(self):
        for x in self.hSet:
            yield x.getKey()
コード例 #13
0
    def test_remove(self):
        items = ['A', 'B', 'C']
        hs = HashSet(items)
        for item in items:
            # For each item, make sure can only remove once
            hs.remove(item)
            with self.assertRaises(KeyError):
                hs.remove(item)

        assert hs.size == 0
コード例 #14
0
 def __init__(self):
     self.hSet = HashSet()
コード例 #15
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_remove(self):
     new_set = HashSet(['red', 'brown', 'gray'])
     assert new_set.__length__() == 3
     new_set.remove('brown')
     assert new_set.__length__() == 2
     assert new_set.contains('brown') == False
     assert new_set.contains('red') == True
     assert new_set.contains('gray') == True
     new_set.remove('gray')
     assert new_set.__length__() == 1
     assert new_set.contains('red') == True
     assert new_set.contains('gray') == False
コード例 #16
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_add(self):
     new_set = HashSet()
     assert new_set.__length__() == 0
     new_set.add('red')
     new_set.add('brown')
     new_set.add('gray')
     assert new_set.__length__() == 3
     assert new_set.contains('red') == True
     assert new_set.contains('brown') == True
     assert new_set.contains('purple') == False
     new_set.add('orange')
     new_set.add('purple')
     assert new_set.__length__() == 5
     assert new_set.contains('purple') == True
コード例 #17
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_is_superset(self):
     first_set = HashSet(
         ['Let', 'Me', 'Reck', 'Orange', 'Pill', 'Good', 'Sir'])
     second_set = HashSet(['Let', 'Me', 'Reck'])
     assert first_set.is_superset(second_set) == True
     assert second_set.is_superset(first_set) == False
コード例 #18
0
 def test_init(self):
     hs = HashSet()
     assert hs.size == 0
     assert str(hs) == '{}'
コード例 #19
0
ファイル: hash_set_test.py プロジェクト: KhallilB/CS1.3
 def test_init(self):
     new_set = HashSet()
     assert new_set.size == 0
     working_set = HashSet(['Working', 'Set'])
     assert working_set.__length__() == 2
コード例 #20
0
 def test_init_with_list(self):
     items = [1, 2, 3, 4]
     hs = HashSet(items)
     assert hs.size == len(items)
     for item in items:
         assert item in hs