From 963cccfb5d37157d7337b24148395347f38fb0dd Mon Sep 17 00:00:00 2001 From: Alexander <39702500+threadless-screw@users.noreply.github.com> Date: Sat, 4 May 2019 11:41:48 +0000 Subject: ch-1.p6 --- challenge-006/ozzy/perl6/ch-1.p6 | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 challenge-006/ozzy/perl6/ch-1.p6 (limited to 'challenge-006') 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 "; 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 '.'; } + } +} -- cgit