Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Powershell, <del>104</del> <del>97</del> <del>91</del> <del>86</del> 83 chars</h2> <pre><code>$k=@{};$input|%{$k["$([char[]]$_|%{$_+0}|sort)"]+=@($_)} $k.Values|?{$_[1]}|%{"$_"} </code></pre> <h3>Update for the new requirement (+8 chars):</h3> <p>To exclude the words that only differ in capitalization, we could just remove the duplicates (case-insensitvely) from the input list, i.e. <code>$input|sort -u</code> where <code>-u</code> stands for <code>-unique</code>. <code>sort</code> is case-insenstive by default:</p> <pre><code>$k=@{};$input|sort -u|%{$k["$([char[]]$_|%{$_+0}|sort)"]+=@($_)} $k.Values|?{$_[1]}|%{"$_"} </code></pre> <h3>Explanation of the <code>[char[]]$_|%{$_+0}|sort</code> -part</h3> <p>It's a key for the hashtable entry under which anagrams of a word are stored. My initial solution was: <code>$_.ToLower().ToCharArray()|sort</code>. Then I discovered I didn't need <code>ToLower()</code> for the key, as hashtable lookups are case-insensitive. </p> <p><code>[char[]]$_|sort</code> would be ideal, but sorting of the chars for the key needs to be case-insensitive (otherwise <code>Cab</code> and <code>abc</code> would be stored under different keys). Unfortunately, <code>sort</code> is not case-insenstive for chars (only for strings). </p> <p>What we need is <code>[string[]][char[]]$_|sort</code>, 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 <code>0</code>, hence <code>[char[]]$_|%{$_+0}|sort</code>. This doesn't affect the sorting order, and the actual key ends up being something like: <code>d0 o0 r0 w0</code>. It's not pretty, but it does the job :) </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload