Skip to content

Commit beeafd0

Browse files
authored
Anagram - Allow results in any order (#531)
1 parent c963a59 commit beeafd0

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

exercises/practice/anagram/.docs/instructions.append.md

-3
This file was deleted.
+21-8
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,72 @@
11
local Anagram = require('anagram')
22

33
describe('anagram', function()
4+
local function sorted_clone(t)
5+
local clone = {}
6+
for k, v in pairs(t) do
7+
clone[k] = v
8+
end
9+
table.sort(clone)
10+
return clone
11+
end
12+
13+
local function assert_lists_are_same(expected, actual)
14+
assert.are.same(sorted_clone(expected), sorted_clone(actual))
15+
end
16+
417
it('no result', function()
518
local detector = Anagram:new('diaper')
619
local result = detector:match({ 'hello', 'world', 'zombies', 'pants' })
720
local expected = {}
8-
assert.are.same(expected, result)
21+
assert_lists_are_same(expected, result)
922
end)
1023

1124
it('detects simple anagram', function()
1225
local detector = Anagram:new('ant')
1326
local result = detector:match({ 'tan', 'stand', 'at' })
1427
local expected = { 'tan' }
15-
assert.are.same(expected, result)
28+
assert_lists_are_same(expected, result)
1629
end)
1730

1831
it('does not detect false positives', function()
1932
local detector = Anagram:new('galea')
2033
local result = detector:match({ 'eagle' })
2134
local expected = {}
22-
assert.are.same(expected, result)
35+
assert_lists_are_same(expected, result)
2336
end)
2437

2538
it('detects multiple anagrams', function()
2639
local detector = Anagram:new('master')
2740
local result = detector:match({ 'stream', 'pigeon', 'maters' })
2841
local expected = { 'stream', 'maters' }
29-
assert.are.same(expected, result)
42+
assert_lists_are_same(expected, result)
3043
end)
3144

3245
it('does not detect anagram subsets', function()
3346
local detector = Anagram:new('good')
3447
local result = detector:match({ 'dog', 'goody' })
3548
local expected = {}
36-
assert.are.same(expected, result)
49+
assert_lists_are_same(expected, result)
3750
end)
3851

3952
it('detects anagram', function()
4053
local detector = Anagram:new('listen')
4154
local result = detector:match({ 'enlists', 'google', 'inlets', 'banana' })
4255
local expected = { 'inlets' }
43-
assert.are.same(expected, result)
56+
assert_lists_are_same(expected, result)
4457
end)
4558

4659
it('detects multiple anagrams', function()
4760
local detector = Anagram:new('allergy')
4861
local result = detector:match({ 'gallery', 'ballerina', 'regally', 'clergy', 'largely', 'leading' })
4962
local expected = { 'gallery', 'regally', 'largely' }
50-
assert.are.same(expected, result)
63+
assert_lists_are_same(expected, result)
5164
end)
5265

5366
it('detects anagrams case-insensitively', function()
5467
local detector = Anagram:new('Orchestra')
5568
local result = detector:match({ 'cashregister', 'Carthorse', 'radishes' })
5669
local expected = { 'Carthorse' }
57-
assert.are.same(expected, result)
70+
assert_lists_are_same(expected, result)
5871
end)
5972
end)

0 commit comments

Comments
 (0)