The weekly challenge 242 - Task 2: Flip Matrix
1 #!/usr/bin/perl
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 flip_matrix([1, 1, 0], [1, 0, 1], [0, 0, 0]);
47 flip_matrix([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0]);
48
49 sub flip_matrix {
50 my @rows = @_;
51 my @result;
52 print "Input: (";
53 foreach my $row (@rows) {
54 print "[" . join(", ", @$row) . "],";
55 }
56 print ")\n";
57 foreach my $row (@rows) {
58 push @result, [ inverted( reverse @$row ) ];
59 }
60 print "Ouput: (";
61 foreach my $row (@result) {
62 print "[" . join(", ", @$row) . "],";
63 }
64 print ")\n";
65 }
66
67 sub inverted {
68 my @array = @_;
69 my @result = ();
70 foreach my $elem (@array) {
71 push @result, $elem == 0 ? 1 : 0;
72 }
73 return @result;
74 }