Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- M = 100003
- class Node:
- def __init__(self, uid, v, next_item=None):
- self.value = [uid, v]
- self.next_item = next_item
- def __search_prev_cur__(uid, head):
- if not head:
- return None, None
- if head.value[0] == uid:
- return None, head
- node = head
- while node.next_item and node.next_item.value[0] != uid:
- node = node.next_item
- if node.next_item and node.next_item.value[0] == uid:
- return node, node.next_item
- return None, None
- def put(uid, v, head):
- _, node = __search_prev_cur__(uid, head)
- if node:
- node.value[1] = v
- else:
- head = Node(uid, v, next_item=head)
- return None, head
- def get(uid, head):
- _, node = __search_prev_cur__(uid, head)
- return node.value[1] if node else False, head
- def delete(uid, head):
- prev, node = __search_prev_cur__(uid, head)
- if not node:
- return False, head
- if node == head:
- head = node.next_item
- else:
- prev.next_item = node.next_item
- return node.value[1], head
- def process_commands(n):
- hash_table = [None] * M
- for _ in range(n):
- args = (input() + ' 0').split()[:3]
- cmd = args[0]
- uid = int(args[1])
- v = int(args[2])
- bucket = uid % M
- head = hash_table[bucket]
- res, head = {'put': lambda uid, v: put(uid, v, head),
- 'get': lambda uid, _: get(uid, head),
- 'delete': lambda uid, _: delete(uid, head)}[cmd](uid, v)
- [0 if res is None else print(None) if res is False else print(res)]
- hash_table[bucket] = head
- n = int(input())
- process_commands(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement