Avatar of JaeHyoLee

JaeHyoLee's solution

to Anagram in the Lua Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

Running the tests

To run the tests, run the command busted from within the exercise directory.

Further information

For more detailed information about the Lua track, including how to get help if you're having trouble, please visit the exercism.io Lua language page.

Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

anagram_spec.lua

local Anagram = require('anagram')

describe('anagram', function()
  it('no result', function()
    local detector = Anagram:new('diaper')
    local result = detector:match({ 'hello', 'world', 'zombies', 'pants' })
    local expected = {}
    assert.are.same(expected, result)
  end)

  it('detects simple anagram', function()
    local detector = Anagram:new('ant')
    local result = detector:match({ 'tan', 'stand', 'at' })
    local expected = { 'tan' }
    assert.are.same(expected, result)
  end)

  it('does not detect false positives', function()
    local detector = Anagram:new('galea')
    local result = detector:match({ 'eagle' })
    local expected = {}
    assert.are.same(expected, result)
  end)

  it('detects multiple anagrams', function()
    local detector = Anagram:new('master')
    local result = detector:match({ 'stream', 'pigeon', 'maters' })
    local expected = { 'stream', 'maters' }
    assert.are.same(expected, result)
  end)

  it('does not detect anagram subsets', function()
    local detector = Anagram:new('good')
    local result = detector:match({'dog', 'goody'})
    local expected = {}
    assert.are.same(expected, result)
  end)

  it('detects anagram', function()
    local detector = Anagram:new('listen')
    local result = detector:match({ 'enlists', 'google', 'inlets', 'banana' })
    local expected = { 'inlets' }
    assert.are.same(expected, result)
  end)

  it('detects multiple anagrams', function()
    local detector = Anagram:new('allergy')
    local result = detector:match({ 'gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading' })
    local expected = { 'gallery', 'regally', 'largely' }
    assert.are.same(expected, result)
  end)

  it('detects anagrams case-insensitively', function()
    local detector = Anagram:new('Orchestra')
    local result = detector:match({ 'cashregister', 'Carthorse', 'radishes' })
    local expected = { 'Carthorse' }
    assert.are.same(expected, result)
  end)
end)
local Anagram = {}

local function RemoveLetterIfExists(word, letter)
  --compare both case (lower, upper letter)
  if word:gmatch(string.format("[%s%s]", letter:lower(), letter:upper())) then
    --replace matched letter to '', (remove letter)
    return word:gsub(string.format("[%s%s]", letter:lower(), letter:upper()), '', 1)
    end
  end
  
function Anagram:match(list)
  local result = {}
  for i = 1 , #list do
    local temp = list[i]
    if #list[i] == #self.source then
      for j = 1, #list[i] do
        list[i] = RemoveLetterIfExists(list[i], self.source:sub(j,j))
      end
    end      
    if #list[i] == 0 then result[#result + 1] = temp end
  end
  return result
end

function Anagram:new(source)
  self.source = source
  return self
end

return Anagram

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of ryanplusplus

Your indentation is a little off in RemoveLetterIfExists but looks good otherwise.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?