/
ch-1.pl
164 lines (133 loc) · 4.58 KB
/
ch-1.pl
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/perl
# https://perlweeklychallenge.org/blog/perl-weekly-challenge-050/
# Task #1
#
# Merge Intervals
# Write a script to merge the given intervals where ever possible.
#
# [2,7], [3,9], [10,12], [15,19], [18,22]
#
# The script should merge [2, 7] and [3, 9] together to return [2, 9].
#
# Similarly it should also merge [15, 19] and [18, 22] together to return [15, 22].
#
# The final result should be something like below:
#
# [2, 9], [10, 12], [15, 22]
use strict;
use warnings;
my $intervals = join ',', @ARGV;
if ($intervals) {
print merge($intervals);
}
else {
do_tests();
}
# Test cases borrowed from M. Anwar contributions.
# https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-050/mohammad-anwar/perl/ch-1.pl
sub do_tests {
print "TESTS\n";
test('[1,2]','[1,2]');
test('[1,2],[3,4]','[1,2],[3,4]');
test('[3,4],[1,2]','[1,2],[3,4]');
test('[2,5],[1,2]','[1,5]');
test('[2,5],[1,3]','[1,5]');
test('[2,5],[1,5]','[1,5]');
test('[2,5],[1,6]','[1,6]');
test('[2,5],[2,3]','[2,5]');
test('[2,5],[2,5]','[2,5]');
test('[2,5],[2,6]','[2,6]');
test('[2,5],[3,4]','[2,5]');
test('[2,5],[3,5]','[2,5]');
test('[2,5],[3,6]','[2,6]');
test('[2,5],[5,6]','[2,6]');
test('[1,3],[5,7],[-1,0]','[-1,0],[1,3],[5,7]');
test('[1,3],[5,7],[-1,1]','[-1,3],[5,7]');
test('[1,3],[5,7],[-1,2]','[-1,3],[5,7]');
test('[1,3],[5,7],[-1,3]','[-1,3],[5,7]');
test('[1,3],[5,7],[-1,4]','[-1,4],[5,7]');
test('[1,3],[5,7],[-1,5]','[-1,7]');
test('[1,3],[5,7],[-1,6]','[-1,7]');
test('[1,3],[5,7],[-1,7]','[-1,7]');
test('[1,3],[5,7],[-1,8]','[-1,8]');
test('[1,3],[5,7],[1,2]','[1,3],[5,7]');
test('[1,3],[5,7],[1,3]','[1,3],[5,7]');
test('[1,3],[5,7],[1,4]','[1,4],[5,7]');
test('[1,3],[5,7],[1,5]','[1,7]');
test('[1,3],[5,7],[1,6]','[1,7]');
test('[1,3],[5,7],[1,7]','[1,7]');
test('[1,3],[5,7],[1,8]','[1,8]');
test('[1,3],[5,7],[2,2]','[1,3],[5,7]');
test('[1,3],[5,7],[2,3]','[1,3],[5,7]');
test('[1,3],[5,7],[2,4]','[1,4],[5,7]');
test('[1,3],[5,7],[2,5]','[1,7]');
test('[1,3],[5,7],[2,6]','[1,7]');
test('[1,3],[5,7],[2,7]','[1,7]');
test('[1,3],[5,7],[2,8]','[1,8]');
test('[1,3],[5,7],[3,3]','[1,3],[5,7]');
test('[1,3],[5,7],[3,4]','[1,4],[5,7]');
test('[1,3],[5,7],[3,5]','[1,7]');
test('[1,3],[5,7],[3,6]','[1,7]');
test('[1,3],[5,7],[3,7]','[1,7]');
test('[1,3],[5,7],[3,8]','[1,8]');
test('[1,3],[5,7],[4,4]','[1,3],[4,4],[5,7]');
test('[1,3],[5,7],[4,5]','[1,3],[4,7]');
test('[1,3],[5,7],[4,6]','[1,3],[4,7]');
test('[1,3],[5,7],[4,7]','[1,3],[4,7]');
test('[1,3],[5,7],[4,8]','[1,3],[4,8]');
test('[1,3],[5,7],[5,5]','[1,3],[5,7]');
test('[1,3],[5,7],[5,6]','[1,3],[5,7]');
test('[1,3],[5,7],[5,7]','[1,3],[5,7]');
test('[1,3],[5,7],[5,8]','[1,3],[5,8]');
test('[1,3],[5,7],[6,6]','[1,3],[5,7]');
test('[1,3],[5,7],[6,7]','[1,3],[5,7]');
test('[1,3],[5,7],[6,8]','[1,3],[5,8]');
test('[1,3],[5,7],[7,7]','[1,3],[5,7]');
test('[1,3],[5,7],[7,8]','[1,3],[5,8]');
test('[1,3],[5,7],[8,8]','[1,3],[5,7],[8,8]');
test('[1,2],[5,6],[3,4]','[1,2],[3,4],[5,6]');
test('[1,2],[5,6],[2,5]','[1,6]');
test('[1,1],[2,2],[3,3]','[1,1],[2,2],[3,3]');
test('[0,6],[7,8],[12,19],[3,8]','[0,8],[12,19]');
test('[12,14],[15,19],[7,8],[1,12]','[1,14],[15,19]');
test('[12,17],[18,18],[9,9],[5,17]','[5,17],[18,18]');
test('[2,7],[3,9],[10,12],[15,19],[18,22]','[2,9],[10,12],[15,22]');
}
sub test {
my $intervals = shift;
my $expected = shift;
my $result = merge($intervals);
printf "%s %s => %s\n",
($result eq $expected) ? '[OK]' : '[ERR]',
$intervals,
$result;
}
sub merge {
my $intervals = shift;
$intervals =~ s/\s+//; #remove unnecessary spaces
my %output;
foreach ( split /\Q],[\E/, $intervals ){
next unless (/(-?\d+),(-?\d+)/);
my ($from,$to) = ($1,$2);
$output{$from} = $to unless exists $output{$from} && $output{$from} >= $to;
}
# merge overlapping intervals
my $prev;
foreach my $k (sort {$a <=> $b} keys %output){
if (defined $prev && $output{$prev} >= $k) {
$output{$prev} = $output{$k} if $output{$k} > $output{$prev};
delete $output{$k};
}
else {
$prev = $k;
}
}
return join ',', map { qq|[$_,$output{$_}]| }
sort {$a <=> $b}
keys %output;
}
__END__
./ch-1.pl [0,6],[7,8],[12,19],[3,8]
[0,8],[12,19]
./ch-1.pl [1,3],[5,7],[-1,4]
[-1,4],[5,7]