Exemplo n.º 1
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)
Exemplo n.º 2
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
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()
Exemplo n.º 4
0
 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