diff options
| -rw-r--r-- | challenge-006/ozzy/perl6/ch-1.p6 | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-006/ozzy/perl6/ch-1.p6 b/challenge-006/ozzy/perl6/ch-1.p6 new file mode 100644 index 0000000000..26bf934c40 --- /dev/null +++ b/challenge-006/ozzy/perl6/ch-1.p6 @@ -0,0 +1,29 @@ +#!/usr/bin/env perl6 + +sub MAIN ( *@numbers where { $_.all ~~ Int } ) { + + if @numbers.elems == 0 { say "Usage: script <space-separated list of integers>"; exit; } + + @numbers = @numbers.sort; + my Range @output; + + # Populate @output array with commandline numbers as Range objects + for @numbers -> $n { + my $i = @output.elems; + if $i == 0 || $n > @output[$i-1].max+1 { @output[$i] = Range.new($n.Int, $n.Int); } + elsif $n == @output[$i-1].max+1 { @output[$i-1] = Range.new( @output[$i-1].min, $n.Int ); }; + } + + + # Print the ranges from the @output array + for 0..(@output.elems-1) -> $i { + + FIRST { print 'Compact range representation: '; } + + if @output[$i].elems == 1 { print(@output[$i].min); } + else { print(@output[$i].min, '-', @output[$i].max); } + + if $i < @output.elems-1 { print ', '; } + else { say '.'; } + } +} |
