Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.56 KB | None | 0 0
  1. def verification_type(w):
  2.     if w.isdigit():
  3.         return "const"
  4.     elif w.isalpha() and len(w) == 1:
  5.         return "ID"
  6.     else:
  7.         return "operator"
  8.  
  9.  
  10. text = '''if N > 0 THEN ;
  11. M := M + 1 / N ;
  12. B := M * N + 3 ;
  13. if B = 0 THEN write ( N ) ;'''
  14. word = text.split()
  15. # print(len(ts))
  16. hTable = [None for _ in range(36)]
  17. sTable = list()
  18.  
  19. for i in range(len(word)):
  20.     mirror = False
  21.     if len(sTable) > 0:
  22.         for j in range(len(sTable)):
  23.             if word[i] == sTable[j][0]:
  24.                 mirror = True
  25.     if mirror == True:
  26.         continue
  27.     hash_ID = hash(word[i]) % 36
  28.     if hTable[hash_ID] is None:     # если в ячейке хэш таблицы пусто
  29.         hTable[hash_ID] = len(sTable)
  30.         sTable.append([word[i], verification_type(word[i]), None, hash_ID])
  31.     else:
  32.         latecomer = hTable[hash_ID]     # опаздавший ID
  33.         while True:
  34.             if sTable[latecomer][2] is None:
  35.                 latecomer_ID = len(sTable)  # получаем указатель
  36.                 sTable.append([word[i], verification_type(word[i]), None, hash_ID])
  37.                 sTable[latecomer][2] = latecomer_ID     # записываем указатель в chain
  38.                 break
  39.             latecomer = sTable[latecomer][2] # меняем старый указатель на указатель из chain
  40. print("Таблица символов:")
  41. for index, m in enumerate(sTable):
  42.     print(index, m)
  43. print("Хэш таблица:")
  44. for index, n in enumerate(hTable):
  45.     print(f'{index} {n}')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement