diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-23 00:45:20 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-23 00:45:20 +0000 |
| commit | 67fa1e652496ee376f1593d54ab5062807df1289 (patch) | |
| tree | 2ed4023273f1692d84401aae2ecd2b53d725e917 | |
| parent | 9a0fafbe20aeecccb9bc31b47bc27130ad19a88d (diff) | |
| parent | 46257dafafc44b320a06c7bfeec23a20928a9154 (diff) | |
| download | perlweeklychallenge-club-67fa1e652496ee376f1593d54ab5062807df1289.tar.gz perlweeklychallenge-club-67fa1e652496ee376f1593d54ab5062807df1289.tar.bz2 perlweeklychallenge-club-67fa1e652496ee376f1593d54ab5062807df1289.zip | |
Merge pull request #7442 from simongreen-net/master
Simon's solution to challenge 200
| -rw-r--r-- | challenge-200/sgreen/README.md | 4 | ||||
| -rw-r--r-- | challenge-200/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-200/sgreen/perl/ch-1.pl | 34 | ||||
| -rwxr-xr-x | challenge-200/sgreen/perl/ch-2.pl | 57 | ||||
| -rwxr-xr-x | challenge-200/sgreen/python/ch-1.py | 29 | ||||
| -rwxr-xr-x | challenge-200/sgreen/python/ch-2.py | 47 |
6 files changed, 170 insertions, 2 deletions
diff --git a/challenge-200/sgreen/README.md b/challenge-200/sgreen/README.md index 4708657f42..bd88165103 100644 --- a/challenge-200/sgreen/README.md +++ b/challenge-200/sgreen/README.md @@ -1,3 +1,3 @@ -# The Weekly Challenge 199 +# The Weekly Challenge 200 -Blog: [It's all good](https://dev.to/simongreennet/its-all-good-foe) +Blog: [Two hundred slices](https://dev.to/simongreennet/two-hundred-slices-ach)
\ No newline at end of file diff --git a/challenge-200/sgreen/blog.txt b/challenge-200/sgreen/blog.txt new file mode 100644 index 0000000000..7c1b607cd1 --- /dev/null +++ b/challenge-200/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/two-hundred-slices-ach
\ No newline at end of file diff --git a/challenge-200/sgreen/perl/ch-1.pl b/challenge-200/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..262b50ecfe --- /dev/null +++ b/challenge-200/sgreen/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main (@n) { + my @solutions = (); + + foreach my $start ( 0 .. $#n - 2 ) { + # Calculate the difference between the first two values + my $diff = abs( $n[$start] - $n[ $start + 1 ] ); + + foreach my $end ( $start + 2 .. $#n ) { + if ( abs( $n[$end] - $n[ $end - 1 ] ) == $diff ) { + # We have a solution + push @solutions, '(' . join( ', ', @n[ $start .. $end ] ) . ')'; + } + else { + last; + } + } + } + + if (@solutions) { + say join ', ', @solutions; + } + else { + say '()'; + } +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-200/sgreen/perl/ch-2.pl b/challenge-200/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..72805bc2b6 --- /dev/null +++ b/challenge-200/sgreen/perl/ch-2.pl @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub _in ( $s, $t ) { + return index( $t, $s ) != -1; +} + +sub print_row ( $line, $numbers ) { + my @row = (); + + foreach my $n (@$numbers) { + if ( ref($line) ) { + # We have a left side and right side + my $left = _in( $line->[0], $n ) ? '|' : ' '; + my $right = _in( $line->[1], $n ) ? '|' : ' '; + push @row, "$left $right"; + } + else { + # We want to show a solid line + if ( _in( $line, $n ) ) { + push @row, '-------'; + } + else { + push @row, ' '; + } + } + } + + say join ' ', @row; +} + +sub main ($n) { + # Turn the numbers into a list of strings to show + my @truth = qw'abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg'; + my @numbers = map { $truth[$_] } split //, $n; + + # Define the lines we want to show + my @lines = ( + 'a', + [ 'f', 'b' ], + [ 'f', 'b' ], + 'g', + [ 'e', 'c' ], + [ 'e', 'c' ], + 'd' + ); + + foreach my $line (@lines) { + print_row( $line, \@numbers ); + } +} + +main( $ARGV[0] )
\ No newline at end of file diff --git a/challenge-200/sgreen/python/ch-1.py b/challenge-200/sgreen/python/ch-1.py new file mode 100755 index 0000000000..0d65c06fad --- /dev/null +++ b/challenge-200/sgreen/python/ch-1.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys + + +def main(n): + solutions = [] + + for start in range(len(n)-2): + # Calculate the difference between the first two values + diff = abs(n[start] - n[start+1]) + + for end in range(start+2, len(n)): + if abs(n[end] - n[end-1]) == diff: + # We have a solution + solutions.append(tuple(n[start:end+1])) + else: + break + + if solutions: + print(*solutions, sep=', ') + else: + print('()') + + +if __name__ == '__main__': + # Turn the strings into integers + n = [int(i) for i in sys.argv[1:]] + main(n) diff --git a/challenge-200/sgreen/python/ch-2.py b/challenge-200/sgreen/python/ch-2.py new file mode 100755 index 0000000000..593c4589ae --- /dev/null +++ b/challenge-200/sgreen/python/ch-2.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import sys + + +def print_row(line, numbers): + row = [] + + for n in numbers: + if type(line) == str: + # We want to show a solid line + if line in n: + row.append('-------') + else: + row.append(' ') + else: + # We have a left side and right side + left = '|' if line[0] in n else ' ' + right = '|' if line[1] in n else ' ' + row.append(f'{left} {right}') + + print(*row, sep=' ') + + +def main(n): + # Turn the numbers into a list of strings to show + truth = 'abcdef bc abdeg abcdg bcfg acdfg acdefg abc abcdefg abcfg'.split( + ' ') + numbers = [truth[int(i)] for i in n] + + # Define the lines we want to show + lines = [ + 'a', + ['f', 'b'], + ['f', 'b'], + 'g', + ['e', 'c'], + ['e', 'c'], + 'd' + ] + + for line in lines: + print_row(line, numbers) + + +if __name__ == '__main__': + main(sys.argv[1]) |
