diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-19 12:02:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-19 12:02:16 +0000 |
| commit | 57ebec37a5a581ebf0779695516f86b2bb0d0535 (patch) | |
| tree | 6005f101a81b74884836025614e2d853c16de9de | |
| parent | f777d42e551c48bf2f4deb75abf03d347dfdf893 (diff) | |
| parent | f1b38696e0add85b6c40803067fc26589ef3762e (diff) | |
| download | perlweeklychallenge-club-57ebec37a5a581ebf0779695516f86b2bb0d0535.tar.gz perlweeklychallenge-club-57ebec37a5a581ebf0779695516f86b2bb0d0535.tar.bz2 perlweeklychallenge-club-57ebec37a5a581ebf0779695516f86b2bb0d0535.zip | |
Merge pull request #7589 from simongreen-net/master
Simon's solution to challenge 203 and 204
| -rw-r--r-- | challenge-203/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-203/sgreen/perl/ch-1.pl | 26 | ||||
| -rw-r--r-- | challenge-203/sgreen/python/ch-1.py | 21 | ||||
| -rw-r--r-- | challenge-204/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-204/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-204/sgreen/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-204/sgreen/perl/ch-2.pl | 27 | ||||
| -rwxr-xr-x | challenge-204/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-204/sgreen/python/ch-2.py | 26 |
9 files changed, 158 insertions, 5 deletions
diff --git a/challenge-203/sgreen/README.md b/challenge-203/sgreen/README.md index 642af7b377..fb74669ff4 100644 --- a/challenge-203/sgreen/README.md +++ b/challenge-203/sgreen/README.md @@ -1,3 +1 @@ -# The Weekly Challenge 202 - -Blog: [Weekly Challenge 202](https://dev.to/simongreennet/weekly-challenge-202-4dcm) +# The Weekly Challenge 203 diff --git a/challenge-203/sgreen/perl/ch-1.pl b/challenge-203/sgreen/perl/ch-1.pl new file mode 100644 index 0000000000..232eeecfb2 --- /dev/null +++ b/challenge-203/sgreen/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use Algorithm::Combinatorics 'combinations'; + +sub main (@n) { + my $solutions = 0; + + # Work through all combinations of positions + my $iter = combinations( [ 0 .. $#n ], 4 ); + while ( my $x = $iter->next ) { + my ( $i, $j, $k, $l ) = sort { $a <=> $b } @$x; + if ( $n[$i] + $n[$j] + $n[$k] == $n[$l] ) { + $solutions++; + return; + } + } + + say $solutions; +} + +main(@ARGV); diff --git a/challenge-203/sgreen/python/ch-1.py b/challenge-203/sgreen/python/ch-1.py new file mode 100644 index 0000000000..cc1bc522e9 --- /dev/null +++ b/challenge-203/sgreen/python/ch-1.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python3 + +from itertools import combinations +import sys + +def main(n): + solutions = 0 + + # Work through all combinations of positions + for x in combinations(range(len(n)), 4): + i, j, k, l = sorted(x) + if n[i] + n[j] + n[k] == n[l]: + solutions += 1 + + # No solution is found + print(solutions) + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-204/sgreen/README.md b/challenge-204/sgreen/README.md index 642af7b377..ec08b855cc 100644 --- a/challenge-204/sgreen/README.md +++ b/challenge-204/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 202 +# The Weekly Challenge 204 -Blog: [Weekly Challenge 202](https://dev.to/simongreennet/weekly-challenge-202-4dcm) +Blog: [Weekly Challenge 204](https://dev.to/simongreennet/weekly-challenge-204-256e) diff --git a/challenge-204/sgreen/blog.txt b/challenge-204/sgreen/blog.txt new file mode 100644 index 0000000000..56db88d2e8 --- /dev/null +++ b/challenge-204/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/weekly-challenge-204-256e
\ No newline at end of file diff --git a/challenge-204/sgreen/perl/ch-1.pl b/challenge-204/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..e6657c3390 --- /dev/null +++ b/challenge-204/sgreen/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@n) { + + # If the last value is less than the first, reverse it + if ( $n[0] > $n[-1] ) { + @n = reverse @n; + } + + # Go through the positions 0 .. len(n)-2 + foreach my $i ( 0 ... $#n - 1 ) { + if ( $n[$i] > $n[ $i + 1 ] ) { + # If the earlier value is higher, we don't have a solution + say '0'; + return; + } + } + + # We have a solution + say '1'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-204/sgreen/perl/ch-2.pl b/challenge-204/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..641d8f89de --- /dev/null +++ b/challenge-204/sgreen/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@arg) { + # Extract anything that looks like an integer + my @n = ( join( ' ', @arg ) =~ /(-?[0-9]+)/g ); + + # Remove the last two numbers + my ($r, $c) = splice(@n, -2); + + # Check there there is a viable solution + if ( scalar(@n) != $c * $r ) { + say 0; + return; + } + + # Print the result + for ( my $i = 0 ; $i <= $#n ; $i += $c ) { + say '[ ', join( ' ', @n[ $i .. $i + $c - 1 ] ), ' ]'; + } +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-204/sgreen/python/ch-1.py b/challenge-204/sgreen/python/ch-1.py new file mode 100755 index 0000000000..8d81507f84 --- /dev/null +++ b/challenge-204/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + + # If the last value is less than the first, reverse it + if n[0] > n[-1]: + n = n[::-1] + + # Go through the positions 0 .. len(n)-2 + for i in range(len(n)-1): + if n[i] > n[i+1]: + # If the earlier value is higher, we don't have a solution + print(0) + return + + # We have a solution + print('1') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-204/sgreen/python/ch-2.py b/challenge-204/sgreen/python/ch-2.py new file mode 100755 index 0000000000..0ecfbd91eb --- /dev/null +++ b/challenge-204/sgreen/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import re +import sys + + +def main(n): + # Remove the last two numbers + c = int(n.pop()) + r = int(n.pop()) + + # Check there there is a viable solution + if len(n) != c * r: + print('0') + return + + # Print the result + for i in range(0, len(n), c): + print('[ ' + ' '.join(n[i:i+c]) + ' ]') + + +if __name__ == '__main__': + # Extract all the integers + inputs = ' '.join(sys.argv[1:]) + nums = re.findall(r'-?\d+', inputs) + main(nums) |
