lrep-grondilu-p5

#!/usr/bin/perl
use strict;
use warnings;

# parsing data
my $dna = <DATA>;
my $k = <DATA>;
my @edge = map [ split / +/ ], <DATA>;

# enumerating nodes
my %node; for my $edge (@edge) { $node{$edge->[$_]}++ for 0, 1 }
my @node = keys %node;

# enumerating parents
my %parent; $parent{$_->[1]} = $_->[0] for @edge;

# building tree-like structure
my $tree = {};
$tree->{$_->[0]}{$_->[1]} = [ @$_[2, 3] ] for @edge;

sub count_offspring {
    my $node = shift;
    return 1 unless keys %{$tree->{$node}};
    my $count;
    $count += count_offspring($_) for keys %{$tree->{$node}};
    return $count;
}

sub build_substr {
    my $node = shift;
    my $substr = '';
    while (exists $parent{$node}) {
        my $il = $tree->{$parent{$node}}{$node};
        $substr = substr($dna, $$il[0]-1, $$il[1]) . $substr;
        $node = $parent{$node};
    }
    return $substr;
}

my $found = '';
for my $node (@node) {
    my $count = count_offspring $node;
    if ($count >= $k) {
        my $substr = build_substr $node;
        $found = $substr if length($substr) > length($found);
    }
}
print "$found\n";


__DATA__
CATACATAC$
2
node1 node2 1 1
node1 node7 2 1
node1 node14 3 3
node1 node17 10 1
node2 node3 2 4
node2 node6 10 1
node3 node4 6 5
node3 node5 10 1
node7 node8 3 3
node7 node11 5 1
node8 node9 6 5
node8 node10 10 1
node11 node12 6 5
node11 node13 10 1
node14 node15 6 5
node14 node16 10 1

See Also

afrq-grondilu.raku

Counting Disease Carriers

aspc-grondilu.raku

Introduction to Alternative Splicing

cons-grondilu.raku

Consensus and Profile

conv-grondilu.raku

Comparing Spectra with the Spectral Convolution

cstr-grondilu.raku

Creating a Character Table from Genetic Strings

ctbl-grondilu.raku

Creating a Character Table

dbpr-grondilu.raku

Introduction to Protein Databases

dna-gerdr.raku

Counting DNA Nucleotides

dna-grondilu.raku

Counting DNA Nucleotides

eubt-grondilu.raku

Enumerating Unrooted Binary Trees

eval-grondilu.raku

Expected Number of Restriction Sites

fib-grondilu.raku

Rabbits and Recurrence Relations

fibd-grondilu.raku

Mortal Fibonacci Rabbits

gc-gerdr.raku

Computing GC Content

grph-grondilu.raku

Overlap Graphs

hamm-grondilu.raku

Counting Point Mutations

iev-grondilu.raku

Calculating Expected Offspring

indc-grondilu.raku

Independent Segregation of Chromosomes

iprb-grondilu.raku

Mendel's First Law

itwv-grondilu.raku

Finding Disjoint Motifs in a Gene

lcsq-grondilu.raku

Finding a Shared Spliced Motif

lia-grondilu.raku

Independent Alleles

mmch-grondilu.raku

Maximum Matchings and RNA Secondary Structures

mprt-grondilu.raku

Finding a Protein Motif

mrna-grondilu.raku

Inferring mRNA from Protein

nwck-grondilu.raku

Distances in Trees

orf-grondilu.raku

Open Reading Frames

pmch-grondilu.raku

Perfect Matchings and RNA Secondary Structures

pper-grondilu.raku

Partial Permutations

prob-grondilu.raku

Introduction to Random Strings

qrt-grondilu.raku

Quartets

README.md

revc-gerdr.raku

Complementing a Strand of DNA

rna-gerdr.raku

Transcribing DNA into RNA

rstr-grondilu.raku

Matching Random Motifs

sexl-grondilu.raku

Sex-Linked Inheritance

sgra-grondilu.raku

Using the Spectrum Graph to Infer Peptides

spec-grondilu.raku

Inferring Protein from Spectrum

sseq-grondilu.raku

Finding a Spliced Motif

subs-grondilu.raku

Finding a Motif in DNA

suff-grondilu.raku

Encoding Suffix Trees

tran-grondilu.raku

Transitions and Transversions

trie-grondilu.raku

Introduction to Pattern Matching

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.

Built with Podlite — the markup and publishing tools behind this site.