I think the primes thing was actually harder than what I did. Here is my 15-line Elm solution where you could also reuse the breakdown of how many of which letters were used. I could have turned it into 1 long line with the 3 import statements, but I won't do that to you. :)
Read this code from the bottom up. I got used to writing it like this in F# because of its single-pass compiler. This is not a requirement of Elm, though.
importDictexposing(Dict)importStringimportCharupdateCountcountM=casecountMof-- no previous count of letter, set to 1Nothing->Just1-- saw same letter again, incrementJusti->Just(i+1)updateAnalysischardict=Dict.update(Char.toLowerchar)updateCountdictanalyzeWordword=String.foldrupdateAnalysisDict.emptywordisAnagramword1word2=-- built-in equality superpowersanalyzeWordword1==analyzeWordword2
Don't take my word for it.
Here is a full program with tests that you can run over at elm-lang.org/try. Paste the code below in the left window and hit the Compile button.
I included type annotations for clarity, but they are not necessary.
importHtmlexposing(Html,text,div,span,u)importHtml.Attributesexposing(style)importDictexposing(Dict)importStringimportChar-- anagram functionstypealiasWordAnalysis=DictCharIntupdateCount:MaybeInt->MaybeIntupdateCountcountM=casecountMof-- no previous count of letter, set to 1Nothing->Just1-- saw same letter again, incrementJusti->Just(i+1)updateAnalysis:Char->WordAnalysis->WordAnalysisupdateAnalysischardict=Dict.update(Char.toLowerchar)updateCountdictanalyzeWord:String->WordAnalysisanalyzeWordword=String.foldrupdateAnalysisDict.emptywordisAnagram:String->String->BoolisAnagramword1word2=analyzeWordword1==analyzeWordword2-- test functionsanagramTests:List(String,String)anagramTests=[("stressed","desserts"),("stressed","deserts"),("marching","charming"),("happy","sad")]renderTest:(String,String)->HtmlmsgrenderTest(word1,word2)=div[][u[][textword1],text" and ",u[][textword2],ifisAnagramword1word2thenspan[style[("color","green"),("font-weight","bold")]][text" are anagrams"]elsespan[][text" are not anagrams"]]main=div[](List.maprenderTestanagramTests)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Edit: Added sort-based solution for Elm.
I think the primes thing was actually harder than what I did. Here is my 15-line Elm solution where you could also reuse the breakdown of how many of which letters were used. I could have turned it into 1 long line with the 3 import statements, but I won't do that to you. :)
Don't take my word for it.
Here is a full program with tests that you can run over at elm-lang.org/try. Paste the code below in the left window and hit the Compile button.