diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-25 00:14:30 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-25 00:14:30 +0000 |
| commit | 7703035b017723333a409d5fdcb9c3a3da64bfd7 (patch) | |
| tree | c044040384477e5f42612e6d976ce3ab7e97c527 | |
| parent | 9c0f8a63d9a392ab39064ec01129e66bb0a1a810 (diff) | |
| parent | b3d032d9a41b5bcc631f60d780683245ca60a1a6 (diff) | |
| download | perlweeklychallenge-club-7703035b017723333a409d5fdcb9c3a3da64bfd7.tar.gz perlweeklychallenge-club-7703035b017723333a409d5fdcb9c3a3da64bfd7.tar.bz2 perlweeklychallenge-club-7703035b017723333a409d5fdcb9c3a3da64bfd7.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
| -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 | ||||
| -rwxr-xr-x | challenge-248/steve-g-lynn/perl/ch-1.pl | 18 | ||||
| -rwxr-xr-x | challenge-248/steve-g-lynn/perl/ch-1a.pl | 77 |
8 files changed, 226 insertions, 13 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])) diff --git a/challenge-248/steve-g-lynn/perl/ch-1.pl b/challenge-248/steve-g-lynn/perl/ch-1.pl index 16652eeb5a..6ab90e77a1 100755 --- a/challenge-248/steve-g-lynn/perl/ch-1.pl +++ b/challenge-248/steve-g-lynn/perl/ch-1.pl @@ -49,22 +49,16 @@ pp_def( PDL_Indx i,c_mid, n_size=$SIZE(n); if (0==(n_size % 2)){ c_mid=n_size/2; - for (i=0; i < c_mid; i++) { - $b(n=>i)=i; - } - for (i=c_mid; i < n_size; i++) { - $b(n=>i)=n_size-i-1; - } } else { c_mid=(n_size+1)/2; - for (i=0; i < c_mid; i++) { - $b(n=>i)=i; - } - for (i=c_mid; i < n_size; i++) { - $b(n=>i)=n_size-i-1; - } } + for (i=0; i < c_mid; i++) { + $b(n=>i)=i; + } + for (i=c_mid; i < n_size; i++) { + $b(n=>i)=n_size-i-1; + } }, ); diff --git a/challenge-248/steve-g-lynn/perl/ch-1a.pl b/challenge-248/steve-g-lynn/perl/ch-1a.pl new file mode 100755 index 0000000000..7c7d8d69db --- /dev/null +++ b/challenge-248/steve-g-lynn/perl/ch-1a.pl @@ -0,0 +1,77 @@ +#!/usr/bin/env -S perl -wl + +#-- faster version that does everything in Inline Pdlpp + +use PDL; +use Inline Pdlpp; + +local *shortest_distance_a=sub { + # faster version using PP for outer loop + my ($str,$chr)=@_; + ($str =~ /$chr/) || return 0; + + (1==length($chr)) || (die "Single character input needed:$!\n"); + + #-- get indices where the element equals chr + my $grp=pdl(grep {$chr eq substr($str,$_,1)} 0..length($str)); + my $retval=$grp->shortest_distance_pp(length($str)); + + return $retval; + }; + +print &shortest_distance_a('loveleetcode','e'); +#[3 2 1 0 1 0 0 1 2 2 1 0] + +print &shortest_distance_a('aaab','b'); +#[3 2 1 0] + +__DATA__ + +__Pdlpp__ + + +pp_def( + 'shortest_distance_pp', + Pars => 'indx a(m); indx lenstr(); [o]b(n);', + RedoDimsCode => '$SIZE(n)=$lenstr();', + GenericTypes => ['N'], + Code => q{ + PDL_Indx i,j,size_m, size_n, ctr_stop; + size_m=$SIZE(m); + size_n=$SIZE(n); + ctr_stop=$a(m=>0); + if (0 < $a(m=>0)){ + for (i=0; i < ctr_stop; i++) { + $b(n=>i)=ctr_stop-i; + } + } + PDL_Indx a1,a2,c_mid,gap_size; + for (j=0; j<size_m; j++) { + a1=$a(m=>j); + a2=$a(m=>j+1); + gap_size=a2-a1+1; + + if (0==(gap_size % 2)){ + c_mid=gap_size/2; + } + else { + c_mid=(gap_size+1)/2; + } + + for (i=a1; i < a1+c_mid; i++) { + $b(n=>i)=i-a1; + } + for (i=a1+c_mid; i <= a2; i++) { + $b(n=>i)=a2-i; + } + + } + if (size_n > $a(m=>-1)+1){ + a1=$a(m=>size_m-1); + for (i=a1; i < size_n; i++) { + $b(n=>i)=i-a1; + } + } + }, +); + |
