aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Green <mail@simon.green>2023-03-27 23:51:44 +1100
committerSimon Green <mail@simon.green>2023-03-27 23:51:44 +1100
commit706737f15c4a18ef44e26c080508b28240917fe7 (patch)
tree6f273fd01d0ace0aec51fc84107559656c23e31d
parent8915a66de2cb2a724aee5e55ddfc15580cfdf1d5 (diff)
downloadperlweeklychallenge-club-706737f15c4a18ef44e26c080508b28240917fe7.tar.gz
perlweeklychallenge-club-706737f15c4a18ef44e26c080508b28240917fe7.tar.bz2
perlweeklychallenge-club-706737f15c4a18ef44e26c080508b28240917fe7.zip
Simon's solution to challenge 210
-rw-r--r--challenge-210/sgreen/README.md2
-rw-r--r--challenge-210/sgreen/blog.txt1
-rwxr-xr-xchallenge-210/sgreen/perl/ch-1.pl28
-rwxr-xr-xchallenge-210/sgreen/perl/ch-2.pl40
-rwxr-xr-xchallenge-210/sgreen/python/ch-1.py25
-rwxr-xr-xchallenge-210/sgreen/python/ch-2.py39
6 files changed, 134 insertions, 1 deletions
diff --git a/challenge-210/sgreen/README.md b/challenge-210/sgreen/README.md
index f393339e11..64a534294d 100644
--- a/challenge-210/sgreen/README.md
+++ b/challenge-210/sgreen/README.md
@@ -1,3 +1,3 @@
# The Weekly Challenge 209
-Blog: [Special Accounts](https://dev.to/simongreennet/special-accounts-5969)
+Blog: [Numbers Challenges](https://dev.to/simongreennet/numbers-challenges-32k1)
diff --git a/challenge-210/sgreen/blog.txt b/challenge-210/sgreen/blog.txt
new file mode 100644
index 0000000000..295399d982
--- /dev/null
+++ b/challenge-210/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/numbers-challenges-32k1 \ No newline at end of file
diff --git a/challenge-210/sgreen/perl/ch-1.pl b/challenge-210/sgreen/perl/ch-1.pl
new file mode 100755
index 0000000000..0e865f4b13
--- /dev/null
+++ b/challenge-210/sgreen/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+use List::Util qw(sum uniq);
+
+sub main(@array) {
+ my $score = 0;
+
+ # Get all unique numbers
+ foreach my $i (uniq(@array)) {
+ # Calculate the sum of all numbers one less, the same or one more
+ # than the target
+ my $this_score = sum( grep { $_ >= $i-1 and $_ <= $i+1} @array);
+
+ # Record this score if it is larger
+ if ($score < $this_score) {
+ $score = $this_score;
+ }
+ }
+
+ say $score;
+}
+
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-210/sgreen/perl/ch-2.pl b/challenge-210/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..45e1ebb835
--- /dev/null
+++ b/challenge-210/sgreen/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+use experimental 'signatures';
+
+sub main (@array) {
+ # Continue until we can't
+ C: while (1) {
+ # Go through each item in the array, get that value and the next
+ foreach my $i ( 0 .. $#array - 1 ) {
+ my $left = $array[$i];
+ my $right = $array[ $i + 1 ];
+
+ # If the left value is > 0 and the right one is < 0, we have
+ # a collision
+ if ( $left > 0 and $right < 0 ) {
+ $right = abs($right);
+ if ( $right <= $left ) {
+ # Remove the right number
+ splice( @array, $i + 1, 1 );
+ }
+ if ( $right >= $left ) {
+ # Remove the left number
+ splice( @array, $i, 1 );
+ }
+
+ # Start the loop again
+ next C;
+ }
+ }
+
+ # We've gone through the list and removed all collisions
+ last;
+ }
+
+ say '(', join( ', ', @array ), ')';
+}
+main(@ARGV); \ No newline at end of file
diff --git a/challenge-210/sgreen/python/ch-1.py b/challenge-210/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..6ec294ffce
--- /dev/null
+++ b/challenge-210/sgreen/python/ch-1.py
@@ -0,0 +1,25 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(array):
+ score = 0
+
+ # Get all unique numbers
+ for i in set(array):
+ # Calculate the sum of all numbers one less, the same or one more
+ # than the target
+ this_score = sum(x for x in array if i-1 <= x <= i+1)
+
+ # Record this score if it is larger
+ if score < this_score:
+ score = this_score
+
+ print(score)
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)
diff --git a/challenge-210/sgreen/python/ch-2.py b/challenge-210/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..791d842c48
--- /dev/null
+++ b/challenge-210/sgreen/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import sys
+
+
+def main(array):
+ # Continue until we can't
+ should_continue = True
+
+ while should_continue:
+ # Go through each item in the array, get that value and the next
+ for i in range(len(array)-1):
+ left = array[i]
+ right = array[i+1]
+
+ # If the left value is > 0 and the right one is < 0, we have
+ # a collision
+ if left > 0 and right < 0:
+ right = abs(right)
+ if right <= left:
+ # Remove the right number
+ array.pop(i+1)
+ if right >= left:
+ # Remove the left number
+ array.pop(i)
+
+ # Start the loop again
+ break
+ else:
+ # We've gone through the list and removed all collisions
+ should_continue = False
+
+ print(array)
+
+
+if __name__ == '__main__':
+ # Turn the strings into integers
+ n = [int(i) for i in sys.argv[1:]]
+ main(n)