Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>[Assuming you can reuse the source letters within one word]: For each word in your dictionary list, construct two arrays of letters - one for the candidate word and one for the input string. Subtract the input array-of-letters from the word array-of-letters and if there weren't any letters left over, you've got a match. Code to do that looks like this:</p> <pre><code>def findWordsWithReplacement(sentence) out=[] splitArray=sentence.downcase.split(//) `cat /usr/share/dict/words`.each{|word| if (word.strip!.downcase.split(//) - splitArray).empty? out.push word end } return out end </code></pre> <p>You can call that function from the irb debugger like so:</p> <pre><code>output=findWordsWithReplacement("some input string"); puts output.join(" ") </code></pre> <p>...or here's a wrapper you could use to call the function interactively from a script:</p> <pre><code>puts "enter the text." ARGF.each {|line| puts "working..." out=findWordsWithReplacement(line) puts out.join(" ") puts "there were #{out.size} words." } </code></pre> <p>When running this on a Mac, the output looks like this:</p> <blockquote> <p>$ ./findwords.rb<br> enter the text.<br> Ziegler's Giant Bar<br> working...<br> A a aa aal aalii Aani Ab aba abaiser abalienate Abantes Abaris abas abase abaser Abasgi abasia Abassin abatable abate abater abatis abaze abb Abba abbas abbasi abbassi abbatial abbess Abbie Abe abear Abel abele Abelia Abelian Abelite abelite abeltree Aberia aberrant aberrate abet abettal Abie Abies abietate abietene abietin Abietineae Abiezer Abigail abigail abigeat abilla abintestate<br> [....]<br> Z z za Zabaean zabeta Zabian zabra zabti zabtie zag zain Zan zanella zant zante Zanzalian zanze Zanzibari zar zaratite zareba zat zati zattare Zea zeal zealless zeallessness zebra zebrass Zebrina zebrine zee zein zeist zel Zelanian Zeltinger Zen Zenaga zenana zer zest zeta ziara ziarat zibeline zibet ziega zieger zig zigzag zigzagger Zilla zing zingel Zingiber zingiberene Zinnia zinsang Zinzar zira zirai Zirbanit Zirian Zirianian Zizania Zizia zizz<br> there were 6725 words.</p> </blockquote> <p>That is well over 4500 words, but that's because the Mac word dictionary is pretty large. If you want to reproduce Knuth's results exactly, download and unzip Knuth's dictionary from here: <a href="http://www.packetstormsecurity.org/Crackers/wordlists/dictionaries/knuth_words.gz" rel="nofollow noreferrer">http://www.packetstormsecurity.org/Crackers/wordlists/dictionaries/knuth_words.gz</a> and replace "/usr/share/dict/words" with the path to wherever you've unpacked the substitute directory. If you did it right you'll get 4514 words, ending in this collection:</p> <blockquote> <p>zanier zanies zaniness Zanzibar zazen zeal zebra zebras Zeiss zeitgeist Zen Zennist zest zestier zeta Ziegler zig zigging zigzag zigzagging zigzags zing zingier zings zinnia </p> </blockquote> <p>I believe that answers the original question.</p> <p>Alternatively, the questioner/reader might have wanted to list all the words one can construct from a string <em>without</em> reusing any of the input letters. My suggested code to accomplish that works as follows: Copy the candidate word, then for each letter in the input string, destructively remove the first instance of that letter from the copy (using "slice!"). If this process absorbs all the letters, accept that word.</p> <pre><code>def findWordsNoReplacement(sentence) out=[] splitInput=sentence.downcase.split(//) `cat /usr/share/dict/words`.each{|word| copy=word.strip!.downcase splitInput.each {|o| copy.slice!(o) } out.push word if copy=="" } return out end </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