Re: I dont get it... Why is everyone from science schools so obsessed with Perl and Python when we got C?
Posted: Thu Jul 18, 2019 5:29 pm
That's nicely faster than the Perl code and in many ways more readable.jalih wrote: ↑Thu Jul 18, 2019 4:46 pmI tried that approach with 8th to generate map keys instead of sorting word alphabets. It ran about the same speed as my original version. I got a little help from the 8th forums and version below is currently the fastest version for 8th:Execution time using "/usr/share/dict/british-english-insane" word list:
Code: Select all
m:new var, anamap a:new var, anakeys : s:sort \ s -- s a:new swap ' a:push s:each! ' n:cmp a:sort ( swap s:+ ) "" a:reduce ; : process-words \ word -- /^[a-z]+$/ r:match if r:str nip dup >r s:sort anamap @ over m:exists? if over m:@ r> a:push 2drop else swap a:new r> a:push m:! then then drop ; : read-and-check-words \ fname -- f:slurp ' process-words s:eachline ; : len>= \ key -- anakeys @ swap a:push drop ; : fetch-ana-list \ key array -- a:len 2 n:cmp 1 n:+ nip [ ' drop , ' len>= , ' len>= ] swap caseof ; : key-val-cmp \ key1 key2 anamap @ swap m:@ \ key1 anamap key2val 0 a:@ nip \ key1 anamap val2 swap rot \ val2 anamap key1 m:@ nip \ val2 key1val 0 a:@ nip \ val2 val1 swap \ val1 val2 s:cmp ; : sort-keys-by-first-word anakeys @ ' key-val-cmp a:sort drop ; : list-words \ value -- " " a:join . cr ; : app:main 0 args "/usr/share/dict/british-english-insane" ?: read-and-check-words anamap @ ' fetch-ana-list m:each drop sort-keys-by-first-word anamap @ anakeys @ m:@@ nip ' list-words a:each! drop anakeys @ a:len nip "\nAnagrams: %d\n" s:strfmt . bye ;
Code: Select all
real 0m10,689s user 0m9,992s sys 0m0,684s
I wonder whether C++ with sort templates would generate less greenhouse gases than the C programs. Although compiling might destroy a rainforest or two, the loss would quickly be compensated by a billion people using the resulting binary to improve their bananagrams.
Along different lines, do you think it might be possible to find anagrams in the insane British dictionary using interpreted Basic? Since no compiling would be needed, no rainforests would be harmed.