Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Halp with regex

Name: Anonymous 2008-08-04 3:54

Hey /prog/,
Lets say I have a file with many lines,
each line consists of a very very long random number (string)
Is it possible to write a regular expression to find the longest series of repeated numbers shared by all lines
aka
120123456789948761238746102347106416239847113958719385
217490812741234567890182472103571938547019112413956138756183756183756817356817356
192346112345678978932671658917612746183724681734682173649187245

(I bolded it for readability)

tldr;
how does I finded biggest repeaded series of numbers contained in all lines? (using regex or something one-line'ish)

Name: Anonymous 2008-08-06 3:37

use strict;
use List::Util qw(max);
use Benchmark qw(:all :hireswallclock);

my $loopcount=10;
my $linecount=(shift or 4);
my $linelength=(shift or 40000);

my @list=map{join "",map{int rand 10}1..$linelength}1..$linecount;

my $reg=qr/1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)
          |6(?=7)|7(?=8)|8(?=9)|9(?=0)|0(?=1)/x;

sub insert($$$){
    my($ref,$key,$pos)=@_;
   
    return if $ref->{$key} or length $key == 1;
    $ref->{$key}=$pos;
   
    insert($ref,(substr $key,1),$pos+1);
    insert($ref,(substr $key,0,length($key)-1),$pos);
}

sub finder(){
    my @sequences=map{
        my $matches={};
        insert($matches,$&,$-[0]) while /$reg+\d/g;
        $matches;
    } @list;
   
    my($first,@rest)=@sequences;
    my @matches=grep{
        my $key=$_;
        not grep{not $_->{$key}}@rest
    }keys %$first;

    my $len=max map{length $_}@matches;
    grep{$len==length $_}@matches
}

my @matches;
my $time=timeit($loopcount,sub{@matches=finder()});

print "Longest series of repeated numbers in $linecount lines $linelength charactes each:
    ",join " ",@matches,"\n";
print "$loopcount loops of code took:",timestr($time);


---

C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        0123 2345 4567 9012 3456 5678 7890 8901 1234 6789
10 loops of code took:5.12966 wallclock secs ( 5.13 usr +  0.00 sys =  5.13 CPU) @  1.95/s (n=10)
C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        0123 2345 4567 9012 3456 7890 5678 8901 1234 6789
10 loops of code took:5.1259 wallclock secs ( 5.13 usr +  0.00 sys =  5.13 CPU) @  1.95/s (n=10)
C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        89012
10 loops of code took:5.16758 wallclock secs ( 5.16 usr +  0.00 sys =  5.16 CPU) @  1.94/s (n=10)
C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        0123 2345 4567 9012 3456 5678 1234 6789
10 loops of code took:5.1835 wallclock secs ( 5.17 usr +  0.00 sys =  5.17 CPU) @  1.93/s (n=10)
C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        2345 4567 9012 7890 5678 1234 6789
10 loops of code took:5.03543 wallclock secs ( 5.03 usr +  0.00 sys =  5.03 CPU) @  1.99/s (n=10)
C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        9012 3456 0123 5678 7890 8901 4567 1234 6789
10 loops of code took:5.27253 wallclock secs ( 5.22 usr +  0.02 sys =  5.24 CPU) @  1.91/s (n=10)
C:\andrey>perl go.pl 4 40000
Longest series of repeated numbers in 4 lines 40000 charactes each:
        9012 3456 0123 7890 5678 8901 2345 4567 1234
10 loops of code took:5.13019 wallclock secs ( 5.13 usr +  0.00 sys =  5.13 CPU) @  1.95/s (n=10)


could someone time >>15-kun's program?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List