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