diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-02 23:57:08 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-02 23:57:08 +0100 |
| commit | f71237e9c7d2bfd11848989cbc5e8d9a904b342d (patch) | |
| tree | 656737f1052fb2695b071ed73562c424610867a3 | |
| parent | 2ffc219fb5eefa9b15aafa1bdc7e923f14ba5c79 (diff) | |
| parent | 8e4d803855ac88d07c17d846c996306f90e15e7c (diff) | |
| download | perlweeklychallenge-club-f71237e9c7d2bfd11848989cbc5e8d9a904b342d.tar.gz perlweeklychallenge-club-f71237e9c7d2bfd11848989cbc5e8d9a904b342d.tar.bz2 perlweeklychallenge-club-f71237e9c7d2bfd11848989cbc5e8d9a904b342d.zip | |
Merge pull request #7816 from simongreen-net/swg-210
Simon's solution to challenge 210
| -rw-r--r-- | challenge-210/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-210/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-210/sgreen/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-210/sgreen/perl/ch-2.pl | 40 | ||||
| -rwxr-xr-x | challenge-210/sgreen/python/ch-1.py | 25 | ||||
| -rwxr-xr-x | challenge-210/sgreen/python/ch-2.py | 39 |
6 files changed, 135 insertions, 2 deletions
diff --git a/challenge-210/sgreen/README.md b/challenge-210/sgreen/README.md index f393339e11..0f4559dc5a 100644 --- a/challenge-210/sgreen/README.md +++ b/challenge-210/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 209 +# The Weekly Challenge 210 -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) |
