Advertisement
nebyt

nested_structure

Feb 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. num = int(input())
  2. # create main global space
  3. # it is flat dictionary construction
  4. spaces = {'global':{
  5.     'parent': None,
  6.     'vars': {}
  7.     }}
  8. count = 1
  9.  
  10.  
  11. #  add more in exist spaces
  12. def add(arg1,arg2):
  13.     global count
  14.     spaces[arg1]['vars'][arg2] = count
  15.     count+=1
  16.  
  17. # create a new spaces
  18. # we sign the previous space every time when we create a space in 'parent'
  19. def create(arg1,arg2):
  20.     spaces[arg1] = {'parent':arg2,'vars':{}}
  21.  
  22. # get information about spaces
  23. def get(arg1,arg2):
  24.     if arg2 in spaces[arg1]['vars']:
  25.         print(arg1)
  26.     elif spaces[arg1]['parent'] == None:
  27.         print('None')
  28.     else:
  29.         get(spaces[arg1]['parent'],arg2)
  30.  
  31.  
  32. if 1 <= num <= 100:
  33.     for i in range(num):
  34.         command = input().split(" ")
  35.         if command[0] == "add":
  36.             add(command[1],command[2])
  37.         elif command[0] == "create":
  38.             create(command[1],command[2])
  39.         elif command[0] == "get":
  40.             get(command[1],command[2])
  41.         else:
  42.             print("Unknowm command")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement