From dae0a10e23b470229e2440cd0683b3a90a1e4ca4 Mon Sep 17 00:00:00 2001 From: James Smith Date: Mon, 26 Dec 2022 01:06:51 +0000 Subject: Update README.md --- challenge-196/james-smith/README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/challenge-196/james-smith/README.md b/challenge-196/james-smith/README.md index 2a84b66e88..5c485666aa 100644 --- a/challenge-196/james-smith/README.md +++ b/challenge-196/james-smith/README.md @@ -75,3 +75,19 @@ sub range_v2 { grep { $_->[1]-$_->[0] } @r } ``` + +### Version 3 + +Finally one last method - where we use `for` rather than `shift`/`while` which is the fastest... + +We just for over `@_`; If `$_` is equal to the `$end+1` we extend the region, o/w we push the interval if it is not "empty" (`$s==$e`), and start a new interval. +Finally at the end we add the last interval if non-empty. + +```perl +sub range_for { + my $s = my $e = shift, my @r; + ($_==$e+1) ? $e++ : ( $s==$e || push(@r,[$s,$e]) , $e=$s=$_ ) for @_; + push @r, [$s,$e] unless $s==$e; + @r +} +``` -- cgit