Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anagram - Allow results in any order #531

Merged
merged 1 commit into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions exercises/practice/anagram/.docs/instructions.append.md

This file was deleted.

29 changes: 21 additions & 8 deletions exercises/practice/anagram/anagram_spec.lua
Original file line number Diff line number Diff line change
@@ -1,59 +1,72 @@
local Anagram = require('anagram')

describe('anagram', function()
local function sorted_clone(t)
local clone = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad there's not a table.unpack equivalent for associative arrays.

for k, v in pairs(t) do
clone[k] = v
end
table.sort(clone)
return clone
end

local function assert_lists_are_same(expected, actual)
assert.are.same(sorted_clone(expected), sorted_clone(actual))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could possibly check lengths are the same before sorting.

end

it('no result', function()
local detector = Anagram:new('diaper')
local result = detector:match({ 'hello', 'world', 'zombies', 'pants' })
local expected = {}
assert.are.same(expected, result)
assert_lists_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)
assert_lists_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)
assert_lists_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)
assert_lists_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)
assert_lists_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)
assert_lists_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)
assert_lists_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)
assert_lists_are_same(expected, result)
end)
end)
Loading