Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Here is my effort. It finds all the patterns and also does a quick dictionary lookup for real words. If you don't have a dictionary text file you can select from a wide variety at the National Puzzlers' League -- Word Lists

my $str ='helloworldhellohellohihellohiworld'; my %hash; # grab all the substrings 2+ chars and count occurences in a hash for my $i ( 0 ..(length($str) -1) ) { $hash{ substr($str, $i, $_) }++ for 2.. (length($str) - $i); } # sort on occurences and then in alphabetical order # only select elements that occur >1 times using grep my @order = sort { $hash{$b} <=> $hash{$a} || $a cmp $b } grep { $hash{$_} > 1 } keys %hash; print "\nPatterns found:\n\n"; print "$hash{$_} occurrences of $_ \n" for @order; # now grab dictionary file into a hash # you can save memory using Search::Dict open DICT, "c:/windows/desktop/dict.txt" or die $!; while ($word = <DICT>) { chomp $word; $dict{$word}++; } @words = grep { defined $dict{$_} } @order; print "\nReal words found in dictionary:\n\n"; print "$hash{$_} occurrences of $_ \n" for @words; # remove substrings of larger words @words = sort { length $b <=> length $a } @words; for $i ( 0 .. $#words - 1 ) { for $j ( $i + 1 .. $#words ) { $hash{$words[$j]} = 0 if $words[$i] =~ m/\Q$words[$j]/ and $ha +sh{$words[$i]} == $hash{$words[$j]}; } } # regenerate sort order grepping out unwanted substrings (set occurenc +es to zero above) @words = sort { $hash{$b} <=> $hash{$a} || $a cmp $b } grep { $hash{$_} } @words; print "\nBest Matches:\n\n"; print "$hash{$_} occurrences of $_ \n" for @words; __END__ # sample output Patterns found: 4 occurrences of el 4 occurrences of ell 4 occurrences of ello 4 occurrences of he 4 occurrences of hel 4 occurrences of hell 4 occurrences of hello 4 occurrences of ll 4 occurrences of llo 4 occurrences of lo 3 occurrences of elloh 3 occurrences of helloh 3 occurrences of lloh 3 occurrences of loh 3 occurrences of oh 2 occurrences of ellohi 2 occurrences of hellohi 2 occurrences of hi 2 occurrences of ld 2 occurrences of llohi 2 occurrences of lohi 2 occurrences of ohi 2 occurrences of or 2 occurrences of orl 2 occurrences of orld 2 occurrences of rl 2 occurrences of rld 2 occurrences of wo 2 occurrences of wor 2 occurrences of worl 2 occurrences of world Real words found in dictionary: 4 occurrences of el 4 occurrences of ell 4 occurrences of he 4 occurrences of hell 4 occurrences of hello 4 occurrences of lo 3 occurrences of oh 2 occurrences of hi 2 occurrences of or 2 occurrences of wo 2 occurrences of world Best Matches: 4 occurrences of hello 3 occurrences of oh 2 occurrences of hi 2 occurrences of world

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print


In reply to Re: Pattern Finding by tachyon
in thread Pattern Finding by artist

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (8)
    As of 2014-12-28 03:54 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (178 votes), past polls