diff options
| author | James Smith <js5@sanger.ac.uk> | 2022-12-19 12:56:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-19 12:56:09 +0000 |
| commit | aded57d669f83dfa227a8c646b3129ff2a084e2d (patch) | |
| tree | 4120deae30774729ecb9765c45d51b127f1803db | |
| parent | f115a156a59806f35b0e0066618b1d7b4082d58c (diff) | |
| download | perlweeklychallenge-club-aded57d669f83dfa227a8c646b3129ff2a084e2d.tar.gz perlweeklychallenge-club-aded57d669f83dfa227a8c646b3129ff2a084e2d.tar.bz2 perlweeklychallenge-club-aded57d669f83dfa227a8c646b3129ff2a084e2d.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 84936d76d5..2a84b66e88 100644 --- a/challenge-196/james-smith/README.md +++ b/challenge-196/james-smith/README.md @@ -59,3 +59,19 @@ sub range { @r } ``` + +### version 2 + +That version is a bit messi - far too many variables! + +This time we keep track of the intervals inside the result `@r`, we note that we don't need to discard the "empty" intervals while making the array - we can use grep to filter them out as we return the list. This makes the logic easier... + +We start with an "empty" interval `[ $_[0],$_[0] ]`, and then we loop through the array if there is a gap we create a new "empty" interval and push to the list - o/w we just extend the last interval in the list... As we only want the "non-empty" intervals we just `grep` this at the end. + +```perl +sub range_v2 { + my @r = [ (shift) x 2 ]; + $_ == $r[-1][1] + 1 ? $r[-1][1] = $_ : push @r, [$_,$_] for @_; + grep { $_->[1]-$_->[0] } @r +} +``` |
