Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So we have the following:</p> <p>n = length of input word<br/> L = lines in dictionary file</p> <p>If n tends to be small and L tends to be huge, might we be better off finding all permutations of the input word and looking for those, rather than doing something (like sorting) to all L lines of the dictionary file? (Actually, since finding all permutations of a word is O(n!), and we have to run through the entire dictionary file once for each word, maybe not, but I wrote the code anyway.)</p> <p>This is Perl - I know you wanted command-line operations but I don't have a way to do that in shell script that's not super-hacky:</p> <pre><code>sub dedupe { my (@list) = @_; my (@new_list, %seen_entries, $entry); foreach $entry (@list) { if (!(defined($seen_entries{$entry}))) { push(@new_list, $entry); $seen_entries{$entry} = 1; } } return @new_list; } sub find_all_permutations { my ($word) = @_; my (@permutations, $subword, $letter, $rest_of_word, $i); if (length($word) == 1) { push(@permutations, $word); } else { for ($i=0; $i&lt;length($word); $i++) { $letter = substr($word, $i, 1); $rest_of_word = substr($word, 0, $i) . substr($word, $i + 1); foreach $subword (find_all_permutations($rest_of_word)) { push(@permutations, $letter . $subword); } } } return @permutations; } $words_file = '/usr/share/dict/words'; $word = 'table'; @all_permutations = dedupe(find_all_permutations($word)); foreach $permutation (@all_permutations) { if (`grep -c -m 1 ^$permutation\$ $words_file` == 1) { print $permutation . "\n"; } } </code></pre>
 

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