Code golf: find all anagrams

后端 未结 8 2023
再見小時候
再見小時候 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:25

    Powershell, 104 97 91 86 83 chars

    $k=@{};$input|%{$k["$([char[]]$_|%{$_+0}|sort)"]+=@($_)}
    $k.Values|?{$_[1]}|%{"$_"}
    

    Update for the new requirement (+8 chars):

    To exclude the words that only differ in capitalization, we could just remove the duplicates (case-insensitvely) from the input list, i.e. $input|sort -u where -u stands for -unique. sort is case-insenstive by default:

    $k=@{};$input|sort -u|%{$k["$([char[]]$_|%{$_+0}|sort)"]+=@($_)} 
    $k.Values|?{$_[1]}|%{"$_"} 
    

    Explanation of the [char[]]$_|%{$_+0}|sort -part

    It's a key for the hashtable entry under which anagrams of a word are stored. My initial solution was: $_.ToLower().ToCharArray()|sort. Then I discovered I didn't need ToLower() for the key, as hashtable lookups are case-insensitive.

    [char[]]$_|sort would be ideal, but sorting of the chars for the key needs to be case-insensitive (otherwise Cab and abc would be stored under different keys). Unfortunately, sort is not case-insenstive for chars (only for strings).

    What we need is [string[]][char[]]$_|sort, but I found a shorter way of converting each char to string, which is to concat something else to it, in this case an integer 0, hence [char[]]$_|%{$_+0}|sort. This doesn't affect the sorting order, and the actual key ends up being something like: d0 o0 r0 w0. It's not pretty, but it does the job :)

    0 讨论(0)
  • 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
    0 讨论(0)
提交回复
热议问题