aboutsummaryrefslogtreecommitdiff
path: root/challenge-057/mohammad-anwar/perl/ch-1a.pl
blob: 9418c608aaaec7728e81e19c8d5092a75ff6259e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;
use Data::Dump qw(dump);

my $tree_1 = {
    1 => [ [ 2,
             [ [ 4 ],
               [ 5 ],
             ],
           ],
           [ 3,
             [ [ 6 ],
               [ 7 ],
             ],
           ],
         ],
};

my $tree_2 = {
    1 => [ [ 2,
             [ [ 4 ],
               [ 5 ],
             ],
           ],
           [ 3,
             [ [ 6 ],
               [ 7,
                 [ [ 8 ],
                   [ 9 ],
                 ],
               ],
             ],
           ],
         ],
};

mirror($tree_1->{1});
mirror($tree_2->{1});

is (dump($tree_1), "{ 1 => [[3, [[7], [6]]], [2, [[5], [4]]]] }", "testing tree 1");
is (dump($tree_2), "{ 1 => [[3, [[7, [[9], [8]]], [6]]], [2, [[5], [4]]]] }", "testing tree 2");

done_testing;

sub mirror {
    my ($branch) = @_;

    ($branch->[0], $branch->[1]) = ($branch->[1], $branch->[0]);
    mirror($branch->[0][1]) if defined $branch->[0][1];
    mirror($branch->[1][1]) if defined $branch->[1][1];

    return $branch;
}