Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.2 kB
1
Indexable
Never
class HashTable:
    def __init__(self):
        self.index = []
        for i in range(100000):
            self.index.append([])

    def get_hash(self, num):
        return num % 100000

    def add(self, key, value):
        for i in self.index[self.get_hash(key)]:
            if i[0] == key:
                i[1] = value
                return
        self.index[self.get_hash(key)].append([key, value])

    def find(self, key):
        arr = self.index[self.get_hash(key)]
        if len(arr) == 0:
            return None
        return arr[-1][1]
        #for pair in arr:
            #if pair[0] == key:
                #return pair[1]

    def delete(self, key):
        arr = self.index[self.get_hash(key)]
        if len(arr) > 0:
            arr.remove(arr[-1])
        for pair in arr:
            if pair[0] == key:
                arr.remove(pair)

n = int(input())
table = HashTable()

for i in range(n):
    inp = input().split()
    if inp[0] == "add":
        table.add(int(inp[1]), inp[2])
    elif inp[0] == "find":
        res = table.find(int(inp[1]))
        print(res if res is not None else "not found")
    elif inp[0] == "del":
        table.delete(int(inp[1]))
Leave a Comment