From 77997b6e85b2142b057108c0c2fd01dfb8aac90e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 10:29:32 +0100 Subject: Task 1 done --- challenge-204/luca-ferrari/raku/ch-1.p6 | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 challenge-204/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-204/luca-ferrari/raku/ch-1.p6 b/challenge-204/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..cbcc3efbb6 --- /dev/null +++ b/challenge-204/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,25 @@ +#!raku + +# +# Perl Weekly Challenge 204 +# Task 1 +# +# See +# + +sub MAIN( *@list where { @list.grep( * ~~ Int ).elems == @list.elems } ) { + my $monotonic-type; + for 0 ^..^ @list.elems { + if ( ! $monotonic-type ) { + $monotonic-type = ( @list[ $_ ] > @list[ $_ - 1 ] ) ?? True !! False; + } + + # elements are the same + next if @list[ $_ ] == @list[ $_ - 1 ]; + + '0'.say and exit if ( ! $monotonic-type && @list[ $_ ] > @list[ $_ - 1 ] ); + '0'.say and exit if ( $monotonic-type && @list[ $_ ] < @list[ $_ - 1 ] ); + } + + '1'.say; +} -- cgit From 52cbb7bad49711e2900a2dbc2b2c014f7dc30276 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 10:45:02 +0100 Subject: Task 2 done --- challenge-204/luca-ferrari/raku/ch-2.p6 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 challenge-204/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-204/luca-ferrari/raku/ch-2.p6 b/challenge-204/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..652ef3e1ec --- /dev/null +++ b/challenge-204/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,32 @@ +#!raku + +# +# Perl Weekly Challenge 204 +# Task 2 +# +# See +# + +# raku raku/ch-2.p6 -r=3 -c=2 "1 2 3" "4 5 6" +# [[1 2] [3 4] [5 6]] + +sub MAIN( Int :$r, Int :$c, *@matrix ) { + my @M = @matrix.map: { $_.split( ' ' ) }; + + # if cannot reshape, exit + '0'.say and exit if ( $r * $c ) < ( @M.elems * @M[ 0 ].elems ); + + my @N; + my @new-row; + for @M -> $row { + + for 0 ..^ $row.elems { + @new-row.push: $row[ $_ ] if ( @new-row.elems < $c ); + @N.push: [ @new-row ] if ( @new-row.elems == $c ); + @new-row = () if ( @new-row.elems == $c ); + + } + } +v + @N.join( "\n" ).say; +} -- cgit From ec0c81db1376cdf10a67797c1cfc5b55b0d22fa0 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 10:51:42 +0100 Subject: Task 1 PLPerl --- challenge-204/luca-ferrari/postgresql/ch-1.plperl | 30 +++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 challenge-204/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-204/luca-ferrari/postgresql/ch-1.plperl b/challenge-204/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..7fbb93bf4c --- /dev/null +++ b/challenge-204/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,30 @@ +-- +-- Perl Weekly Challenge 204 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc204; + +CREATE OR REPLACE FUNCTION +pwc204.task1_plperl( int[] ) +RETURNS int +AS $CODE$ + my ( $list ) = @_; + my $monotonic_type; + + for ( 1 .. scalar( $list->@* ) - 1 ) { + next if ( $list->[ $_ ] == $list->[ $_ - 1 ] ); + + if ( ! defined( $monotonic_type ) ) { + $monotonic_type = ( $list->[ $_ ] > $list->[ $_ - 1 ] ) ? 1 : 0; + } + + return 0 if ( $monotonic_type && $list->[ $_ ] < $list->[ $_ - 1 ] ); + return 0 if ( ! $monotonic_type && $list->[ $_ ] > $list->[ $_ - 1 ] ); + } + + return 1; + +$CODE$ +LANGUAGE plperl; -- cgit From 5972b734c116d76306bf1047883ff9ef99b3ef90 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 10:57:58 +0100 Subject: Task 2 plperl --- challenge-204/luca-ferrari/postgresql/ch-2.plperl | 39 +++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 challenge-204/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-204/luca-ferrari/postgresql/ch-2.plperl b/challenge-204/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..fe64ecbca6 --- /dev/null +++ b/challenge-204/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,39 @@ +-- +-- Perl Weekly Challenge 204 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc204; + +/* +testdb=> select pwc204.task2_plperl( 3, 2, array[ array[ 1,2,3]::int[], array[3,4,5]::int[] ]::int[] ); + task2_plperl +--------------------- + {{1,2},{3,3},{4,5}} +(1 row) + +*/ +CREATE OR REPLACE FUNCTION +pwc204.task2_plperl( int, int, int[][] ) +RETURNS int[][] +AS $CODE$ + my ( $r, $c, $matrix ) = @_; + my @N; + my @new_row; + + return undef if ( ( $r * $c ) < $matrix->@* * $matrix->[0]->@* ); + + for my $row ( 0 .. scalar( $matrix->@* ) - 1 ) { + for my $col ( 0 .. scalar( $matrix->[ $row ]->@* ) - 1 ) { + push @new_row, $matrix->[ $row ]->[ $col ] if ( @new_row < $c ); + if ( @new_row == $c ) { + push @N, [ @new_row ]; + @new_row = (); + } + } + } + + return [ @N ]; +$CODE$ +LANGUAGE plperl; -- cgit From 30409b39a194d7dde19e2fa46f2f9cd3094928a9 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 11:02:35 +0100 Subject: Task1 PL/PgSQL done --- challenge-204/luca-ferrari/postgresql/ch-1.sql | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 challenge-204/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-204/luca-ferrari/postgresql/ch-1.sql b/challenge-204/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..98b6698450 --- /dev/null +++ b/challenge-204/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,43 @@ +-- +-- Perl Weekly Challenge 204 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc204; + +CREATE OR REPLACE FUNCTION +pwc204.task1_plpgsql( l int[] ) +RETURNS int +AS $CODE$ +DECLARE + monotonic_mode bool; + i int; +BEGIN + + FOR i IN 2 .. array_length( l, 1 ) LOOP + IF l[ i ] = l[ i - 1 ] THEN + CONTINUE; + END IF; + + IF monotonic_mode IS NULL THEN + IF l[ i ] > l[ i - 1 ] THEN + monotonic_mode := true; + ELSE + monotonic_mode := false; + END IF; + END IF; + + IF monotonic_mode AND l[ i ] < l[ i - 1 ] THEN + RETURN 0; + END IF; + IF NOT monotonic_mode AND l[ i ] > l[ i - 1 ] THEN + RETURN 0; + END IF; + END LOOP; + + RETURN 1; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From d984472fb4dd7176806d3545ba710c765e2c134c Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 11:25:38 +0100 Subject: Task 2 plpgsql --- challenge-204/luca-ferrari/postgresql/ch-2.sql | 37 ++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-204/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-204/luca-ferrari/postgresql/ch-2.sql b/challenge-204/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..ec40b84823 --- /dev/null +++ b/challenge-204/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 204 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc204; + +CREATE OR REPLACE FUNCTION +pwc204.task2_plpgsql( r int, c int, a int[][] ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE + current_row int[]; + index_r int; + index_c int; +BEGIN + IF ( r * c ) < ( array_length( a, 1 ) * array_length( a, 2 ) ) THEN + RETURN; + END IF; + + FOR index_r IN 1 .. array_length( a, 1 ) LOOP + FOR index_c IN 1 .. array_length( a, 2 ) LOOP + current_row := current_row || a[ index_r ][ index_c ]; + IF array_length( current_row, 1 ) = c THEN + RETURN NEXT current_row; + current_row := array[]::int[]; + + END IF; + END LOOP; + END LOOP; + + RETURN; +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 4560012437d1aa5816303b29b5fe3d9f5f936969 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 13 Feb 2023 11:29:06 +0100 Subject: Challenge 204 Task1 LK Perl Python --- challenge-204/lubos-kolouch/perl/ch-1.pl | 38 ++++++++++++++++++++++++++++++ challenge-204/lubos-kolouch/python/ch-1.py | 32 +++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 challenge-204/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-204/lubos-kolouch/python/ch-1.py diff --git a/challenge-204/lubos-kolouch/perl/ch-1.pl b/challenge-204/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..6a8f0f9b61 --- /dev/null +++ b/challenge-204/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# function to check if an array is monotonic +sub is_monotonic { + my @nums = @_; + my $increasing = 1; + my $decreasing = 1; + + for (my $i = 0; $i < $#nums; $i++) { + if ($nums[$i] > $nums[$i + 1]) { + $increasing = 0; + } + if ($nums[$i] < $nums[$i + 1]) { + $decreasing = 0; + } + } + + return $increasing || $decreasing; +} + +# test cases +my @test_cases = ( + [1, 2, 3, 4, 5], + [5, 4, 3, 2, 1], + [1, 1, 1, 1, 1], + [1, 2, 3, 2, 1], + [5, 3, 2, 4, 3], +); + +foreach my $test_case (@test_cases) { + my $result = is_monotonic(@$test_case); + my $output = $result ? 1 : 0; + print "Array @$test_case is monotonic: $output\n"; +} + diff --git a/challenge-204/lubos-kolouch/python/ch-1.py b/challenge-204/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..e1d6f9a9a4 --- /dev/null +++ b/challenge-204/lubos-kolouch/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from typing import List + + +def is_monotonic(nums: List[int]) -> bool: + increasing = True + decreasing = True + + for i in range(len(nums) - 1): + if nums[i] > nums[i + 1]: + increasing = False + if nums[i] < nums[i + 1]: + decreasing = False + + return increasing or decreasing + + +# Test cases +test_cases = [ + [1, 2, 3, 4, 5], + [5, 4, 3, 2, 1], + [1, 1, 2, 2, 3], + [1, 2, 3, 2, 1], + [5, 3, 2, 4, 3], +] + +for test_case in test_cases: + result = is_monotonic(test_case) + output = 1 if result else 0 + print(f"Array {test_case} is monotonic: {output}") -- cgit From ce71c71ec00f5355aa481e5caee3f1e779b357cf Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Feb 2023 10:34:00 +0000 Subject: Challenge 204 Solutions (Raku) --- challenge-204/mark-anderson/raku/ch-1.raku | 13 +++++++++++++ challenge-204/mark-anderson/raku/ch-2.raku | 13 +++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 challenge-204/mark-anderson/raku/ch-1.raku create mode 100644 challenge-204/mark-anderson/raku/ch-2.raku diff --git a/challenge-204/mark-anderson/raku/ch-1.raku b/challenge-204/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..e7c4a2f3db --- /dev/null +++ b/challenge-204/mark-anderson/raku/ch-1.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; + +ok monotonic-array(1,2,2,3); +nok monotonic-array(1,3,2); +ok monotonic-array(6,5,5,4); + +sub monotonic-array(*@a) +{ + return 1 if [<=] @a; + return 1 if [>=] @a; + return 0 +} diff --git a/challenge-204/mark-anderson/raku/ch-2.raku b/challenge-204/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..da3853abe5 --- /dev/null +++ b/challenge-204/mark-anderson/raku/ch-2.raku @@ -0,0 +1,13 @@ +#!/usr/bin/env raku +use Test; + +is-deeply reshape-matrix(1, 4, [[1,2], [3,4]]), [[1,2,3,4],]; +is-deeply reshape-matrix(3, 2, [[1,2,3], [4,5,6]]), [[1,2],[3,4],[5,6]]; +nok reshape-matrix(3, 2, [[1,2]]); + +sub reshape-matrix($r, $c, @m) +{ + @m .= map(*.Slip); + return 0 if $r * $c !== @m; + @m.rotor($c).map(*.Array).Array +} -- cgit From 954b2ff314f57c9b76d07464589359ffec42f5fb Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Mon, 13 Feb 2023 11:36:49 +0100 Subject: Challenge 204 Task2 LK Perl Python --- challenge-204/lubos-kolouch/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++ challenge-204/lubos-kolouch/python/ch-2.py | 28 ++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 challenge-204/lubos-kolouch/perl/ch-2.pl create mode 100644 challenge-204/lubos-kolouch/python/ch-2.py diff --git a/challenge-204/lubos-kolouch/perl/ch-2.pl b/challenge-204/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..da5d85d681 --- /dev/null +++ b/challenge-204/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,38 @@ +use strict; +use warnings; + +sub reshape { + my ($matrix, $r, $c) = @_; + my $m = scalar @$matrix; + my $n = scalar @{$matrix->[0]}; + + if ($m * $n != $r * $c) { + return 0; + } + + my @result; + for my $i (0 .. $r-1) { + for my $j (0 .. $c-1) { + my $idx = $j + $i * $c; + my $x = int($idx / $n); + my $y = $idx % $n; + $result[$i][$j] = $matrix->[$x][$y]; + } + } + return \@result; +} + +my $matrix = [[1, 2], [3, 4]]; +my $r = 1; +my $c = 4; + +my $result = reshape($matrix, $r, $c); +if ($result) { + print "Reshaped matrix:\n"; + for my $row (@$result) { + print join(" ", @$row), "\n"; + } +} else { + print "Cannot reshape matrix\n"; +} + diff --git a/challenge-204/lubos-kolouch/python/ch-2.py b/challenge-204/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..4a49b7a544 --- /dev/null +++ b/challenge-204/lubos-kolouch/python/ch-2.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import numpy as np +from typing import List, Union + + +def reshape(matrix: List[List[int]], r: int, + c: int) -> Union[int, List[List[int]]]: + m, n = np.shape(matrix) + if m * n != r * c: + return 0 + flat_matrix = np.array(matrix).flatten() + reshaped_matrix = np.reshape(flat_matrix, (r, c)) + return reshaped_matrix.tolist() + + +matrix = [[1, 2], [3, 4]] +r = 1 +c = 4 +result = reshape(matrix, r, c) + +if result == 0: + print("Cannot reshape matrix") +else: + print("Reshaped matrix:") + for row in result: + print(row) -- cgit From c16ee9b7cd3d443c54afe59b4665f6644c379567 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 11:59:50 +0100 Subject: Remove typo --- challenge-204/luca-ferrari/raku/ch-2.p6 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-204/luca-ferrari/raku/ch-2.p6 b/challenge-204/luca-ferrari/raku/ch-2.p6 index 652ef3e1ec..e819c86fbb 100644 --- a/challenge-204/luca-ferrari/raku/ch-2.p6 +++ b/challenge-204/luca-ferrari/raku/ch-2.p6 @@ -27,6 +27,6 @@ sub MAIN( Int :$r, Int :$c, *@matrix ) { } } -v + @N.join( "\n" ).say; } -- cgit From 3138a686ce1d33a1b2147a893daed10e3377727e Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 13 Feb 2023 12:05:50 +0100 Subject: Blog references --- challenge-204/luca-ferrari/blog-1.txt | 1 + challenge-204/luca-ferrari/blog-2.txt | 1 + challenge-204/luca-ferrari/blog-3.txt | 1 + challenge-204/luca-ferrari/blog-4.txt | 1 + challenge-204/luca-ferrari/blog-5.txt | 1 + challenge-204/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-204/luca-ferrari/blog-1.txt create mode 100644 challenge-204/luca-ferrari/blog-2.txt create mode 100644 challenge-204/luca-ferrari/blog-3.txt create mode 100644 challenge-204/luca-ferrari/blog-4.txt create mode 100644 challenge-204/luca-ferrari/blog-5.txt create mode 100644 challenge-204/luca-ferrari/blog-6.txt diff --git a/challenge-204/luca-ferrari/blog-1.txt b/challenge-204/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..751ee80999 --- /dev/null +++ b/challenge-204/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/13/PerlWeeklyChallenge204.html#task1 diff --git a/challenge-204/luca-ferrari/blog-2.txt b/challenge-204/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..369385b202 --- /dev/null +++ b/challenge-204/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/13/PerlWeeklyChallenge204.html#task2 diff --git a/challenge-204/luca-ferrari/blog-3.txt b/challenge-204/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..68a83114ef --- /dev/null +++ b/challenge-204/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/13/PerlWeeklyChallenge204.html#task1plperl diff --git a/challenge-204/luca-ferrari/blog-4.txt b/challenge-204/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..8289b48396 --- /dev/null +++ b/challenge-204/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/13/PerlWeeklyChallenge204.html#task2plperl diff --git a/challenge-204/luca-ferrari/blog-5.txt b/challenge-204/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..a285e2a983 --- /dev/null +++ b/challenge-204/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/13/PerlWeeklyChallenge204.html#task1plpgsql diff --git a/challenge-204/luca-ferrari/blog-6.txt b/challenge-204/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..a2e78b5d36 --- /dev/null +++ b/challenge-204/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/02/13/PerlWeeklyChallenge204.html#task2plpgsql -- cgit From c3657efc72e3b8e4ac50cafbe6b6a391ebf96bed Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 13 Feb 2023 11:44:34 +0000 Subject: Challenge 204 Solutions (Raku) --- challenge-204/mark-anderson/raku/ch-2.raku | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/challenge-204/mark-anderson/raku/ch-2.raku b/challenge-204/mark-anderson/raku/ch-2.raku index da3853abe5..2591183a00 100644 --- a/challenge-204/mark-anderson/raku/ch-2.raku +++ b/challenge-204/mark-anderson/raku/ch-2.raku @@ -7,7 +7,6 @@ nok reshape-matrix(3, 2, [[1,2]]); sub reshape-matrix($r, $c, @m) { - @m .= map(*.Slip); - return 0 if $r * $c !== @m; - @m.rotor($c).map(*.Array).Array + return 0 unless $r * $c == @m * @m.head; + @m.map(*.Slip).rotor($c).map(*.Array).Array } -- cgit From f51b5c00f93bdde4240d55d8e237e3694f079e65 Mon Sep 17 00:00:00 2001 From: Andrew Shitov Date: Mon, 13 Feb 2023 15:25:26 +0100 Subject: Solutions Week 204 by ash and ChatGPT --- challenge-204/ash/blog-1.txt | 1 + challenge-204/ash/blog-2.txt | 1 + challenge-204/ash/raku/ch-1.raku | 23 +++++++++++++++ challenge-204/ash/raku/ch-2.raku | 48 +++++++++++++++++++++++++++++++ challenge-204/chatgpt/README | 7 +++++ challenge-204/chatgpt/blog-1.txt | 1 + challenge-204/chatgpt/blog-2.txt | 1 + challenge-204/chatgpt/raku/ch-1.raku | 33 +++++++++++++++++++++ challenge-204/chatgpt/raku/ch-2.raku | 56 ++++++++++++++++++++++++++++++++++++ 9 files changed, 171 insertions(+) create mode 100644 challenge-204/ash/blog-1.txt create mode 100644 challenge-204/ash/blog-2.txt create mode 100644 challenge-204/ash/raku/ch-1.raku create mode 100644 challenge-204/ash/raku/ch-2.raku create mode 100644 challenge-204/chatgpt/README create mode 100644 challenge-204/chatgpt/blog-1.txt create mode 100644 challenge-204/chatgpt/blog-2.txt create mode 100644 challenge-204/chatgpt/raku/ch-1.raku create mode 100644 challenge-204/chatgpt/raku/ch-2.raku diff --git a/challenge-204/ash/blog-1.txt b/challenge-204/ash/blog-1.txt new file mode 100644 index 0000000000..59e520d49a --- /dev/null +++ b/challenge-204/ash/blog-1.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/ diff --git a/challenge-204/ash/blog-2.txt b/challenge-204/ash/blog-2.txt new file mode 100644 index 0000000000..8fabcdca67 --- /dev/null +++ b/challenge-204/ash/blog-2.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/ diff --git a/challenge-204/ash/raku/ch-1.raku b/challenge-204/ash/raku/ch-1.raku new file mode 100644 index 0000000000..ad2c0b4410 --- /dev/null +++ b/challenge-204/ash/raku/ch-1.raku @@ -0,0 +1,23 @@ +# A Raku solution to the Task 1 "Monotonic Array" of the Weekly Challenge 204 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK1 +# Solved together with ChatGPT, the dialogue published at +# https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/ + +# Test run: +# $ raku ch-1.raku +# 1 +# 0 +# 1 + +sub is-monotonic(@nums) { + [>=] @nums or [<=] @nums +} + +my @nums = (1, 2, 2, 3); +say +is-monotonic(@nums); + +@nums = (1, 3, 2); +say +is-monotonic(@nums); + +@nums = (6, 5, 5, 4); +say +is-monotonic(@nums); diff --git a/challenge-204/ash/raku/ch-2.raku b/challenge-204/ash/raku/ch-2.raku new file mode 100644 index 0000000000..e6a2b13241 --- /dev/null +++ b/challenge-204/ash/raku/ch-2.raku @@ -0,0 +1,48 @@ +# A Raku solution to the Task 2 "Reshape Matrix" of the Weekly Challenge 204 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK2 +# Solved together with ChatGPT. The whole dialogue with the machine is published in my blog: +# https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/ + +# Test run: +# $ raku ch-2.raku +# [1 2 3 4] +# [[1 2] [3 4] [5 6]] +# [[1] [2]] +# 0 + +sub reshape-matrix (@matrix, $r, $c) { + my @flat = @matrix.map(*.flat).flat; + my $total_elements = @flat.elems; + return 0 if $total_elements != $r * $c; + + my @result; + for ^$r -> $i { + push @result, [@flat[$c * $i .. $c * $i + $c - 1]]; + } + + return @result.elems == 1 ?? @result[0] !! @result; +} + +my @matrix = [ [ 1, 2 ], [ 3, 4 ] ]; +my $r = 1; +my $c = 4; +my $result = reshape-matrix(@matrix, $r, $c); +say $result; + +@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +$r = 3; +$c = 2; +$result = reshape-matrix(@matrix, $r, $c); +say $result; + +@matrix = [ [ 1 ], [ 2 ] ]; +$r = 2; +$c = 1; +$result = reshape-matrix(@matrix, $r, $c); +say $result; + +@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +$r = 2; +$c = 4; +$result = reshape-matrix(@matrix, $r, $c); +say $result; diff --git a/challenge-204/chatgpt/README b/challenge-204/chatgpt/README new file mode 100644 index 0000000000..7735e06a24 --- /dev/null +++ b/challenge-204/chatgpt/README @@ -0,0 +1,7 @@ +Solutions generated by ChatGPT. + +The solutions are based on the description and the examples that the Weekly Challenge offers. It was sent to the chat as they are published on the website without any modifications. + +The solution located in this directory are the first versions that ChatGPT generated. The code is taken as soon as it compiles and gives correct results. Optimised versions with a more natural Raku syntax are placed at ../ash. All those were also done in a dialogue with the machine with some hints from a human. + +Submitted by Andrew Shitov (ash). diff --git a/challenge-204/chatgpt/blog-1.txt b/challenge-204/chatgpt/blog-1.txt new file mode 100644 index 0000000000..59e520d49a --- /dev/null +++ b/challenge-204/chatgpt/blog-1.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/ diff --git a/challenge-204/chatgpt/blog-2.txt b/challenge-204/chatgpt/blog-2.txt new file mode 100644 index 0000000000..8fabcdca67 --- /dev/null +++ b/challenge-204/chatgpt/blog-2.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/ diff --git a/challenge-204/chatgpt/raku/ch-1.raku b/challenge-204/chatgpt/raku/ch-1.raku new file mode 100644 index 0000000000..cbc18bdd63 --- /dev/null +++ b/challenge-204/chatgpt/raku/ch-1.raku @@ -0,0 +1,33 @@ +# This program has been created by ChatGPT +# +# Test run: +# $ raku ch-1.raku +# 1 +# 0 +# 1 + +sub is_monotonic(@nums) { + my $increasing = True; + my $decreasing = True; + for (0 ..^ @nums.end) -> $i { + for ($i + 1 .. @nums.end) -> $j { + if (@nums[$i] > @nums[$j]) { + $increasing = False; + } + if (@nums[$i] < @nums[$j]) { + $decreasing = False; + } + } + } + return 1 if $increasing || $decreasing; + return 0; + } + +my @nums = (1, 2, 2, 3); +say is_monotonic(@nums); + +@nums = (1, 3, 2); +say is_monotonic(@nums); + +@nums = (6, 5, 5, 4); +say is_monotonic(@nums); diff --git a/challenge-204/chatgpt/raku/ch-2.raku b/challenge-204/chatgpt/raku/ch-2.raku new file mode 100644 index 0000000000..20375d1164 --- /dev/null +++ b/challenge-204/chatgpt/raku/ch-2.raku @@ -0,0 +1,56 @@ +# This program has been created by ChatGPT +# +# Test run: +# $ raku ch-2.raku +# [1 2 3 4] +# [[1 2] [3 4] [5 6]] +# [[1] [2]] +# +# Notice that if you change $r and $c to 3 and 2 in the last example, +# the program prints 0 as expected. + +sub reshape-matrix (@matrix, $r, $c) { + my $total_elements = 0; + for @matrix -> @row { + $total_elements += @row.elems; + } + if $total_elements != $r * $c { + return 0; + } + + my @result = []; + my $element_index = 0; + for (0 .. $r - 1) -> $i { + my @new_row = []; + for (0 .. $c - 1) -> $j { + @new_row.push(@matrix[$element_index div @matrix[0].elems][$element_index % @matrix[0].elems]); + $element_index++; + } + @result.push(@new_row); + } + + if @result.elems == 1 { + return @result[0]; + } + else { + return @result; + } +} + +my @matrix = [ [ 1, 2 ], [ 3, 4 ] ]; +my $r = 1; +my $c = 4; +my $result = reshape-matrix(@matrix, $r, $c); +say $result; + +@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +$r = 3; +$c = 2; +$result = reshape-matrix(@matrix, $r, $c); +say $result; + +@matrix = [ [ 1 ], [ 2 ] ]; +$r = 2; +$c = 1; +$result = reshape-matrix(@matrix, $r, $c); +say $result; -- cgit From f129cbf6732d8d404459006777cf213024a3fe21 Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Mon, 13 Feb 2023 11:11:16 -0600 Subject: Solve PWC204 --- challenge-204/wlmb/blog.txt | 2 ++ challenge-204/wlmb/perl/ch-1.pl | 25 +++++++++++++++++++++++++ challenge-204/wlmb/perl/ch-2.pl | 22 ++++++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 challenge-204/wlmb/blog.txt create mode 100755 challenge-204/wlmb/perl/ch-1.pl create mode 100755 challenge-204/wlmb/perl/ch-2.pl diff --git a/challenge-204/wlmb/blog.txt b/challenge-204/wlmb/blog.txt new file mode 100644 index 0000000000..da9fde3295 --- /dev/null +++ b/challenge-204/wlmb/blog.txt @@ -0,0 +1,2 @@ +https://wlmb.github.io/2023/02/13/PWC204/ + diff --git a/challenge-204/wlmb/perl/ch-1.pl b/challenge-204/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..66cc937e71 --- /dev/null +++ b/challenge-204/wlmb/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# Perl weekly challenge 204 +# Task 1: Monotonic Array +# +# See https://wlmb.github.io/2023/02/13/PWC204/#task-1-monotonic-array +use v5.36; +die <<~"FIN" unless @ARGV; + Usage: $0 N1 [N2...] + to test if the sequence N1 N2... is monotonic + FIN +my @orig=@ARGV; +my $current=shift; +my ($increasing, $decreasing); +for(@ARGV){ + $_>$current and $increasing=1; + $_<$current and $decreasing=1; + last if $increasing and $decreasing; # shortcut if non monotonic + $current=$_; +} +my ($result, $reason)= + $increasing && $decreasing?(0, "Non-monotonic"): + $increasing ?(1, "Non-decreasing"): + $decreasing ?(1, "Non-increasing"): + (1, "Constant"); +say join " ", @orig, "->", $result, $reason; diff --git a/challenge-204/wlmb/perl/ch-2.pl b/challenge-204/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..af92096064 --- /dev/null +++ b/challenge-204/wlmb/perl/ch-2.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 204 +# Task 2: Reshape Matrix +# +# See https://wlmb.github.io/2023/02/13/PWC204/#task-2-reshape-matrix +use v5.36; +use PDL; +die <<~"FIN" unless @ARGV==3; + Usage: $0 M r c + to convert matrix M (a string using PDL's notation) + to a matrix with r rows, c columns + FIN +my $M=pdl shift; +my ($rows, $cols)=@ARGV; +my $nelem=$M->nelem; # total number of elements +my $desired=$rows*$cols; +say("$M cannot be reshaped to $rows rows and $cols columns: 0"), exit + unless $desired==$nelem; +my $N=$M->copy; +$N->reshape($cols) if $rows==1; # 1D row vector +$N->reshape($cols, $rows) if $rows!=1; #2D matrix +say "$M as $rows x $cols matrix becomes $N" -- cgit From 69ea84efc8bf5c206240a823fa10950337d54499 Mon Sep 17 00:00:00 2001 From: Thomas Köhler Date: Mon, 13 Feb 2023 20:34:59 +0100 Subject: Add solution 204 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Köhler --- challenge-204/jeanluc2020/blog-1.txt | 1 + challenge-204/jeanluc2020/blog-2.txt | 1 + challenge-204/jeanluc2020/perl/ch-1.pl | 86 +++++++++++++++++++++ challenge-204/jeanluc2020/perl/ch-2.pl | 136 +++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+) create mode 100644 challenge-204/jeanluc2020/blog-1.txt create mode 100644 challenge-204/jeanluc2020/blog-2.txt create mode 100755 challenge-204/jeanluc2020/perl/ch-1.pl create mode 100755 challenge-204/jeanluc2020/perl/ch-2.pl diff --git a/challenge-204/jeanluc2020/blog-1.txt b/challenge-204/jeanluc2020/blog-1.txt new file mode 100644 index 0000000000..ecda755aca --- /dev/null +++ b/challenge-204/jeanluc2020/blog-1.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-204-1.html diff --git a/challenge-204/jeanluc2020/blog-2.txt b/challenge-204/jeanluc2020/blog-2.txt new file mode 100644 index 0000000000..b687d7ff97 --- /dev/null +++ b/challenge-204/jeanluc2020/blog-2.txt @@ -0,0 +1 @@ +http://gott-gehabt.de/800_wer_wir_sind/thomas/Homepage/Computer/perl/theweeklychallenge-204-2.html diff --git a/challenge-204/jeanluc2020/perl/ch-1.pl b/challenge-204/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..c4db878e83 --- /dev/null +++ b/challenge-204/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,86 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK1 +# Task 1: Monotonic Array +# +# You are given an array of integers. +# +# Write a script to find out if the given array is Monotonic. Print 1 if it is otherwise 0. +# +## An array is Monotonic if it is either monotone increasing or decreasing. +# +## Monotone increasing: for i <= j , nums[i] <= nums[j] +## Monotone decreasing: for i <= j , nums[i] >= nums[j] +# +# +## Example 1 +## +## Input: @nums = (1,2,2,3) +## Output: 1 +# +## Example 2 +## +## Input: @nums (1,3,2) +## Output: 0 +# +## Example 3 +## +## Input: @nums = (6,5,5,4) +## Output: 1 +# +############################################################ +## +## discussion +## +############################################################ +# +# While walking the array we have to check in each step if +# we're still monotone. In order to achieve this we need to +# know if we're monotone increasing or monotone decreasing +# so far (or still constant, in which case both are still +# possible). If we were increasing and switch to decreasing +# or vice versa the whole array is not monotone and we can +# return 0 right away. If we reach the end of the array +# without any such switch we are monotone and can return 1. + +use strict; +use warnings; + +my @examples = ( + [1, 2, 2, 3], + [1, 3, 2], + [6, 5, 5, 4] +); + +foreach my $nums (@examples) { + print "Input: (" . join(", ", @$nums) . ")\n"; + print "Output: " . is_monotone(@$nums) . "\n"; +} + +# given an array, return 1 if it is monotone and 0 otherwise +sub is_monotone { + # put the first element of the array into $last, the rest into @nums + my ($last, @nums) = @_; + my $direction = 0; # so far we're neither increasing nor decreasing + foreach my $elem (@nums) { + if($direction > 0) { # we're monotone increasing so far + if($elem < $last) { + # we're no longer monotone + return 0; + } + } elsif ($direction < 0) { # we're monotone decreasing so far + if($elem > $last) { + # we're no longer monotone + return 0; + } + } else { # still constant, we can still be increasing or decreasing + if($elem > $last) { + $direction = 1; # now we know we're increasing + } elsif ($elem < $last) { + $direction = -1; # now we know we're decreasing + } + } + # make sure we have $last set to the previous element in the next step + $last = $elem; + } + return 1; +} diff --git a/challenge-204/jeanluc2020/perl/ch-2.pl b/challenge-204/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..a4f679bc6f --- /dev/null +++ b/challenge-204/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,136 @@ +#!/usr/bin/perl +# https://theweeklychallenge.org/blog/perl-weekly-challenge-204/#TASK2 +# +# Task 2: Reshape Matrix +# +# You are given a matrix (m x n) and two integers (r) and (c). +# +# Write a script to reshape the given matrix in form (r x c) with the original +# value in the given matrix. If you can’t reshape print 0. +# +## Example 1 +## +## Input: [ 1 2 ] +## [ 3 4 ] +## +## $matrix = [ [ 1, 2 ], [ 3, 4 ] ] +## $r = 1 +## $c = 4 +## +## Output: [ 1 2 3 4 ] +# +## Example 2 +## +## Input: [ 1 2 3 ] +## [ 4 5 6 ] +## +## $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ] +## $r = 3 +## $c = 2 +## +## Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +## +## [ 1 2 ] +## [ 3 4 ] +## [ 5 6 ] +# +## Example 3 +## +## Input: [ 1 2 ] +## +## $matrix = [ [ 1, 2 ] ] +## $r = 3 +## $c = 2 +## +## Output: 0 +# +############################################################ +## +## discussion +## +############################################################ +# +# So first we check our input to find m and n from our (m x n) matrix. +# If then m*n != $r * $c we can return 0 right away. +# Otherwise, flatten the matrix and select $r arrays of $c elements +# as the new matrix. +# +use strict; +use warnings; + +my @examples = ( + [ [ [ 1, 2 ], [ 3, 4 ] ], 1, 4 ], + [ [ [ 1, 2, 3 ], [ 4, 5, 6] ], 3, 2], + [ [ [ 1, 2 ] ], 3, 2] +); +foreach my $example (@examples) { + my ($matrix, $r, $c) = @$example; + print_reshaped_matrix($matrix, $r, $c); +} + +# given the matrix, $r and $c let's reshape the matrix +sub print_reshaped_matrix { + my ($matrix, $r, $c) = @_; + # print the input matrix first + print "Input:\n"; + print_matrix($matrix); + # fetch the individual arrays from the matrix and + # calculate m and n + my @arrays = @$matrix; + my $n = scalar(@arrays); + my $m = scalar(@{$arrays[0]}); + # sanity check to see whether this is actually a matrix + foreach my $array (@arrays) { + die "Not a matrix" if scalar(@$array) != $m; + } + # m*n != $r*$c => we can't create the output matrix, exit immediately + if ($r*$c != $m*$n) { + print "Output: 0\n"; + return; + } + # flatten the matrix + my @elements = flatten($matrix); + my @result = (); + # calculate the arrays for the target matrix + foreach my $index (0..$r-1) { + # calculate the current slice. It starts at $index * $c, + # while the end is one less than the beginning of the next + # slice (or the end of data) + my $start = $index * $c; + my $end = ($index+1) * $c - 1; + # get the slice and push it onto the result matrix + my @tmp = @elements[$start..$end]; + push @result, [ @tmp ]; + } + # print the output + print "Output:\n"; + print_matrix(\@result); +} + +# flatten a matrix into an array +sub flatten { + my $matrix = shift; + my @elements = (); + foreach my $array (@$matrix) { + push @elements, @$array; + } + return @elements; +} + +# print a given matrix +sub print_matrix { + my $matrix = shift; + my $first = 1; + print "[ " unless scalar(@$matrix) == 1; + foreach my $array (@$matrix) { + if($first) { + $first = 0; + } else { + print " , "; + } + print "[ " . join(", ", @$array) . " ]"; + } + print " ]" unless scalar(@$matrix) == 1; + print "\n"; +} + -- cgit From af7c882a4b596eb5cfad61f6195d76d1faca4cce Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 13 Feb 2023 16:16:07 -0500 Subject: new file: challenge-204/mattneleigh/perl/ch-1.pl new file: challenge-204/mattneleigh/perl/ch-2.pl --- challenge-204/mattneleigh/perl/ch-1.pl | 88 ++++++++++++++++++ challenge-204/mattneleigh/perl/ch-2.pl | 160 +++++++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+) create mode 100755 challenge-204/mattneleigh/perl/ch-1.pl create mode 100755 challenge-204/mattneleigh/perl/ch-2.pl diff --git a/challenge-204/mattneleigh/perl/ch-1.pl b/challenge-204/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..058e9a7c6e --- /dev/null +++ b/challenge-204/mattneleigh/perl/ch-1.pl @@ -0,0 +1,88 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @arrays = ( + # Given cases + [ 1, 2, 2, 3 ], + [ 1, 3, 2 ], + [ 6, 5, 5, 4 ], + + # Additional test cases + [ 2, 2, 2, 4 ], + [ 2, 2, 2, 0 ], + [ 4, 2, 2, 2 ], + [ 0, 2, 2, 2 ] +); + +print("\n"); +foreach my $array (@arrays){ + printf( + "Input: \@nums = (%s)\nOutput: %d\n\n", + join(", ", @{$array}), + array_is_monotonic(@{$array}) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine whether an array of numbers is monotonic- that is to say: +# for every i <= j, array[i] <= array[j] (monotone increasing) +# - OR - +# for every i <= j, array[i] >= array[j] (monotone decreasing) +# Takes one argument: +# * The list to examine +# Returns: +# * 1 if the array is monotonic, as defined above +# * 0 if the array is not monotonic +# NOTE: this function does not differentiate between the two kinds of +# monotonicity +################################################################################ +sub array_is_monotonic{ + + my $i = 0; + my $ascending = 0; + + # Loop over the array, examining each pair + # of values- pairs of equal values don't + # preclude monotonicity of either type so + # they're effectively ignored + for my $i (1 .. $#ARG){ + if($ARG[$i - 1] < $ARG[$i]){ + # An ascending pair + if($ascending < 0){ + # $ascending was less than zero- + # we'd seen descending values before + return(0); + } + $ascending = 1; + } + if($ARG[$i - 1] > $ARG[$i]){ + # A descending pair + if($ascending > 0){ + # $ascending was greater than zero- + # we'd seen ascending values before + return(0); + } + $ascending = -1; + } + } + + return(1); + +} + + + diff --git a/challenge-204/mattneleigh/perl/ch-2.pl b/challenge-204/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..acc203b64a --- /dev/null +++ b/challenge-204/mattneleigh/perl/ch-2.pl @@ -0,0 +1,160 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @matrix_data = ( + { + matrix => [ + [ 1, 2 ], + [ 3, 4 ] + ], + r => 1, + c => 4 + }, + { + matrix => [ + [ 1, 2, 3 ], + [ 4, 5, 6 ] + ], + r => 3, + c => 2 + }, + { + matrix => [ + [ 1, 2 ] + ], + r => 3, + c => 2 + } +); + +print("\n"); +foreach my $matrix_instance (@matrix_data){ + my $rearranged_matrix = reshape_matrix( + $matrix_instance->{matrix}, + $matrix_instance->{r}, + $matrix_instance->{c} + ); + + printf( + "\$matrix = %s\n\$r = %d\n\$c = %d\nOutput: %s\n\n", + matrix_to_string($matrix_instance->{matrix}), + $matrix_instance->{r}, + $matrix_instance->{c}, + defined($rearranged_matrix) ? + matrix_to_string($rearranged_matrix) + : + "0" + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Reshape a matrix into a new rectangular matrix (i.e. all rows have the same +# number of columns) of a specified set of dimensions +# Takes three arguments: +# * A reference to a matrix, which must consist of an array reference to a two +# dimensional array, e.g. +# +# [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +# +# This must be the case even for a matrix with a single row, e.g. +# +# [ [ 1, 2, 3, 4 ] ] +# +# * The number of rows the reshaped matrix is to have +# * The number of columns the reshaped matrix is to have +# Returns on success: +# * A ref to a reshaped matrix into which data from the original has been +# copied, now arranged in the specified number of rows and columns; the +# internal arrangement of this matrix will meet the requirements specified +# above +# Returns on error: +# * undef if the quantity of elements in the original matrix does not permit +# rearrangement into a rectangular matrix of the specified dimensions +# NOTE: The original matrix will NOT be altered +################################################################################ +sub reshape_matrix{ + my $matrix = shift(); + my $r = shift(); + my $c = shift(); + + my @elements; + + # Copy the contents of the matrix into a + # flat list + foreach my $row (@{$matrix}){ + push(@elements, @{$row}); + } + + # Now that we know how many elements there + # are, make sure we can produce a rectangular + # matrix of the specified dimensions + return(undef) + unless((scalar(@elements) / $r) == $c); + + # Re-use $matrix to build the output matrix, + # transferring the contents from the flat + # list row by row + $matrix = []; + for(1 .. $r){ + push(@{$matrix}, [ splice(@elements, 0, $c) ]); + } + + return($matrix); + +} + + + +################################################################################ +# Produce a string representation of a matrix. +# Takes one argument: +# * A reference to a matrix, which must consist of an array reference to a two +# dimensional array, e.g. +# +# [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] +# +# This must be the case even for a matrix with a single row, e.g. +# +# [ [ 1, 2, 3, 4 ] ] +# +# Returns: +# * A string representation of the specified matrix, which matches the format +# of the examples presented above +################################################################################ +sub matrix_to_string{ + my $matrix = shift(); + + return( + sprintf( + "[ %s ]", + join( + ", ", + map( + sprintf( + "[ %s ]", + join(", ", @{$_}) + ), + @{$matrix} + ) + ) + ) + ); + +} + + + -- cgit From 5531150995d6f1c7d77e2e79283d970656ba32f1 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 13 Feb 2023 16:21:11 -0500 Subject: modified: challenge-204/mattneleigh/perl/ch-1.pl --- challenge-204/mattneleigh/perl/ch-1.pl | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/challenge-204/mattneleigh/perl/ch-1.pl b/challenge-204/mattneleigh/perl/ch-1.pl index 058e9a7c6e..cd2b965382 100755 --- a/challenge-204/mattneleigh/perl/ch-1.pl +++ b/challenge-204/mattneleigh/perl/ch-1.pl @@ -53,7 +53,7 @@ exit(0); sub array_is_monotonic{ my $i = 0; - my $ascending = 0; + my $direction = 0; # Loop over the array, examining each pair # of values- pairs of equal values don't @@ -62,21 +62,21 @@ sub array_is_monotonic{ for my $i (1 .. $#ARG){ if($ARG[$i - 1] < $ARG[$i]){ # An ascending pair - if($ascending < 0){ - # $ascending was less than zero- + if($direction < 0){ + # $direction was less than zero- # we'd seen descending values before return(0); } - $ascending = 1; + $direction = 1; } if($ARG[$i - 1] > $ARG[$i]){ # A descending pair - if($ascending > 0){ - # $ascending was greater than zero- + if($direction > 0){ + # $direction was greater than zero- # we'd seen ascending values before return(0); } - $ascending = -1; + $direction = -1; } } -- cgit From 1c81242da0755205fbeb9c0270fe9455b800d356 Mon Sep 17 00:00:00 2001 From: Matthew Neleigh Date: Mon, 13 Feb 2023 16:27:49 -0500 Subject: modified: challenge-204/mattneleigh/perl/ch-2.pl --- challenge-204/mattneleigh/perl/ch-2.pl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/challenge-204/mattneleigh/perl/ch-2.pl b/challenge-204/mattneleigh/perl/ch-2.pl index acc203b64a..161113b656 100755 --- a/challenge-204/mattneleigh/perl/ch-2.pl +++ b/challenge-204/mattneleigh/perl/ch-2.pl @@ -36,7 +36,7 @@ my @matrix_data = ( print("\n"); foreach my $matrix_instance (@matrix_data){ - my $rearranged_matrix = reshape_matrix( + my $reshaped_matrix = reshape_matrix( $matrix_instance->{matrix}, $matrix_instance->{r}, $matrix_instance->{c} @@ -47,8 +47,8 @@ foreach my $matrix_instance (@matrix_data){ matrix_to_string($matrix_instance->{matrix}), $matrix_instance->{r}, $matrix_instance->{c}, - defined($rearranged_matrix) ? - matrix_to_string($rearranged_matrix) + defined($reshaped_matrix) ? + matrix_to_string($reshaped_matrix) : "0" ); -- cgit From 86db5ac7471eaf554cb7ccf3986585e6e45803b4 Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 13 Feb 2023 22:34:20 +0100 Subject: Add solution to 204: Monotonic Array & Reshape Matrix by E. Choroba --- challenge-204/e-choroba/perl/ch-1.pl | 26 ++++++++++++++++++++++++++ challenge-204/e-choroba/perl/ch-2.pl | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100755 challenge-204/e-choroba/perl/ch-1.pl create mode 100755 challenge-204/e-choroba/perl/ch-2.pl diff --git a/challenge-204/e-choroba/perl/ch-1.pl b/challenge-204/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..e3e964e256 --- /dev/null +++ b/challenge-204/e-choroba/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +sub monotonic_array($arr) { + my $first_cmp; + for my $i (1 .. $#$arr) { + my $cmp = $arr->[ $i - 1 ] <=> $arr->[$i]; + return 0 if $first_cmp && $cmp && $first_cmp != $cmp; + $first_cmp //= $cmp; + } + return 1 +} + +use Test::More tests => 3 + 5; + +is monotonic_array([1, 2, 2, 3]), 1, 'Example 1'; +is monotonic_array([1, 3, 2]), 0, 'Example 2'; +is monotonic_array([6, 5, 5, 4]), 1, 'Example 3'; + +is monotonic_array([5, 5, 4, 3]), 1, 'Starts flat'; +is monotonic_array([2, 2, 2]), 1, 'All flat'; +is monotonic_array([1, 2]), 1, 'Two elements'; +is monotonic_array([1]), 1, 'Single element'; +is monotonic_array([]), 1, 'Empty'; diff --git a/challenge-204/e-choroba/perl/ch-2.pl b/challenge-204/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..c02cfdbc9f --- /dev/null +++ b/challenge-204/e-choroba/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental 'signatures'; + +use PDL; + +sub reshape_matrix($matrix, $r, $c) { + my $p = pdl($matrix); + die 'Two dimensional matrix expected' unless $p->ndims == 2; + + return 0 unless $r * $c == $p->getdim(0) * $p->getdim(1); + + return unpdl($p->reshape($c, $r)) +} + +# Import explicitly, some functions (e.g. float) conflict with PDL's ones. +use Test2::V0 qw{ is plan }; +plan 3; + +is reshape_matrix([[1, 2], [3, 4]], 1, 4), [[1, 2, 3, 4]], 'Example 1'; +is reshape_matrix([[1, 2, 3], [4, 5, 6]], 3, 2), + [[1, 2], [3, 4], [5, 6]], + 'Example 2'; +is reshape_matrix([[1, 2]], 3, 2), 0, 'Example 3'; -- cgit From 8eff90065b666aff863f1a5c7368433a49f6e400 Mon Sep 17 00:00:00 2001 From: Carlos Eduardo de Oliveira Date: Tue, 14 Feb 2023 00:07:25 -0300 Subject: solution to challenge 204 --- challenge-204/carlos-oliveira/perl/ch-1.pl | 20 ++++++++++++++ challenge-204/carlos-oliveira/perl/ch-2.pl | 42 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 challenge-204/carlos-oliveira/perl/ch-1.pl create mode 100644 challenge-204/carlos-oliveira/perl/ch-2.pl diff --git a/challenge-204/carlos-oliveira/perl/ch-1.pl b/challenge-204/carlos-oliveira/perl/ch-1.pl new file mode 100644 index 0000000000..08e87559d2 --- /dev/null +++ b/challenge-204/carlos-oliveira/perl/ch-1.pl @@ -0,0 +1,20 @@ +use strict; +use warnings; +use v5.36; + +use List::MoreUtils qw(slide); +use List::Util qw(none); + +use Test::More; + +sub is_monotonic_array (@array) { + my @diffs = slide { $a <=> $b } @array; + # Perl is returning '' instead of 0 ? + return (none { $_ == -1 } @diffs) || (none { $_ == 1 } @diffs) ? 1 : 0; +} + +is is_monotonic_array (1,2,2,3), 1; +is is_monotonic_array (1,3,2), 0; +is is_monotonic_array (6,5,5,4), 1; + +done_testing; diff --git a/challenge-204/carlos-oliveira/perl/ch-2.pl b/challenge-204/carlos-oliveira/perl/ch-2.pl new file mode 100644 index 0000000000..8f5be7be1d --- /dev/null +++ b/challenge-204/carlos-oliveira/perl/ch-2.pl @@ -0,0 +1,42 @@ +use strict; +use warnings; +use v5.36; + +use experimental qw(refaliasing); + +use Test::More; + +sub reshape_matrix ($matrix, $nr, $nc) { + # get old rows and columns counts + my $or = $matrix->@*; + my $oc; + if (ref $matrix->[0] eq 'ARRAY') { + $oc = $matrix->[0]->@*; + } else { + $oc = 1; + $matrix = [$matrix]; + } + return 0 unless $nr * $nc == $or * $oc; + my @new_matrix; + my @new_row; + my $col = 0; + for \my @row (@$matrix) { + for my $elem (@row) { + push @new_row, $elem; + if (++$col == $nc) { + push @new_matrix, [@new_row]; + @new_row = (); + $col = 0; + } + } + } + return $nr == 1 ? $new_matrix[0] : \@new_matrix; +} + +is_deeply reshape_matrix ([ [ 1, 2 ], [ 3, 4 ] ], 1, 4), [ 1, 2, 3, 4 ]; +is_deeply reshape_matrix ([ [ 1, 2, 3 ] , [ 4, 5, 6 ] ], 3, 2), [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]; +is reshape_matrix ([ 1, 2 ], 3, 2), 0; +is_deeply reshape_matrix ([ [ 1, 2, 3, 4 ] ], 1, 4), [ 1, 2, 3, 4 ]; +is_deeply reshape_matrix ([ 1, 2, 3, 4 ], 2, 2), [ [ 1, 2 ], [ 3, 4 ] ]; + +done_testing; -- cgit From 6732f65edd92a303df28106373a5f9af7660ebba Mon Sep 17 00:00:00 2001 From: robbie-hatley Date: Mon, 13 Feb 2023 20:06:57 -0800 Subject: Robbie Hatley's Perl solutions for PWCC 204. --- challenge-204/robbie-hatley/blog.txt | 1 + challenge-204/robbie-hatley/perl/ch-1.pl | 59 ++++++++++++++++ challenge-204/robbie-hatley/perl/ch-2.pl | 118 +++++++++++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 challenge-204/robbie-hatley/blog.txt create mode 100755 challenge-204/robbie-hatley/perl/ch-1.pl create mode 100755 challenge-204/robbie-hatley/perl/ch-2.pl diff --git a/challenge-204/robbie-hatley/blog.txt b/challenge-204/robbie-hatley/blog.txt new file mode 100644 index 0000000000..1548752c4d --- /dev/null +++ b/challenge-204/robbie-hatley/blog.txt @@ -0,0 +1 @@ +https://hatley-software.blogspot.com/2023/02/robbie-hatleys-perl-solutions-to-weekly_13.html \ No newline at end of file diff --git a/challenge-204/robbie-hatley/perl/ch-1.pl b/challenge-204/robbie-hatley/perl/ch-1.pl new file mode 100755 index 0000000000..952cbbb867 --- /dev/null +++ b/challenge-204/robbie-hatley/perl/ch-1.pl @@ -0,0 +1,59 @@ +#! /usr/bin/perl +# Robbie Hatley's Solution to PWCC 204-1 + +=pod + +Task 1: Monotonic Array +Submitted by: Mohammad S Anwar +Given an array of integers, write a script that prints 1 if the given array +is Monotonic, else prints 0. +Example 1: Input: (1,2,2,3) Output: 1 +Example 2: Input: (1,3,2) Output: 0 +Example 3: Input: (6,5,5,4) Output: 1 + +=cut + +# IO NOTES: +# NOTE: Input is by either built-in array-of-arrays, or @ARGV. +# If using @ARGV,the args should be a space-separated sequence of +# integers, which will be interpreted as being a single array. +# NOTE: Output is to STDOUT and will be 1 if monotonic, 0 if not. + +# PRELIMINARIES: +use v5.36; +$"=", "; + +# DEFAULT INPUTS: +my @arrays = ( [1,2,2,3], [1,3,2], [6,5,5,4] ); + +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = ([@ARGV]);} + +# SUBROUTINES: + +sub is_mono (@a){ + my $mono; + $mono = 1; + for ( my $i = 1 ; $i <= $#a ; ++$i ){ + $mono &&= ($a[$i-1]<=$a[$i]);} # mono inc? + if ( $mono == 1 ) {return 1;} + $mono = 1; + for ( my $i = 1 ; $i <= $#a ; ++$i ){ + $mono &&= ($a[$i-1]>=$a[$i]);} # mono dec? + if ( $mono == 1 ) {return 2;} + return 0;} + +# MAIN BODY OF SCRIPT: +for (@arrays){ + my @array = @{$_}; + say ''; + say "array: (@array)"; + my $mono = is_mono(@array); + if ($mono == 0){ + say "not monotonic"} + elsif ($mono == 1){ + say "monotonically increasing"} + elsif ($mono == 2){ + say "monotonically decreasing"} + else { + say "We never had to take any of it seriously, did we?"}} \ No newline at end of file diff --git a/challenge-204/robbie-hatley/perl/ch-2.pl b/challenge-204/robbie-hatley/perl/ch-2.pl new file mode 100755 index 0000000000..5cfad41d99 --- /dev/null +++ b/challenge-204/robbie-hatley/perl/ch-2.pl @@ -0,0 +1,118 @@ +#! /usr/bin/perl +# Robbie Hatley's Solution to PWCC 204-2 + +=pod + +Task 2: Reshape Matrix +Submitted by: Mohammad S Anwar +Given an m-by-n matrix @m and two integers $r and $c, write a script to +reshape @m to r-by-c, re-using all of the original elements of @m. +If this is not possible, just print 0. + +Example 1: +Inputs: + old shape: + rows = 2 + cols = 2 + matrix: + 1 2 + 3 4 + new shape: + rows = 1 + cols = 4 +Output: + 1 2 3 4 + +Example 2: +Inputs: + old shape: + rows = 2 + cols = 3 + matrix: + 1 2 3 + 4 5 6 + new shape: + rows = 3 + cols = 2 +Output: + 1 2 + 3 4 + 5 6 + +Example 3: +Input: + old shape: + rows = 1 + cols = 2 + matrix: + 1 2 + new shape: + rows = 3 + cols = 2 +Output: + 0 + +=cut + +# IO NOTES: +# +# NOTE: Input is by either built-in array-of-arrays, or @ARGV. +# +# If using @ARGV,the args should be a space-separated sequence of +# integers, which should be the numbers of rows & columns of the +# original matrix, followed by the elements of the matrix in +# left-to-right-within-up-to-down order (like reading a book), +# followed by the numbers of rows & columns desired. For example, +# to reshape the matrix ([5,8],[1,4]) from 2-by-2 to 4-by-1: +# ./ch-2.pl 2 2 5 8 1 4 4 1 +# +# NOTE: Output is to STDOUT and will be the original matrix, followed by +# the reshaped matrix (or 0 if the matrix can't be reshaped to +# the given shape). + +# PRELIMINARIES: +use v5.36; +$"=" "; + +# SUBROUTINES: + +sub shape { + my $r = shift @_; + my $c = shift @_; + my @flat = @_; + if ($r*$c != scalar @flat){ + return ([0])} + my @rows; + my $i = 0; + for (@flat){ + $rows[int $i/$c]->[$i%$c]=$_; + ++$i} + return @rows} + +# DEFAULT INPUTS: +my @arrays = +( + [2,2,1,2,3,4,1,4], + [2,3,1,2,3,4,5,6,3,2], + [1,2,1,2,3,2] +); + +# NON-DEFAULT INPUTS: +if (@ARGV) {@arrays = ([@ARGV]);} + +# MAIN BODY OF SCRIPT: +for (@arrays){ + say ''; + my @array = @{$_}; + my $or = shift @array; + my $oc = shift @array; + my $nc = pop @array; + my $nr = pop @array; + my @old_rows = shape($or, $oc, @array); + my @new_rows = shape($nr, $nc, @array); + say "Original matrix ($or rows, $oc cols):"; + say "@{$_}" for @old_rows; + say "Reshaped matrix ($nr rows, $nc cols):"; + say "@{$_}" for @new_rows; + if (1 == @new_rows && 1 == @{$new_rows[0]} && 0 == $new_rows[0]->[0]){ + say "(Couldn't reshape.)"}} \ No newline at end of file -- cgit From 6051a217ebf6f4804bf3015fc670971a489aecb0 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 14 Feb 2023 08:03:38 +0000 Subject: - Added solutions by Mark Anderson. - Added solutions by Lubos Kolouch. - Added solutions by Luca Ferrari. - Added solutions by Andrew Shitov. - Added solutions by W. Luis Mochan. - Added solutions by Thomas Kohler. - Added solutions by Matthew Neleigh. - Added solutions by E. Choroba. - Added solutions by Carlos Oliveira. - Added solutions by Robbie Hatley. --- challenge-204/ash/chatgpt/README | 7 + challenge-204/ash/chatgpt/blog-1.txt | 1 + challenge-204/ash/chatgpt/blog-2.txt | 1 + challenge-204/ash/chatgpt/raku/ch-1.raku | 33 + challenge-204/ash/chatgpt/raku/ch-2.raku | 56 + challenge-204/chatgpt/README | 7 - challenge-204/chatgpt/blog-1.txt | 1 - challenge-204/chatgpt/blog-2.txt | 1 - challenge-204/chatgpt/raku/ch-1.raku | 33 - challenge-204/chatgpt/raku/ch-2.raku | 56 - challenge-204/eric-cheung/python/ch-1.py | 33 + challenge-204/eric-cheung/python/ch-2.py | 32 + challenge-204/robert-dicicco/julia/ch-1.jl | 101 ++ challenge-204/robert-dicicco/raku/ch-1.raku | 91 ++ challenge-204/robert-dicicco/ruby/ch-1.rb | 79 ++ stats/pwc-challenge-203.json | 570 +++++++++ stats/pwc-current.json | 551 ++------- stats/pwc-language-breakdown-summary.json | 56 +- stats/pwc-language-breakdown.json | 1401 ++++++++++----------- stats/pwc-leaders.json | 468 +++---- stats/pwc-summary-1-30.json | 114 +- stats/pwc-summary-121-150.json | 120 +- stats/pwc-summary-151-180.json | 98 +- stats/pwc-summary-181-210.json | 98 +- stats/pwc-summary-211-240.json | 42 +- stats/pwc-summary-241-270.json | 114 +- stats/pwc-summary-271-300.json | 34 +- stats/pwc-summary-31-60.json | 36 +- stats/pwc-summary-61-90.json | 100 +- stats/pwc-summary-91-120.json | 102 +- stats/pwc-summary.json | 1758 +++++++++++++-------------- 31 files changed, 3395 insertions(+), 2799 deletions(-) create mode 100644 challenge-204/ash/chatgpt/README create mode 100644 challenge-204/ash/chatgpt/blog-1.txt create mode 100644 challenge-204/ash/chatgpt/blog-2.txt create mode 100644 challenge-204/ash/chatgpt/raku/ch-1.raku create mode 100644 challenge-204/ash/chatgpt/raku/ch-2.raku delete mode 100644 challenge-204/chatgpt/README delete mode 100644 challenge-204/chatgpt/blog-1.txt delete mode 100644 challenge-204/chatgpt/blog-2.txt delete mode 100644 challenge-204/chatgpt/raku/ch-1.raku delete mode 100644 challenge-204/chatgpt/raku/ch-2.raku create mode 100755 challenge-204/eric-cheung/python/ch-1.py create mode 100755 challenge-204/eric-cheung/python/ch-2.py create mode 100644 challenge-204/robert-dicicco/julia/ch-1.jl create mode 100644 challenge-204/robert-dicicco/raku/ch-1.raku create mode 100644 challenge-204/robert-dicicco/ruby/ch-1.rb create mode 100644 stats/pwc-challenge-203.json diff --git a/challenge-204/ash/chatgpt/README b/challenge-204/ash/chatgpt/README new file mode 100644 index 0000000000..7735e06a24 --- /dev/null +++ b/challenge-204/ash/chatgpt/README @@ -0,0 +1,7 @@ +Solutions generated by ChatGPT. + +The solutions are based on the description and the examples that the Weekly Challenge offers. It was sent to the chat as they are published on the website without any modifications. + +The solution located in this directory are the first versions that ChatGPT generated. The code is taken as soon as it compiles and gives correct results. Optimised versions with a more natural Raku syntax are placed at ../ash. All those were also done in a dialogue with the machine with some hints from a human. + +Submitted by Andrew Shitov (ash). diff --git a/challenge-204/ash/chatgpt/blog-1.txt b/challenge-204/ash/chatgpt/blog-1.txt new file mode 100644 index 0000000000..59e520d49a --- /dev/null +++ b/challenge-204/ash/chatgpt/blog-1.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/02/13/dialogues-with-chatpgp-about-the-raku-programming-languages-solving-the-weekly-challange-204/ diff --git a/challenge-204/ash/chatgpt/blog-2.txt b/challenge-204/ash/chatgpt/blog-2.txt new file mode 100644 index 0000000000..8fabcdca67 --- /dev/null +++ b/challenge-204/ash/chatgpt/blog-2.txt @@ -0,0 +1 @@ +https://andrewshitov.com/2023/02/13/solving-task-2-of-the-weekly-challenge-204-with-the-help-of-chatgpt/ diff --git a/challenge-204/ash/chatgpt/raku/ch-1.raku b/challenge-204/ash/chatgpt/raku/ch-1.raku new file mode 100644 index 0000000000..cbc18bdd63 --- /dev/null +++ b/challenge-204/ash/chatgpt/raku/ch-1.raku @@ -0,0 +1,33 @@ +# This program has been created by ChatGPT +# +# Test run: +# $ raku ch-1.raku +# 1 +# 0 +# 1 + +sub is_monotonic(@nums) { + my $increasing = True; + my $decreasing = True; + for (0 ..^ @nums.end) -> $i { + for ($i + 1 .. @nums.end) -> $j { + if (@nums[$i] > @nums[$j]) { + $increasing = False; + } + if (@nums[$i] < @nums[$j]) { + $decreasing = False; + } + } + } + return 1 if $increasing || $decreasing; + return 0; + } + +my @nums = (1, 2, 2, 3); +say is_monotonic(@nums); + +@nums = (1, 3, 2); +say is_monotonic(@nums); + +@nums = (6, 5, 5, 4); +say is_monotonic(@nums); diff --git a/challenge-204/ash/chatgpt/raku/ch-2.raku b/challenge-204/ash/chatgpt/raku/ch-2.raku new file mode 100644 index 0000000000..20375d1164 --- /dev/null +++ b/challenge-204/ash/chatgpt/raku/ch-2.raku @@ -0,0 +1,56 @@ +# This program has been created by ChatGPT +# +# Test run: +# $ raku ch-2.raku +# [1 2 3 4] +# [[1 2] [3 4] [5 6]] +# [[1] [2]] +# +# Notice that if you change $r and $c to 3 and 2 in the last example, +# the program prints 0 as expected. + +sub reshape-matrix (@matrix, $r, $c) { + my $total_elements = 0; + for @matrix -> @row { + $total_elements += @row.elems; + } + if $total_elements != $r * $c { + return 0; + } + + my @result = []; + my $element_index = 0; + for (0 .. $r - 1) -> $i { + my @new_row = []; + for (0 .. $c - 1) -> $j { + @new_row.push(@matrix[$element_index div @matrix[0].elems][$element_index % @matrix[0].elems]); + $element_index++; + } + @result.push(@new_row); + } + + if @result.elems == 1 { + return @result[0]; + } + else { + return @result; + } +} + +my @matrix = [ [ 1, 2 ], [ 3, 4 ] ]; +my $r = 1; +my $c = 4; +my $result = reshape-matrix(@matrix, $r, $c); +say $result; + +@matrix = [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]; +$r = 3; +$c = 2; +$result = reshape-matrix(@matrix, $r, $c