diff options
| author | James Smith <js5@sanger.ac.uk> | 2022-12-26 01:06:51 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-26 01:06:51 +0000 |
| commit | dae0a10e23b470229e2440cd0683b3a90a1e4ca4 (patch) | |
| tree | 7294dd85b469514827cca189f4760967c6ba432d | |
| parent | a54797c3cd52fac11700d96f1f5daf55470b108e (diff) | |
| download | perlweeklychallenge-club-dae0a10e23b470229e2440cd0683b3a90a1e4ca4.tar.gz perlweeklychallenge-club-dae0a10e23b470229e2440cd0683b3a90a1e4ca4.tar.bz2 perlweeklychallenge-club-dae0a10e23b470229e2440cd0683b3a90a1e4ca4.zip | |
Update README.md
| -rw-r--r-- | challenge-196/james-smith/README.md | 16 |
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 +} +``` |
