Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: substrings that consist of repeating characters

by GrandFather (Saint)
on Sep 27, 2020 at 22:26 UTC ( [id://11122273]=note: print w/replies, xml ) Need Help??


in reply to substrings that consist of repeating characters

In Perl length is cheap so calculate it when you need it. The following code is a little more Perlish but, other than using a threshold to drop out short strings, is similar to your code. For varieties sake the regex has changed slightly to be a little easier to grok:

use strict; use warnings; my $string = "AAATTTAGTTCTTAAGGCTGACATCGGTTTACGTCAGCGTTACCCCCCAAGTTATT +GGGGACTTT"; my @runs; my $threshold = 3; length $1 >= $threshold && (push @runs, $1) while $string =~ /(A+|C+|G ++|T+)/g; @runs = sort {length($b) <=> length($a)} @runs; printf "@runs\n";

Prints:

CCCCCC GGGG AAA TTT TTT TTT
Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

Replies are listed 'Best First'.
Re^2: substrings that consist of repeating characters
by LanX (Saint) on Sep 27, 2020 at 23:33 UTC
    > For varieties sake the regex has changed slightly to be a little easier to grok:

    Oh ... we got a one-liner :)

    DB<56> $_ = "AAATTTAGTTCTTAAGGCTGACATCGGTTTACGTCAGCGTTACCCCCCAAGTTAT +TGGGGACTTT"; DB<57> $threshold = 3; DB<58> x sort { length($b) <=> length($a) } grep { length >= $thresh +old } /(A+|C+|G+|T+)/g 0 'CCCCCC' 1 'GGGG' 2 'AAA' 3 'TTT' 4 'TTT' 5 'TTT' DB<59>

    EDIT

    and for the original problem

    DB<59> x sort { length($b) <=> length($a) } /(AA+|CC+|GG+|TT+)/g 0 'CCCCCC' 1 'GGGG' 2 'AAA' 3 'TTT' 4 'TTT' 5 'TTT' 6 'TT' 7 'TT' 8 'AA' 9 'GG' 10 'GG' 11 'TT' 12 'AA' 13 'TT' 14 'TT' DB<60>

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 05:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found