SHARE
TWEET

Untitled

a guest Aug 19th, 2019 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/python3
  2.  
  3. DEBUG = False
  4.  
  5. import os
  6. import sys
  7.  
  8. def trie_find(node, path):
  9.     if not node.get(path[0]):
  10.         return 0
  11.  
  12.     if len(path) > 1:
  13.         return trie_find(node[path[0]], path[1:])
  14.     else:
  15.         return node[path[0]]['__count']
  16.  
  17. def trie_inc(node, path):
  18.     if not node.get(path[0]):
  19.         node[path[0]] = { '__count': 0 }
  20.  
  21.     node[path[0]]['__count'] = node[path[0]]['__count'] + 1
  22.  
  23.     if len(path) > 1:
  24.         trie_inc(node[path[0]], path[1:])
  25.        
  26.  
  27. #
  28. # Complete the contacts function below.
  29. #
  30. def contacts(queries):
  31.     trie = {}
  32.     answers = []
  33.  
  34.     for query in queries:
  35.         if query[0] == 'add':
  36.             trie_inc(trie, list(query[1]))
  37.         else:
  38.             answers.append(trie_find(trie, list(query[1])))
  39.  
  40.     return answers
  41.  
  42. if __name__ == '__main__':
  43.     if DEBUG:
  44.         queries = [line.strip().split() for line in open("in.txt", 'r')][1:]
  45.        
  46.         result = contacts(queries)
  47.  
  48.         for line in result:
  49.             print(line)
  50.     else:
  51.         fptr = open(os.environ['OUTPUT_PATH'], 'w')
  52.  
  53.         queries_rows = int(input())
  54.  
  55.         queries = []
  56.  
  57.         for _ in range(queries_rows):
  58.             queries.append(input().rstrip().split())
  59.  
  60.         result = contacts(queries)
  61.  
  62.         fptr.write('\n'.join(map(str, result)))
  63.         fptr.write('\n')
  64.  
  65.         fptr.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top