So I was doing the bash exercise anagram
and all tests except one are passing.
For all bash exercises that I have done so far, I did not use any external, non-builtin features just to exhaust what are the possibilities with it. Going back to anagram
, the one test that is failing is against this:
user@debian:~/programming/exercism.org/bash/anagram$ cat -n anagram.bat
...
83 @test "anagrams must use all letters exactly once" {
84 #[[ $BATS_RUN_SKIPPED == "true" ]] || skip
85 run bash anagram.sh "tapper" "patter"
86 assert_success
87 refute_output
88 }
And the test report:
user@debian:~/programming/exercism.org/bash/anagram$ bats anagram.bats
anagram.bats
✓ no matches
✓ detects two anagrams
✓ does not detect anagram subsets
✓ detects anagram
✓ detects three anagrams
✓ detects multiple anagrams with different case
✓ does not detect non-anagrams with identical checksum
✓ detects anagrams case-insensitively
✓ detects anagrams using case-insensitive subject
✓ detects anagrams using case-insensitive possible matches
✓ does not detect a anagram if the original word is repeated
✗ anagrams must use all letters exactly once
(from function `refute_output' in file bats-extra.bash, line 611,
in test file anagram.bats, line 87)
`refute_output' failed
-- output non-empty, but expected no output --
output : patter
--
✓ words are not anagrams of themselves
✓ words are not anagrams of themselves even if letter case is partially different
✓ words are not anagrams of themselves even if letter case is completely different
✓ words other than themselves can be anagrams
16 tests, 1 failure
The failure (I think) is due to the character t
on the test word patter
being repeated. To resolve that, I’m thinking of creating a sort function where it walks through the entire string and checks for any duplicates. But since I’m thinking of implementing sort in pure bash function, are they any guides/tips you can provide? I understand that it may be a bit silly to avoid the external command sort
, but it’s just me.
I did not include my code as it still messy am looking only for some pointers on how to create a bash sort function.
As always, thanks a lot!