diff options
| author | Simon Green <mail@simon.green> | 2023-12-24 23:13:25 +1100 |
|---|---|---|
| committer | Simon Green <mail@simon.green> | 2023-12-24 23:13:25 +1100 |
| commit | e6097e4da93a71bbadb7bfaa6fac66cb5ed1a876 (patch) | |
| tree | 9b4aff375d3459e98efc5fde85e19ecb147297f5 | |
| parent | 60c8fe6ec139ec65766bc30e41b50c2582ddf244 (diff) | |
| download | perlweeklychallenge-club-e6097e4da93a71bbadb7bfaa6fac66cb5ed1a876.tar.gz perlweeklychallenge-club-e6097e4da93a71bbadb7bfaa6fac66cb5ed1a876.tar.bz2 perlweeklychallenge-club-e6097e4da93a71bbadb7bfaa6fac66cb5ed1a876.zip | |
Simon's solution to challenge 248
| -rw-r--r-- | challenge-248/sgreen/README.md | 2 | ||||
| -rw-r--r-- | challenge-248/sgreen/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-248/sgreen/perl/ch-1.pl | 40 | ||||
| -rwxr-xr-x | challenge-248/sgreen/perl/ch-2.pl | 37 | ||||
| -rwxr-xr-x | challenge-248/sgreen/python/ch-1.py | 33 | ||||
| -rwxr-xr-x | challenge-248/sgreen/python/ch-2.py | 31 |
6 files changed, 143 insertions, 1 deletions
diff --git a/challenge-248/sgreen/README.md b/challenge-248/sgreen/README.md index 191a192e6e..296c098161 100644 --- a/challenge-248/sgreen/README.md +++ b/challenge-248/sgreen/README.md @@ -1,3 +1,3 @@ # The Weekly Challenge 247 -Blog: [The one about frequency](https://dev.to/simongreennet/the-one-about-frequency-la6) +Blog: [A string, a character and a matrix...](https://dev.to/simongreennet/a-string-a-character-and-a-matrix-5bc9) diff --git a/challenge-248/sgreen/blog.txt b/challenge-248/sgreen/blog.txt new file mode 100644 index 0000000000..1a8b90ac22 --- /dev/null +++ b/challenge-248/sgreen/blog.txt @@ -0,0 +1 @@ +https://dev.to/simongreennet/a-string-a-character-and-a-matrix-5bc9
\ No newline at end of file diff --git a/challenge-248/sgreen/perl/ch-1.pl b/challenge-248/sgreen/perl/ch-1.pl new file mode 100755 index 0000000000..701b20f191 --- /dev/null +++ b/challenge-248/sgreen/perl/ch-1.pl @@ -0,0 +1,40 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +sub main ( $word, $char ) { + # Obtain the positions of the character in the string + my @positions = (); + foreach my $i ( 0 .. length($word) - 1 ) { + push @positions, $i if substr( $word, $i, 1 ) eq $char; + } + + if ( $#positions == -1 ) { + # Throw an error if we don't find the character in the string + die "'$char' does not appear in string\n"; + } + + # Record the position of the first occurrence of the character, and the next + my $current_pos = shift(@positions); + my $next_pos = shift(@positions) // $current_pos; + + my @solution = (); + foreach my $i ( 0 .. length($word) - 1 ) { + if ( abs( $i - $next_pos ) < abs( $i - $current_pos ) ) { + # We are closer to the next occurrence of the character + $current_pos = $next_pos; + $next_pos = shift(@positions) // $current_pos; + } + + # Record the closest occurrence for this position + push @solution, abs( $i - $current_pos ); + } + + # Print the solution + say '(', join( ',', @solution ), ')'; +} + +main(@ARGV);
\ No newline at end of file diff --git a/challenge-248/sgreen/perl/ch-2.pl b/challenge-248/sgreen/perl/ch-2.pl new file mode 100755 index 0000000000..38e26033a7 --- /dev/null +++ b/challenge-248/sgreen/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use experimental 'signatures'; + +use JSON 'decode_json'; + +sub main ($matrix) { + # Calculate the size of the matrix + my $cols = scalar( @{ $matrix->[0] } ); + + # Check that all columns are of equal length + foreach my $r ( 1 .. $#$matrix ) { + if ( scalar( @{ $matrix->[$r] } ) != $cols ) { + die "Row $r has different number of columns\n"; + } + } + + say '['; + foreach my $r ( 0 .. $#$matrix - 1) { + # Compute each row + my @row = (); + foreach my $c ( 0 .. $cols - 2 ) { + push @row, + $matrix->[$r][$c] + + $matrix->[$r][ $c + 1 ] + + $matrix->[ $r + 1 ][$c] + + $matrix->[ $r + 1 ][ $c + 1 ]; + } + say ' [', join( ', ', @row ), '],'; + } + say ']'; +} + +main( decode_json( $ARGV[0] ) );
\ No newline at end of file diff --git a/challenge-248/sgreen/python/ch-1.py b/challenge-248/sgreen/python/ch-1.py new file mode 100755 index 0000000000..2a236bacf4 --- /dev/null +++ b/challenge-248/sgreen/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +import sys + + +def main(word, char): + # Obtain the positions of the character in the string + positions = [i for i, s in enumerate(word) if s == char] + + if len(positions) == 0: + # Throw an error if we don't find the character in the string + raise ValueError(f"'{char}' does not appear in string") + + # Record the position of the first occurrence of the character, and the next + current_pos = positions.pop(0) + next_pos = positions.pop(0) if len(positions) > 0 else current_pos + + solution = [] + for i in range(len(word)): + if abs(i - next_pos) < abs(i-current_pos): + # We are closer to the next occurrence of the character + current_pos = next_pos + next_pos = positions.pop(0) if len(positions) > 0 else current_pos + + # Record the closest occurrence for this position + solution.append(abs(i-current_pos)) + + # Print the solution + print('(' + ','.join(map(str, solution)) + ')') + + +if __name__ == '__main__': + main(*sys.argv[1:]) diff --git a/challenge-248/sgreen/python/ch-2.py b/challenge-248/sgreen/python/ch-2.py new file mode 100755 index 0000000000..47eba6db2d --- /dev/null +++ b/challenge-248/sgreen/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def main(matrix): + # Calculate the size of the matrix + rows = len(matrix) + cols = len(matrix[0]) + + # Check that all columns are of equal length + for r in range(1, rows): + if len(matrix[r]) != cols: + raise ValueError(f'Row {r} has different number of columns') + + print('[') + for r in range(rows-1): + # Compute each row + row = [] + for c in range(cols-1): + row.append(matrix[r][c] + matrix[r][c+1] + + matrix[r+1][c] + matrix[r+1][c+1]) + print(' ' + str(row) + ',') + + print(']') + + +if __name__ == '__main__': + # Read the input as JSON + main(json.loads(sys.argv[1])) |
