aboutsummaryrefslogtreecommitdiff
path: root/challenge-131
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-09-27 02:03:07 +0100
committerGitHub <noreply@github.com>2021-09-27 02:03:07 +0100
commit251118ed929b561a045ddff35baec8a707b22eac (patch)
tree334d13d9b01685abde3efede968ae510edea5041 /challenge-131
parentd2753d8c0db52be324b4548e8a6685322ab91d68 (diff)
parentdd49254ef2d8b83715c22f254511d3146a32ee29 (diff)
downloadperlweeklychallenge-club-251118ed929b561a045ddff35baec8a707b22eac.tar.gz
perlweeklychallenge-club-251118ed929b561a045ddff35baec8a707b22eac.tar.bz2
perlweeklychallenge-club-251118ed929b561a045ddff35baec8a707b22eac.zip
Merge pull request #4932 from drbaggy/master
Added some docs
Diffstat (limited to 'challenge-131')
-rw-r--r--challenge-131/james-smith/README.md10
1 files changed, 9 insertions, 1 deletions
diff --git a/challenge-131/james-smith/README.md b/challenge-131/james-smith/README.md
index a7987880c8..5c3ce0ff49 100644
--- a/challenge-131/james-smith/README.md
+++ b/challenge-131/james-smith/README.md
@@ -18,11 +18,19 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-131/ja
## The solution
+There isn't much to the solution, we are going to return the data as an array of arrayrefs each containing consecutive numbers.
+
+ * We start by creating our first arrayref containing the first value. {for the `if` code to work without an edge case we need in element in our first arrayref to compare against)
+ * We then loop through the values:
+ * if the next number is 1 greater than the last value in the last arrayref. We push it there,
+ * otherwise we create a new arrayref and push it on the end of our array.
+ * We "cheat" a bit with the `if` statement - by replace `if( $a ) { $b } else { $c }` with `($a) ? ($b) : ($c)` this means we can use it inline within a `foreach` loop.
+
```perl
sub conseq {
my @val = @{$_[0]};
my @res = ( [shift @val] );
- ( $_ == 1 + $res[-1][-1] ) ? (push @{$res[-1]},$_) : (push @res,[$_]) foreach @val;
+ ( $_ == 1 + $res[-1][-1] ) ? (push @{$res[-1]},$_) : (push @res,[$_]) for @val;
\@res;
}
```