Mercurial > coderloop
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/reverse_seq.pl Tue Oct 18 01:17:49 2011 -0700 1.3 @@ -0,0 +1,43 @@ 1.4 +#!/usr/bin/perl 1.5 + 1.6 +$a = "/home/r/coderloop-test/reversesequence-a.in"; 1.7 +$b = "/home/r/coderloop-test/reversesequence-b.in"; 1.8 +$c = "/home/r/coderloop-test/reversesequence-c.in"; 1.9 + 1.10 +%test_map = ("a" => "b", "b" => "c"); 1.11 + 1.12 +sub create_map{ 1.13 + ## creates a "linked map" from a string representing 1.14 + ## the input file. 1.15 + (my $file) = @_; 1.16 + my %map; 1.17 + open FILE, "<".$file or die $!; 1.18 + for (<FILE>){ chomp; /^(\w+)\W+(\w+)$/; $map{$1} = $2;} 1.19 + close FILE; 1.20 + return %map;} 1.21 + 1.22 +sub find_beginning{ 1.23 + ## takes a map representing a linked list and 1.24 + ## returns the beginning of that list. 1.25 + (my %map) = @_; 1.26 + my %val_set = map {$_ => 1} (values %map); 1.27 + my $start; 1.28 + foreach (keys %map){ 1.29 + if (!($val_set{$_})){$start = $_;last}} 1.30 + return $start;} 1.31 + 1.32 +sub trace_map{ 1.33 + ## crawls through a map representing a linked-list 1.34 + ## and converts it into an actual array structure. 1.35 + (my %map) = @_; 1.36 + my $position = &find_beginning(%map); 1.37 + my @array = ($position); 1.38 + while(my $new_position = $map{$position}){ 1.39 + push (@array, $new_position); 1.40 + $position = $new_position;} 1.41 + return @array;} 1.42 + 1.43 + 1.44 + 1.45 +if (defined @ARGV){ 1.46 + map {print;print"\n";} reverse(trace_map(create_map($ARGV[0])));}