Sha256: 9d5da5873801598784cbb72d25aafcab2fb1dd2673dfdf76d519b46302817946

Contents?: true

Size: 747 Bytes

Versions: 396

Compression:

Stored size: 747 Bytes

Contents

local Anagram = {}

function Anagram:new(a_word)
  self.__index = self
  return setmetatable({word = a_word:lower()}, self)
end

local function alphagram(a_word)
  local str = a_word
  local t = {}
  for i = 1,#str do
    t[i] = str:sub(i, i)      -- string library sets metatable so can be accessed in object oriented style 
  end
  table.sort(t)               -- sorts in place, no return value
  return table.concat(t,'')
end  
 
function Anagram:match( list )
  local result = {}
  local t = list
  for i =1,#t do
    if (t[i]:lower() == self.word) then 
      table.insert(result, t[i])    
    elseif (alphagram(t[i]:lower()) == alphagram(self.word) ) then
      table.insert(result, t[i])
    end  
  end
  return result
end

return Anagram

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.179 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.178 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.177 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.176 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.175 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.174 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.173 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.172 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.171 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.170 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.169 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.167 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.166 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.165 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.164 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.163 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.162 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.161 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.160 tracks/lua/exercises/anagram/example.lua