aboutsummaryrefslogtreecommitdiff
path: root/challenge-006
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2019-05-04 17:49:37 +0100
committerGitHub <noreply@github.com>2019-05-04 17:49:37 +0100
commit9241d06f28ea1e4300d8d79f37e4e6255cdc2aa8 (patch)
tree9961a9494ebc0a94eab951967072ffcb176b6a5c /challenge-006
parentf042cae50ee9684c2403c56d6ad2aafcd9778e3f (diff)
parent963cccfb5d37157d7337b24148395347f38fb0dd (diff)
downloadperlweeklychallenge-club-9241d06f28ea1e4300d8d79f37e4e6255cdc2aa8.tar.gz
perlweeklychallenge-club-9241d06f28ea1e4300d8d79f37e4e6255cdc2aa8.tar.bz2
perlweeklychallenge-club-9241d06f28ea1e4300d8d79f37e4e6255cdc2aa8.zip
Merge pull request #118 from perl6/master
Challenge #006 - ch-1.p6 solution
Diffstat (limited to 'challenge-006')
-rw-r--r--challenge-006/ozzy/perl6/ch-1.p629
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 '.'; }
+ }
+}