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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
#!/usr/bin/perl
=begin comment
Task #2 > Conflict Intervals
============================
You are given a list of intervals.
Write a script to find out if the current interval conflicts with any of the
previous intervals.
EXAMPLE
Input: @intervals = ( [1, 4], [3, 5], [6, 8], [12, 13], [3, 20] );
Output: ( [3, 5], [3, 20] )
- The 1st interval [1, 4] does not have any previous intervals to compare
with, so skip it.
- The 2nd interval [3, 5] does conflict with previous interval [1, 4].
- The 3rd interval [6, 8] does not conflict with any of the previous
intervals [1, 4] and [3, 5], so skip it.
- The 4th interval [12, 13] again does not conflict with any previous
intervals [1, 4], [3, 5], or [6, 8] so skip it.
- The 5th interval [3, 20] conflicts with the first interval [1, 4].
Input: @intervals = ( [3, 4], [5, 7], [6, 9], [10, 12], [13, 15] );
Output: ( [6, 9] );
=end comment
=cut
use strict;
use warnings;
use utf8;
use open ":std", ":encoding(UTF-8)";
use Term::ANSIColor;
use Scalar::Util qw( reftype );
use JSON qw( decode_json );
# Here, our subroutine to test our intervals (PWC solution)
sub find_conflict_intervals {
my $set_ref = shift @_;
my @conflicts = ();
my @passed = ();
if (reftype $set_ref ne "ARRAY") {
print "Array reference not found\n";
return 0;
}
foreach my $set (@$set_ref) {
my ($a1, $a2) = sort { $a <=> $b } @$set;
my $conflict = grep {
my ($b1, $b2) = sort { $a <=> $b } @$_;
($a1 >= $b1 && $a1 <= $b2) ||
($a2 >= $b1 && $a2 <= $b2) ||
($a1 <= $b1 && $a2 >= $b2);
} @passed;
push @passed, [$a1, $a2] if not $conflict;
push @conflicts, [$a1, $a2] if $conflict;
}
return \@conflicts;
}
# Followed by some utilities to test our solution
sub parse_test_case {
my $filename = shift;
open my $fh, "<", $filename
or die "Could not open '$filename' - $!\n";
read $fh, my $json, -s $fh;
close $fh;
my $data = decode_json $json;
return $data;
}
sub sort_compare {
return ($a->[0] <=> $b->[0]) || ($a->[1] <=> $b->[1]);
}
sub compare_interval_sets {
my ($output, $compare) = @_;
if (reftype $output ne "ARRAY" || reftype $compare ne "ARRAY") {
print "Output not formatted correctly\n";
return 0;
}
my $length = scalar @$output;
if ($length != scalar @$compare) {
return 0;
}
my @sorted_output = sort sort_compare @$output;
my @sorted_compare = sort sort_compare @$compare;
for my $i (0 .. $length) {
my $test = $sorted_output[0];
my $answer = $sorted_compare[0];
if ($test->[0] != $answer->[0] || $test->[1] != $answer->[1]) {
return 0;
}
}
return 1;
}
sub assert_conflicts {
my ($input, $output) = @_;
my $compare = find_conflict_intervals $input;
my $equal = compare_interval_sets $output, $compare;
print color("green"), "Passed \x{2690}\n", color("reset") if $equal;
print color("red"), "Failed \x{2715}\n", color("reset") if not $equal;
}
# And our test runner
sub main {
my $target = shift @ARGV // "../test_cases/ch2";
if (-e -r -f $target) {
my $json = parse_test_case $target;
my $input = %$json{"input"};
my $output = %$json{"output"};
print $target, ": ";
assert_conflicts $input, $output;
return;
} elsif (-e -r -d _) {
$target =~ s/^(.*?)\/?$/$1\//; # check for trailing slash
opendir my $dh, $target
or die "Could not open '$target' - $!\n";
my @entries = readdir $dh;
foreach my $entry (@entries) {
if ($entry eq "." or $entry eq "..") {
next;
}
my $path = $target . $entry;
my $json = parse_test_case $path;
my $input = %$json{"input"};
my $output = %$json{"output"};
print $path, ": ";
assert_conflicts $input, $output;
}
closedir $dh;
return;
} else {
print "No test files found\n";
}
}
main();
|