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.139 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.138 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.137 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.136 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.135 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.134 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.133 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.132 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.131 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.130 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.129 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.128 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.127 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.126 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.125 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.124 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.123 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.122 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.121 tracks/lua/exercises/anagram/example.lua
trackler-2.2.1.120 tracks/lua/exercises/anagram/example.lua