Code golf: find all anagrams

后端 未结 8 2122
再見小時候
再見小時候 2021-02-04 06:00

A word is an anagram if the letters in that word can be re-arranged to form a different word.

Task:

  • The shortest source code by character count to find

8条回答
  •  自闭症患者
    2021-02-04 06:27

    Haskell, 147 chars

    prior sizes: 150 159 chars

    import Char
    import List
    x=sort.map toLower
    g&a=g(x a).x
    main=interact$unlines.map unwords.filter((>1).length).groupBy((==)&).sortBy(compare&).lines
    

    This version, at 165 chars satisifies the new, clarified rules:

    import Char
    import List
    y=map toLower
    x=sort.y
    g&f=(.f).g.f
    w[_]="";w a=show a++"\n"
    main=interact$concatMap(w.nubBy((==)&y)).groupBy((==)&x).sortBy(compare&x).lines
    

    This version handles:

    1. Words in the input that differ only by case should only count as one word
    2. The output needs to be one anagram set per line, but extra punctuation is acceptable

提交回复
热议问题