ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# filippigianluca's solution

## to Anagram in the Julia Track

Published at Feb 22 2021 · 0 comments
Instructions
Test suite
Solution

An anagram is a rearrangement of letters to form a new word. Given a word and a list of candidates, select the sublist of anagrams of the given word.

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

## Source

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

## Version compatibility

This exercise has been tested on Julia versions >=1.0.

## Submitting Incomplete Solutions

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

### runtests.jl

``````using Test

include("anagram.jl")

@testset "no matches" begin
@test detect_anagrams("diaper", ["hello", "world", "zombies", "pants"]) == []
end

@testset "detects simple anagram" begin
@test detect_anagrams("ant", ["tan", "stand", "at"]) == ["tan"]
end

@testset "does not detect false positives" begin
@test detect_anagrams("galea", ["eagle"]) == []
end

@testset "detects two anagrams" begin
@test detect_anagrams("master", ["stream", "pigeon", "maters"]) == ["stream", "maters"]
end

@testset "does not detect anagram subsets" begin
@test detect_anagrams("good", ["dog", "goody"]) == []
end

@testset "detects anagram" begin
@test detect_anagrams("listen", ["enlists", "google", "inlets", "banana"]) == ["inlets"]
end

@testset "detects three anagrams" begin
@test detect_anagrams("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]) == ["gallery", "regally", "largely"]
end

@testset "detects multiple anagrams with different case" begin
@test detect_anagrams("nose", ["Eons", "ONES"]) == ["Eons", "ONES"]
end

@testset "does not detect identical words" begin
@test detect_anagrams("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"]) == ["cron"]
end

@testset "does not detect non-anagrams with identical checksum" begin
@test detect_anagrams("mass", ["last"]) == []
end

@testset "detects anagrams case-insensitively" begin
@test detect_anagrams("Orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "detects anagrams using case-insensitive subject" begin
@test detect_anagrams("Orchestra", ["cashregister", "carthorse", "radishes"]) == ["carthorse"]
end

@testset "detects anagrams using case-insensitive possible matches" begin
@test detect_anagrams("orchestra", ["cashregister", "Carthorse", "radishes"]) == ["Carthorse"]
end

@testset "does not detect an anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == []
end

@testset "does not detect a word as its own anagram" begin
@test detect_anagrams("banana", ["Banana"]) == []
end

@testset "does not detect a anagram if the original word is repeated" begin
@test detect_anagrams("go", ["go Go GO"]) == []
end

@testset "anagrams must use all letters exactly once" begin
@test detect_anagrams("tapper", ["patter"]) == []
end

@testset "words are not anagrams of themselves (case-insensitive)" begin
@test detect_anagrams("BANANA", ["BANANA", "Banana", "banana"]) == []
end

@testset "words other than themselves can be anagrams" begin
@test detect_anagrams("LISTEN", ["Listen", "Silent", "LISTEN"]) == ["Silent"]
end

@testset "capital word is not own anagram" begin
@test detect_anagrams("BANANA", ["Banana"]) == []
end``````
``````#=
An anagram is a rearrangement of letters to form a new word. Given a word and a
list of candidates, select the sublist of anagrams of the given word.
Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana"
the program should return a list containing "inlets".

INPUT:
* subject    : word for wich find the anagrams [String]
* candidates : list of words where to find which ones are anagrams [Array of Strings]

OUTPUT:
* out : list of "candidates" that are anagrams of "subject" [Array of Strings]
=#

using Combinatorics

function detect_anagrams(subject, candidates)

# take lower case of "subject", collect String into Char and sort them alphabetically
s_Lower     = lowercase(subject);
s_LowerSort = collect(s_Lower) |> sort

# loop over each candidate to find anagrams of "subject"
out       = String[]
out_Lower = String[]

for ci in candidates

ci_Lower     = lowercase(ci)
ci_LowerSort = collect(ci_Lower) |> sort

# conditions:
# 1. lower-case sorted Char String are identical
# 2. different from previous_Lowery saved (case-insensitive)
# 3. is not equal to subject (case-insensitive)
if s_LowerSort == ci_LowerSort &&
!in(join(ci_Lower), out_Lower) &&
join(ci_Lower) != s_Lower

push!(out, ci);
push!(out_Lower, ci_Lower)
end
end

return out

end``````