aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Smith <js5@sanger.ac.uk>2023-04-02 22:45:29 +0100
committerGitHub <noreply@github.com>2023-04-02 22:45:29 +0100
commitc7949ad5e1c94291ac7f5534da8f234169b4da18 (patch)
treea7a381335d02c445e108cb936eba91e0254f5cd2
parent236529b48e11f0d4bc8f912db5d21482eafa7a78 (diff)
downloadperlweeklychallenge-club-c7949ad5e1c94291ac7f5534da8f234169b4da18.tar.gz
perlweeklychallenge-club-c7949ad5e1c94291ac7f5534da8f234169b4da18.tar.bz2
perlweeklychallenge-club-c7949ad5e1c94291ac7f5534da8f234169b4da18.zip
Update README.md
-rw-r--r--challenge-210/james-smith/README.md15
1 files changed, 15 insertions, 0 deletions
diff --git a/challenge-210/james-smith/README.md b/challenge-210/james-smith/README.md
index 2888dd9d66..0985d3892c 100644
--- a/challenge-210/james-smith/README.md
+++ b/challenge-210/james-smith/README.md
@@ -43,6 +43,21 @@ sub kill_and_win {
}
```
+### A neater solution....
+
+```perl
+sub kill_and_win {
+ my($m,%t,$x)=0;
+ $t{$_} += $_ for @_; ## Get freq in hash
+ ( ( $x = ( $t{$_-1} // 0 ) ## Compute value
+ + ( $t{$_ } // 0 ) ## for current
+ + ( $t{$_+1} // 0 ) ## integer
+ ) > $m ) && ($m = $x) for keys %t; ## if max reset max
+ $m ## return value
+}
+```
+
+About the same efficiency as above - we can simplify the sum part of the operation by computing the sum of each number before the 2nd loop. So instead of incrementing by 1 to get the count we just add the value each time..
# Task 2: Number Collision