aboutsummaryrefslogtreecommitdiff
path: root/challenge-196
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-196')
-rw-r--r--challenge-196/james-smith/README.md16
1 files changed, 16 insertions, 0 deletions
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
+}
+```