My script takes 25 secs on an old HP notebook or 10 secs on Mac Mini for 600 words, 612 paragraphs. I don't know how it scales though yet.
#!/usr/bin/perl use strict; use warnings; my $maxw = 600; # 200 my $maxp = 612; # 600_000 sub uniq { my %uniq; @uniq{@_} = (); return keys %uniq; } # uniq print {*STDERR} "Generate fake paragraphs\n"; my @para = map [uniq map int(rand $maxw),0..int(rand $maxw)],1..$maxp; print {*STDERR} "Sort\n"; @para = sort {@$a <=> @$b} @para; print {*STDERR} "Count reversed hash\n"; my %words_in_para; for(my $i=0;$i<=$#para;$i++){ foreach my $word(@{ $para[$i] }){ push @{ $words_in_para{$word} },$i; } } print {*STDERR} "Solving\n"; my $start_time = time(); my $i = 0; while ($i <= $#para){ my $para = $para[$i]; unless(grep 1 == @{ $words_in_para{$_} },@$para){ # all words appear somewhere else, delete the paragraph foreach my $word (@$para){ $words_in_para{$word} = [ grep $_ ne $i,@{ $words_in_para{$word} + } ]; } $para[$i] = []; } print {*STDERR} "$i/$maxp\r"; $i++; } my $end_time = time(); print "\n"; print join "\n",map {'['.join(' ',sort {$a<=>$b} @$_).']'} grep @$_,@p +ara; print "\n"; print $end_time - $start_time,"s\n";

In reply to Re^5: Help required in optimizing the output (PERL) by choroba
in thread Help required in optimizing the output (PERL) by randomid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.