annotate src/reverse_seq.pl @ 0:307a81e46071 tip

initial committ
author Robert McIntyre <rlm@mit.edu>
date Tue, 18 Oct 2011 01:17:49 -0700
parents
children
rev   line source
rlm@0 1 #!/usr/bin/perl
rlm@0 2
rlm@0 3 $a = "/home/r/coderloop-test/reversesequence-a.in";
rlm@0 4 $b = "/home/r/coderloop-test/reversesequence-b.in";
rlm@0 5 $c = "/home/r/coderloop-test/reversesequence-c.in";
rlm@0 6
rlm@0 7 %test_map = ("a" => "b", "b" => "c");
rlm@0 8
rlm@0 9 sub create_map{
rlm@0 10 ## creates a "linked map" from a string representing
rlm@0 11 ## the input file.
rlm@0 12 (my $file) = @_;
rlm@0 13 my %map;
rlm@0 14 open FILE, "<".$file or die $!;
rlm@0 15 for (<FILE>){ chomp; /^(\w+)\W+(\w+)$/; $map{$1} = $2;}
rlm@0 16 close FILE;
rlm@0 17 return %map;}
rlm@0 18
rlm@0 19 sub find_beginning{
rlm@0 20 ## takes a map representing a linked list and
rlm@0 21 ## returns the beginning of that list.
rlm@0 22 (my %map) = @_;
rlm@0 23 my %val_set = map {$_ => 1} (values %map);
rlm@0 24 my $start;
rlm@0 25 foreach (keys %map){
rlm@0 26 if (!($val_set{$_})){$start = $_;last}}
rlm@0 27 return $start;}
rlm@0 28
rlm@0 29 sub trace_map{
rlm@0 30 ## crawls through a map representing a linked-list
rlm@0 31 ## and converts it into an actual array structure.
rlm@0 32 (my %map) = @_;
rlm@0 33 my $position = &find_beginning(%map);
rlm@0 34 my @array = ($position);
rlm@0 35 while(my $new_position = $map{$position}){
rlm@0 36 push (@array, $new_position);
rlm@0 37 $position = $new_position;}
rlm@0 38 return @array;}
rlm@0 39
rlm@0 40
rlm@0 41
rlm@0 42 if (defined @ARGV){
rlm@0 43 map {print;print"\n";} reverse(trace_map(create_map($ARGV[0])));}