Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: finding number of contiguous letters

by mw487 (Sexton)
on May 23, 2007 at 16:07 UTC ( #617053=note: print w/replies, xml ) Need Help??


in reply to finding number of contiguous letters

"I need to take a string and find the number of 3 contiguous letters."

I notice that the results so far seem to rely upon an array for the result. I think that can lead to duplication- the array may have the same three letters at various indices, which will give an erroneously high result. Consider the test case "Mississippi", which has "Mis", "iss", "ssi", "sis", "sip", and "ppi"- 6 strings of three letters, eh? I think a hash is better to store the results, then count the number of keys.

  • Comment on Re: finding number of contiguous letters

Replies are listed 'Best First'.
Re^2: finding number of contiguous letters
by blazar (Canon) on May 23, 2007 at 20:33 UTC

    Well, just chain a map-based solution like "mine" with the usual grep-based "uniq" or absorb the latter into the map():

    my %saw; my @parts = map { my $s=substr $str, $_, 3; $saw{$s}++ ? () : $s } 0..length($str)-3;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://617053]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (7)
As of 2020-10-27 21:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (258 votes). Check out past polls.

    Notices?