blob: 6d3c0f07154062c4e02838349a7a2652788964e9 (
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/env perl6
use v6;
class GrowableRange {
has Int $.min;
has Int $.max;
submethod BUILD( Int :$!min, Int :$!max ) {}
method next() { $!max + 1 }
method grow() { $!max++; return self }
method gist() { $!min == $!max ??
$!min.Str !! $!max == $!min+1 ??
"{$!min},{$!max}"
!! "{$!min}-{$!max}" }
method Str() { self.gist }
}
sub USAGE { say $*USAGE }
#| Display Help file
multi sub MAIN ( Bool :h($help) where *.so ) { USAGE(); }
#| Get the shortend list of a CSV string
multi sub MAIN (
Str $number-string where * ~~ /^ \d+ [ ',' \d+ ]* $/ #= Comma seperated list of numbers
) {
my Int @in = [$_.Int for $number-string.split(",")].sort( * <=> * );
say process-list( @in );
}
#| Get the shorted list of a space seperated list of number
multi sub MAIN (
*@numbers where { all($_) ~~ IntStr } #= List of integers
) {
my Int @in = [$_.Int for @numbers].sort( * <=> * );
say process-list( @in );
}
sub process-list( Int @numbers ) {
my $current;
my @out;
for @numbers -> $number {
if @out.elems == 0 || @out[*-1].next != $number {
@out.push( GrowableRange.new( min => $number, max => $number ) );
} else {
@out[*-1].grow;
}
}
return @out.join(",");
}
|