Advertisement
alestane

Dictionary tree (hard confirm)

Feb 3rd, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local dictionary = {}
  2.  
  3. local function insertWord(dict, word)
  4.     local head, tail = word:upper():match('^(%a)(%a*)$')
  5.     if not head then return false; end
  6.     if not dict[head] then dict[head] = {}; end
  7.     if tail == '' then
  8.         dict[head][''] = true
  9.         return true
  10.     else
  11.         return insertWord(dict[head], tail)
  12.     end
  13. end
  14.  
  15. insertWord(dictionary, 'BAG')
  16.  
  17. if dictionary['B'] and dictionary['B']['A'] and dictionary['B']['A']['G'] and dictionary['B']['A']['G'][''] then
  18.     print(true)
  19. else
  20.     print(false)
  21. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement