From 16c4b1beb68e6a046b3eea7e947cc09c6be2e75f Mon Sep 17 00:00:00 2001 From: "Jaldhar H. Vyas" Date: Fri, 13 Jan 2023 03:44:01 -0500 Subject: Challenge 198 by Jaldhar H. Vyas. --- challenge-198/jaldhar-h-vyas/blog.txt | 1 + challenge-198/jaldhar-h-vyas/perl/ch-1.pl | 17 +++++++++++++++++ challenge-198/jaldhar-h-vyas/perl/ch-2.pl | 27 +++++++++++++++++++++++++++ challenge-198/jaldhar-h-vyas/raku/ch-1.raku | 17 +++++++++++++++++ challenge-198/jaldhar-h-vyas/raku/ch-2.sh | 3 +++ 5 files changed, 65 insertions(+) create mode 100644 challenge-198/jaldhar-h-vyas/blog.txt create mode 100755 challenge-198/jaldhar-h-vyas/perl/ch-1.pl create mode 100755 challenge-198/jaldhar-h-vyas/perl/ch-2.pl create mode 100755 challenge-198/jaldhar-h-vyas/raku/ch-1.raku create mode 100755 challenge-198/jaldhar-h-vyas/raku/ch-2.sh diff --git a/challenge-198/jaldhar-h-vyas/blog.txt b/challenge-198/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..81e9a32575 --- /dev/null +++ b/challenge-198/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/01/perl_weekly_challenge_week_198.html \ No newline at end of file diff --git a/challenge-198/jaldhar-h-vyas/perl/ch-1.pl b/challenge-198/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..4fd9ef1265 --- /dev/null +++ b/challenge-198/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @list = sort {$a <=> $b} @ARGV; + +if (scalar @list < 2) { + say 0; +} else { + my %gaps; + + for my $i (1 .. scalar @list - 1) { + push @{$gaps{($list[$i] - $list[$i - 1])}}, [$list[$i - 1], $list[$i]]; + } + + say scalar @{ $gaps{(sort { $gaps{$b} <=> $gaps{$a} } keys %gaps)[0]} }; +} diff --git a/challenge-198/jaldhar-h-vyas/perl/ch-2.pl b/challenge-198/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..e7a1d0f8c1 --- /dev/null +++ b/challenge-198/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub isPrime { + my ($n) = @_; + + if ($n < 2) { + return undef; + } + + if ($n == 2) { + return 1; + } + + for my $i (2 .. sqrt($n)) { + if ($n % $i == 0) { + return undef; + } + } + + return 1; +} + +my $n = shift // die "need an integer > 0\n"; + +say scalar grep { isPrime($_) } 0 .. $n; diff --git a/challenge-198/jaldhar-h-vyas/raku/ch-1.raku b/challenge-198/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..2a9a52fdc1 --- /dev/null +++ b/challenge-198/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/raku + +sub MAIN(*@args) { + my @list = @args.sort({$^a <=> $^b}); + + if @list.elems < 2 { + say 0; + } else { + my %gaps; + + for 1 .. @list.end -> $i { + %gaps{(@list[$i] - @list[$i - 1])}.push([@list[$i - 1], @list[$i]]); + } + + %gaps.sort({ $^b.value.elems <=> $^a.value.elems }).first.value.elems.say; + } +} \ No newline at end of file diff --git a/challenge-198/jaldhar-h-vyas/raku/ch-2.sh b/challenge-198/jaldhar-h-vyas/raku/ch-2.sh new file mode 100755 index 0000000000..bd6912c59a --- /dev/null +++ b/challenge-198/jaldhar-h-vyas/raku/ch-2.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -e 'my $n = @*ARGS[0]; (0 .. $n).grep({ .is-prime }).elems.say' $@ \ No newline at end of file -- cgit From 25e5a2d3e45800e6ff6c24ba76c1b7f753113017 Mon Sep 17 00:00:00 2001 From: Mark <53903062+andemark@users.noreply.github.com> Date: Mon, 16 Jan 2023 07:27:16 +0000 Subject: Initial 200 (Raku) --- challenge-200/mark-anderson/raku/ch-1.raku | 18 ++++ challenge-200/mark-anderson/raku/ch-2.raku | 130 +++++++++++++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 challenge-200/mark-anderson/raku/ch-1.raku create mode 100644 challenge-200/mark-anderson/raku/ch-2.raku diff --git a/challenge-200/mark-anderson/raku/ch-1.raku b/challenge-200/mark-anderson/raku/ch-1.raku new file mode 100644 index 0000000000..cbec03c451 --- /dev/null +++ b/challenge-200/mark-anderson/raku/ch-1.raku @@ -0,0 +1,18 @@ +#!/usr/bin/env raku +use Test; + +is arithmetic-slices(1,2,3,4), ((1,2,3), (2,3,4), (1,2,3,4)); +is arithmetic-slices(2), (); +is arithmetic-slices(1,2,3,5,6,7), ((1,2,3), (5,6,7)); +is arithmetic-slices(1,4,7,8,9,11,13), ((1,4,7), (7,8,9), (9,11,13)); + +sub arithmetic-slices(*@array) +{ + gather for 3..@array.elems + { + for @array.rotor($_ => -.pred) + { + .take if [==] .rotor(2 => -1).map({ .[0] - .[1] }) + } + } +} diff --git a/challenge-200/mark-anderson/raku/ch-2.raku b/challenge-200/mark-anderson/raku/ch-2.raku new file mode 100644 index 0000000000..d8332f7aaf --- /dev/null +++ b/challenge-200/mark-anderson/raku/ch-2.raku @@ -0,0 +1,130 @@ +#!/usr/bin/env raku +use Test; + +is ASCII-Art(200), chomp q:to/END/; + ------- ------- ------- + | | | | | + | | | | | + ------- | | | | + | | | | | + | | | | | + ------- ------- ------- + END + +is ASCII-Art(5280), chomp q:to/END/; + ------- ------- ------- ------- + | | | | | | + | | | | | | + ------- ------- ------- | | + | | | | | | + | | | | | | + ------- ------- ------- ------- + END + +sub ASCII-Art($n) +{ + my @digits = + [ + (chomp q:to/END/).split("\n").Array, + ------- + | | + | | + | | + | | + | | + ------- + END + + (chomp q:to/END/).split("\n").Array, + | + | + | + | + | + | + | + END + + (chomp q:to/END/).split("\n").Array, + ------- + | + | + ------- + | + | + ------- + END + + (chomp q:to/END/).split("\n").Array, + ------- + | + | + ------- + | + | + ------- + END + + (chomp q:to/END/).split("\n").Array, + | | + | | + | | + ------- + | + | + | + END + + (chomp q:to/END/).split("\n").Array, + ------- + | + | + ------- + | + | + ------- + END + + (chomp q:to/END/).split("\n").Array, + ------- + | + | + ------- + | | + | | + ------- + END + + (chomp q:to/END/).split("\n").Array, + ------- + | + | + | + | + | + | + END + + (chomp q:to/END/).split("\n").Array, + ------- + | | + | | + ------- + | | + | | + ------- + END + + (chomp q:to/END/).split("\n").Array + ------- + | | + | | + ------- + | + | + ------- + END + ]; + + ([Z] @digits[$n.comb]).join("\n") +} -- cgit From 9e638419327ceacef7cb2d571aed2750d533b1d7 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 09:29:28 +0100 Subject: Task 1 done --- challenge-200/luca-ferrari/raku/ch-1.p6 | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 challenge-200/luca-ferrari/raku/ch-1.p6 diff --git a/challenge-200/luca-ferrari/raku/ch-1.p6 b/challenge-200/luca-ferrari/raku/ch-1.p6 new file mode 100644 index 0000000000..7acf58814b --- /dev/null +++ b/challenge-200/luca-ferrari/raku/ch-1.p6 @@ -0,0 +1,34 @@ +#!raku + +# +# Perl Weekly Challenge 200 +# Task 1 +# +# See +# + +sub MAIN( *@list where { @list.elems == @list.grep( * ~~ Int ).elems } ) { + my @slices; + + for 0 ^..^ @list.elems - 1 -> $center { + for 1 .. $center { + my ( $start, $end ) = $center - $_, $center + $_; + $start = 0 if $start < 0; + $end = @list.elems - 1 if $end >= @list.elems; + + my @seeking = @list[ $start .. $end ]; + my $ok = @list.elems >= 3; + for 1 ..^ @seeking.elems { + state $difference = @seeking[ $_ ] - @seeking[ $_ - 1 ]; + $ok = False and last if @seeking[ $_ ] - @seeking[ $_ - 1 ] != $difference; + } + + @slices.push: @seeking if $ok; + } + } + + @slices.join( "\n" ).say; + + + +} -- cgit From bc210de1838b13167645ae1419889b64ad132e1d Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 09:51:13 +0100 Subject: Task 2 done --- challenge-200/luca-ferrari/raku/ch-2.p6 | 125 ++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 challenge-200/luca-ferrari/raku/ch-2.p6 diff --git a/challenge-200/luca-ferrari/raku/ch-2.p6 b/challenge-200/luca-ferrari/raku/ch-2.p6 new file mode 100644 index 0000000000..a2a993b395 --- /dev/null +++ b/challenge-200/luca-ferrari/raku/ch-2.p6 @@ -0,0 +1,125 @@ +#!raku + +# +# Perl Weekly Challenge 200 +# Task 2 +# +# See +# + +sub MAIN( Int $value = 200, Bool :$sign = False ) { + + my @sign = [ + [ ' ', + ' ', + ' ', + ' ----- ', + ' ', + ' ', + ' ', + ], + [ ' ', + ' ', + ' | ', + ' ----- ', + ' | ', + ' ', + ' ', + ], + ]; + + my @lcd = [ + [ ' -------- ', + '| |', + '| |', + '| |', + '| |', + '| |', + ' -------- ', + ], + [ ' ', + ' |', + ' |', + ' |', + ' |', + ' |', + ' ', + ], + [ ' ------ ', + ' |', + ' |', + ' ------ ', + '| ', + '| ', + ' ------ ', + ], + + [ ' ------ ', + ' |', + ' |', + ' ------ ', + ' |', + ' |', + ' ------ ', + ], + + [ '| |', + '| |', + '| |', + ' ------ ', + ' |', + ' |', + ' ', + ], + [ ' ------ ', + '| ', + '| ', + ' ------ ', + ' |', + ' |', + ' ', + ], + [ ' ------ ', + '| ', + '| ', + ' ------ ', + '| |', + '| |', + ' ------ ', + ], + + [ ' -------', + ' |', + ' |', + ' | ', + ' |', + ' |', + ' ', + ], + + [ ' -------- ', + '| |', + '| |', + ' ------- ', + '| |', + '| |', + ' -------- ', + ], + + [ ' -------- ', + '| |', + '| |', + ' ------- ', + ' |', + ' |', + ' -------- ', + ], + + + ]; + + my @display; + @display.push: @sign[ $value > 0 ?? 1 !! 0 ] if ( $value < 0 || $sign ); + @display.push: |@lcd[ $value.comb ]; + ( [Z] |@display ).join( "\n" ).say; +} -- cgit From 690be1c0b847c56acce9a2927f9dae8d4c151a97 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 11:16:31 +0100 Subject: Task 1 plperl --- challenge-200/luca-ferrari/postgresql/ch-1.plperl | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 challenge-200/luca-ferrari/postgresql/ch-1.plperl diff --git a/challenge-200/luca-ferrari/postgresql/ch-1.plperl b/challenge-200/luca-ferrari/postgresql/ch-1.plperl new file mode 100644 index 0000000000..53d25fae3b --- /dev/null +++ b/challenge-200/luca-ferrari/postgresql/ch-1.plperl @@ -0,0 +1,37 @@ +-- +-- Perl Weekly Challenge 200 +-- Task 1 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc200; + +CREATE OR REPLACE FUNCTION +pwc200.task1_plperl( int[] ) +RETURNS SETOF int[] +AS $CODE$ + my ( $list ) = @_; + my @slices; + + for my $center ( 1 .. $list->@* - 1 ) { + for ( 1 .. $center ) { + my ( $start, $end ) = ( $center - $_, $center + $_ ); + $start = 0 if $start < 0; + $end = $list->@* - 1 if $end >= $list->@*; + + my @seeking = $list->@[ $start .. $end ]; + my $ok = 1; + my $difference = undef; + + for ( 1 .. $#seeking ) { + $difference = $seeking[ $_ ] - $seeking[ $_ - 1 ] if ! defined( $difference ); + $ok = 0 and last if $seeking[ $_ ] - $seeking[ $_ - 1 ] != $difference; + } + + return_next( [@seeking] ) if $ok and scalar( @seeking ) >= 3; + } + } + +return; +$CODE$ +LANGUAGE plperl; -- cgit From ad488d50e99fd7515521e0902671b2c24d032ad3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 11:42:48 +0100 Subject: Task 2 plperl done --- challenge-200/luca-ferrari/postgresql/ch-2.plperl | 122 ++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 challenge-200/luca-ferrari/postgresql/ch-2.plperl diff --git a/challenge-200/luca-ferrari/postgresql/ch-2.plperl b/challenge-200/luca-ferrari/postgresql/ch-2.plperl new file mode 100644 index 0000000000..d436feaeee --- /dev/null +++ b/challenge-200/luca-ferrari/postgresql/ch-2.plperl @@ -0,0 +1,122 @@ +-- +-- Perl Weekly Challenge 200 +-- Task 2 +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc200; + +CREATE OR REPLACE FUNCTION +pwc200.task2_plperl( int ) +RETURNS text +AS $CODE$ + + use List::MoreUtils qw/zip/; + my ( $value ) = @_; + + my $lcd = [ + [ ' -------- ', + '| |', + '| |', + '| |', + '| |', + '| |', + ' -------- ', + ], + [ ' ', + ' |', + ' |', + ' |', + ' |', + ' |', + ' ', + ], + [ ' ------ ', + ' |', + ' |', + ' ------ ', + '| ', + '| ', + ' ------ ', + ], + + [ ' ------ ', + ' |', + ' |', + ' ------ ', + ' |', + ' |', + ' ------ ', + ], + + [ '| |', + '| |', + '| |', + ' ------ ', + ' |', + ' |', + ' ', + ], + [ ' ------ ', + '| ', + '| ', + ' ------ ', + ' |', + ' |', + ' ', + ], + [ ' ------ ', + '| ', + '| ', + ' ------ ', + '| |', + '| |', + ' ------ ', + ], + + [ ' -------', + ' |', + ' |', + ' | ', + ' |', + ' |', + ' ', + ], + + [ ' -------- ', + '| |', + '| |', + ' ------- ', + '| |', + '| |', + ' -------- ', + ], + + [ ' -------- ', + '| |', + '| |', + ' ------- ', + ' |', + ' |', + ' -------- ', + ], + + + ]; + + + my $display; + + for my $row ( 0 .. 6 ) { + for ( split '', $value ) { + $display .= ' ' . $lcd->[ $_ ]->[ $row ]; + } + + $display .= "\n"; + } + + $display .= "\n"; + + return $display; +$CODE$ +LANGUAGE plperlu; -- cgit From 0c2e9c4fd01398cca5d1a65a1456190615e7a8d3 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 11:53:43 +0100 Subject: Task 1 plpgsql --- challenge-200/luca-ferrari/postgresql/ch-1.sql | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 challenge-200/luca-ferrari/postgresql/ch-1.sql diff --git a/challenge-200/luca-ferrari/postgresql/ch-1.sql b/challenge-200/luca-ferrari/postgresql/ch-1.sql new file mode 100644 index 0000000000..67ec06abc9 --- /dev/null +++ b/challenge-200/luca-ferrari/postgresql/ch-1.sql @@ -0,0 +1,27 @@ +-- +-- Perl Weekly Challenge 200 +-- Task 1 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc200; + +CREATE OR REPLACE FUNCTION +pwc200.task1_plpgsql( list int[] ) +RETURNS SETOF int[] +AS $CODE$ +DECLARE +BEGIN + + FOR i IN 2 .. array_length( list, 1 ) - 1 LOOP + IF list[ i + 1 ] - list [ i ] = list[ i ] - list[ i - 1 ] THEN + RETURN NEXT array[ list[ i - 1 ], list[ i ], list[ i + 1 ] ]; + END IF; + END LOOP; + +RETURN; + +END +$CODE$ +LANGUAGE plpgsql; -- cgit From 07dbf3342db050323bf9d7d866bb64071bd27cb7 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 11:54:50 +0100 Subject: Task 2 done plperl --- challenge-200/luca-ferrari/postgresql/ch-2.plperl | 3 +-- challenge-200/luca-ferrari/postgresql/ch-2.sql | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 challenge-200/luca-ferrari/postgresql/ch-2.sql diff --git a/challenge-200/luca-ferrari/postgresql/ch-2.plperl b/challenge-200/luca-ferrari/postgresql/ch-2.plperl index d436feaeee..d30bb62302 100644 --- a/challenge-200/luca-ferrari/postgresql/ch-2.plperl +++ b/challenge-200/luca-ferrari/postgresql/ch-2.plperl @@ -11,7 +11,6 @@ pwc200.task2_plperl( int ) RETURNS text AS $CODE$ - use List::MoreUtils qw/zip/; my ( $value ) = @_; my $lcd = [ @@ -119,4 +118,4 @@ AS $CODE$ return $display; $CODE$ -LANGUAGE plperlu; +LANGUAGE plperl; diff --git a/challenge-200/luca-ferrari/postgresql/ch-2.sql b/challenge-200/luca-ferrari/postgresql/ch-2.sql new file mode 100644 index 0000000000..0e07041c87 --- /dev/null +++ b/challenge-200/luca-ferrari/postgresql/ch-2.sql @@ -0,0 +1,16 @@ +-- +-- Perl Weekly Challenge 200 +-- Task 2 +-- +-- See +-- + +CREATE SCHEMA IF NOT EXISTS pwc200; + +CREATE OR REPLACE FUNCTION +pwc200.task2_plpgsql( v int ) +RETURNS text +AS $CODE$ + SELECT pwc200.task2_plperl( v ); +$CODE$ +LANGUAGE sql; -- cgit From 7f5cecf828ccf85f2be21d1a2d65bf6aaae2e640 Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 16 Jan 2023 13:16:50 +0100 Subject: Blog references --- challenge-200/luca-ferrari/blog-1.txt | 1 + challenge-200/luca-ferrari/blog-2.txt | 1 + challenge-200/luca-ferrari/blog-3.txt | 1 + challenge-200/luca-ferrari/blog-4.txt | 1 + challenge-200/luca-ferrari/blog-5.txt | 1 + challenge-200/luca-ferrari/blog-6.txt | 1 + 6 files changed, 6 insertions(+) create mode 100644 challenge-200/luca-ferrari/blog-1.txt create mode 100644 challenge-200/luca-ferrari/blog-2.txt create mode 100644 challenge-200/luca-ferrari/blog-3.txt create mode 100644 challenge-200/luca-ferrari/blog-4.txt create mode 100644 challenge-200/luca-ferrari/blog-5.txt create mode 100644 challenge-200/luca-ferrari/blog-6.txt diff --git a/challenge-200/luca-ferrari/blog-1.txt b/challenge-200/luca-ferrari/blog-1.txt new file mode 100644 index 0000000000..c2555753bd --- /dev/null +++ b/challenge-200/luca-ferrari/blog-1.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/16/PerlWeeklyChallenge200.html#task1 diff --git a/challenge-200/luca-ferrari/blog-2.txt b/challenge-200/luca-ferrari/blog-2.txt new file mode 100644 index 0000000000..7840fdbd4a --- /dev/null +++ b/challenge-200/luca-ferrari/blog-2.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/16/PerlWeeklyChallenge200.html#task2 diff --git a/challenge-200/luca-ferrari/blog-3.txt b/challenge-200/luca-ferrari/blog-3.txt new file mode 100644 index 0000000000..3e390efa81 --- /dev/null +++ b/challenge-200/luca-ferrari/blog-3.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/16/PerlWeeklyChallenge200.html#task1plperl diff --git a/challenge-200/luca-ferrari/blog-4.txt b/challenge-200/luca-ferrari/blog-4.txt new file mode 100644 index 0000000000..1032ec59ed --- /dev/null +++ b/challenge-200/luca-ferrari/blog-4.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/16/PerlWeeklyChallenge200.html#task2plperl diff --git a/challenge-200/luca-ferrari/blog-5.txt b/challenge-200/luca-ferrari/blog-5.txt new file mode 100644 index 0000000000..5beac38ce5 --- /dev/null +++ b/challenge-200/luca-ferrari/blog-5.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/16/PerlWeeklyChallenge200.html#task1plpgsql diff --git a/challenge-200/luca-ferrari/blog-6.txt b/challenge-200/luca-ferrari/blog-6.txt new file mode 100644 index 0000000000..a68cbb119d --- /dev/null +++ b/challenge-200/luca-ferrari/blog-6.txt @@ -0,0 +1 @@ +https://fluca1978.github.io/2023/01/16/PerlWeeklyChallenge200.html#task2plpgsql -- cgit From 270c52dd984860530ebb708d264b24cf643c2ae8 Mon Sep 17 00:00:00 2001 From: dcw Date: Mon, 16 Jan 2023 20:14:01 +0000 Subject: here are my solutions to last week's challenge (oops), in Perl and C --- challenge-199/duncan-c-white/C/Makefile | 19 +++ challenge-199/duncan-c-white/C/README | 9 ++ challenge-199/duncan-c-white/C/args.c | 207 ++++++++++++++++++++++++++++ challenge-199/duncan-c-white/C/args.h | 11 ++ challenge-199/duncan-c-white/C/ch-1.c | 114 +++++++++++++++ challenge-199/duncan-c-white/C/ch-2.c | 134 ++++++++++++++++++ challenge-199/duncan-c-white/C/parseints.c | 114 +++++++++++++++ challenge-199/duncan-c-white/C/parseints.h | 1 + challenge-199/duncan-c-white/C/printarray.c | 39 ++++++ challenge-199/duncan-c-white/C/printarray.h | 1 + challenge-199/duncan-c-white/README | 72 ++++++---- challenge-199/duncan-c-white/perl/ch-1.pl | 77 +++++++++++ challenge-199/duncan-c-white/perl/ch-2.pl | 80 +++++++++++ 13 files changed, 852 insertions(+), 26 deletions(-) create mode 100644 challenge-199/duncan-c-white/C/Makefile create mode 100644 challenge-199/duncan-c-white/C/README create mode 100644 challenge-199/duncan-c-white/C/args.c create mode 100644 challenge-199/duncan-c-white/C/args.h create mode 100644 challenge-199/duncan-c-white/C/ch-1.c create mode 100644 challenge-199/duncan-c-white/C/ch-2.c create mode 100644 challenge-199/duncan-c-white/C/parseints.c create mode 100644 challenge-199/duncan-c-white/C/parseints.h create mode 100644 challenge-199/duncan-c-white/C/printarray.c create mode 100644 challenge-199/duncan-c-white/C/printarray.h create mode 100755 challenge-199/duncan-c-white/perl/ch-1.pl create mode 100755 challenge-199/duncan-c-white/perl/ch-2.pl diff --git a/challenge-199/duncan-c-white/C/Makefile b/challenge-199/duncan-c-white/C/Makefile new file mode 100644 index 0000000000..bf2d32d1a1 --- /dev/null +++ b/challenge-199/duncan-c-white/C/Makefile @@ -0,0 +1,19 @@ +# Makefile rules generated by CB +CC = gcc +CFLAGS = -Wall -g +LDFLAGS = -lm +BUILD = ch-1 ch-2 + +all: $(BUILD) + +clean: + /bin/rm -f $(BUILD) *.o core a.out + +args.o: args.c +ch-1: ch-1.o args.o parseints.o printarray.o +ch-1.o: ch-1.c args.h parseints.h printarray.h +ch-2: ch-2.o args.o parseints.o printarray.o +ch-2.o: ch-2.c args.h parseints.h printarray.h +parseints.o: parseints.c args.h parseints.h printarray.h +printarray.o: printarray.c + diff --git a/challenge-199/duncan-c-white/C/README b/challenge-199/duncan-c-white/C/README new file mode 100644 index 0000000000..5f960417f8 --- /dev/null +++ b/challenge-199/duncan-c-white/C/README @@ -0,0 +1,9 @@ +Thought I'd also have a go at translating ch-1.pl and ch-2.pl into C.. + +Both C versions produce near-identical (non-debugging and even debugging) +output to the Perl originals. + +They use several of my regular support modules: +- a command-line argument processing module args.[ch], +- a csvlist-of-int parsing module parseints.[ch], and +- an int-array printing module printarray.[ch]. diff --git a/challenge-199/duncan-c-white/C/args.c b/challenge-199/duncan-c-white/C/args.c new file mode 100644 index 0000000000..d4a2d38b9a --- /dev/null +++ b/challenge-199/duncan-c-white/C/args.c @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include + + +bool debug = false; + + +// process_flag_noarg( name, argc, argv ); +// Process the -d flag, and check that there are no +// remaining arguments. +void process_flag_noarg( char *name, int argc, char **argv ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != 0 ) + { + fprintf( stderr, "Usage: %s [-d]\n", name ); + exit(1); + } +} + + +// int argno = process_flag_n_args( name, argc, argv, n, argmsg ); +// Process the -d flag, and check that there are exactly +// n remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_args( char *name, int argc, char **argv, int n, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != n ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Exactly %d " + "arguments needed\n", name, argmsg, n ); + exit(1); + } + return arg; +} + + +// int argno = process_flag_n_m_args( name, argc, argv, min, max, argmsg ); +// Process the -d flag, and check that there are between +// min and max remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_m_args( char *name, int argc, char **argv, int min, int max, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < min || left > max ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Between %d and %d " + "arguments needed\n", name, argmsg, min, max ); + exit(1); + } + return arg; +} + + +// process_onenumarg_default( name, argc, argv, defvalue, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument (or no arguments, in which case +// we use the defvalue), putting it into n +void process_onenumarg_default( char *name, int argc, char **argv, int defvalue, int *n ) +{ + char argmsg[100]; + sprintf( argmsg, "[int default %d]", defvalue ); + int arg = process_flag_n_m_args( name, argc, argv, 0, 1, argmsg ); + + *n = arg == argc ? defvalue : atoi( argv[arg] ); +} + + +// process_onenumarg( name, argc, argv, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument, putting it into n +void process_onenumarg( char *name, int argc, char **argv, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 1, "int" ); + + // argument is in argv[arg] + *n = atoi( argv[arg] ); +} + + +// process_twonumargs( name, argc, argv, &m, &n ); +// Process the -d flag, and check that there are 2 +// remaining numeric arguments, putting them into m and n +void process_twonumargs( char *name, int argc, char **argv, int *m, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 2, "int" ); + + // arguments are in argv[arg] and argv[arg+1] + *m = atoi( argv[arg++] ); + *n = atoi( argv[arg] ); +} + + +// process_twostrargs() IS DEPRECATED: use process_flag_n_m_args() instead + + +// int arr[100]; +// int nel = process_listnumargs( name, argc, argv, arr, 100 ); +// Process the -d flag, and check that there are >= 2 +// remaining numeric arguments, putting them into arr[0..nel-1] +// and returning nel. +int process_listnumargs( char *name, int argc, char **argv, int *arr, int maxel ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < 2 ) + { + fprintf( stderr, "Usage: %s [-d] list_of_numeric_args\n", name ); + exit(1); + } + if( left > maxel ) + { + fprintf( stderr, "%s: more than %d args\n", name, maxel ); + exit(1); + } + + // elements are in argv[arg], argv[arg+1]... + + if( debug ) + { + printf( "debug: remaining arguments are in arg=%d, " + "firstn=%s, secondn=%s..\n", + arg, argv[arg], argv[arg+1] ); + } + + int nel = 0; + for( int i=arg; i +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +typedef struct { + int i, j; +} pair; + + +typedef struct { + int npairs; // number of pairs. + int nalloc; // number of pairs allocated (nalloc >= npairs) + pair *p; // block to store up to pairs, of which + // the first are in use at present. +} pairdynarray; + + +void init_pairdynarray( pairdynarray *p ) +{ + p->npairs = 0; + p->nalloc = 10; + p->p = malloc( p->nalloc * sizeof(pair) ); + assert( p->p != NULL ); +} + + +void add_pair( pairdynarray *p, int i, int j ) +{ + p->npairs++; + if( p->npairs == p->nalloc ) + { + p->nalloc += 10; + p->p = realloc( p->p, p->nalloc * sizeof(pair) ); + assert( p->p != NULL ); + } + p->p[p->npairs].i = i; + p->p[p->npairs].j = j; +} + + +void free_pairdynarray( pairdynarray *p ) +{ + if( p->p != NULL ) free( p->p ); + p->p = NULL; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "good-pairs", argc, argv, + 1, 1000, "intlist" ); + + int nel; + int *list = parse_int_args( argc, argv, argno, &nel ); + + if( nel < 1 ) + { + fprintf( stderr, "good-pairs: need a list of > 0 elements\n" ); + exit(1); + } + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + pairdynarray p; + init_pairdynarray( &p ); + + for( int i=0; i +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +typedef struct { + int i, j, k; +} triple; + + +typedef struct { + int ntriples; // number of triples. + int nalloc; // number of triples allocated (nalloc >= ntriples) + triple *p; // block to store up to triples, of which + // the first are in use at present. +} tripledynarray; + + +void init_tripledynarray( tripledynarray *t ) +{ + t->ntriples = 0; + t->nalloc = 10; + t->p = malloc( t->nalloc * sizeof(triple) ); + assert( t->p != NULL ); +} + + +void add_triple( tripledynarray *t, int i, int j, int k ) +{ + t->ntriples++; + if( t->ntriples == t->nalloc ) + { + t->nalloc += 10; + t->p = realloc( t->p, t->nalloc * sizeof(triple) ); + assert( t->p != NULL ); + } + t->p[t->ntriples].i = i; + t->p[t->ntriples].j = j; + t->p[t->ntriples].k = k; +} + + +void free_tripledynarray( tripledynarray *t ) +{ + if( t->p != NULL ) free( t->p ); + t->p = NULL; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "good-triples", argc, argv, + 4, 1000, "intlist" ); + + int x = atoi( argv[argno++] ); + int y = atoi( argv[argno++] ); + int z = atoi( argv[argno++] ); + + if( debug ) + { + printf( "debug: x=%d, y=%d, z=%d\n", x, y, z ); + } + + int nel; + int *list = parse_int_args( argc, argv, argno, &nel ); + + if( nel < 3 ) + { + fprintf( stderr, "good-triples: need a list of > 2 elements\n" ); + exit(1); + } + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + tripledynarray t; + init_tripledynarray( &t ); + + for( int i=0; i x ) continue; + + for( int k=j+1; k 0 ) + { + printf( "Good triples are below:\n" ); + for( int n=0; n +#include +#include +#include +#include +#include + +#include "args.h" +#include "printarray.h" +#include "parseints.h" + +typedef struct +{ + int nel; // current number of elements + int maxel; // maximum number of elements allocated + int *list; // malloc()d list of integers +} intlist; + + +// +// intlist il.. then initialize il.. then: +// add_one( element, &il ); +// +static void add_one( int x, intlist *p ) +{ + if( p->nel > p->maxel ) + { + p->maxel += 128; + p->list = realloc( p->list, p->maxel ); + assert( p->list ); + } + #if 0 + if( debug ) + { + printf( "PIA: appending %d to result at " + "pos %d\n", x, p->nel ); + } + #endif + p->list[p->nel++] = x; +} + + +// +// intlist il.. then initialize il.. then: +// add_one_arg( argstr, &il ); +// +static void add_one_arg( char *argstr, intlist *p ) +{ + int x; + if( !check_unsigned_int(argstr,&x) ) + { + fprintf( stderr, "PIA: arg %s must be +int\n", argstr ); + exit(1); + } + add_one( x, p ); +} + + +// +// int nel; +// int *ilist = parse_int_args( argc, argv, argno, &nel ); +// process all arguments argv[argno..argc-1], extracting either +// single ints or comma-separated lists of ints from those arguments, +// accumulate all integers in a dynarray list, storing the total number +// of elements in nel. This list must be freed by the caller. +// Note that the list of elements used to be terminated by a -1 value, +// but I've commented this out from now on. +// +int *parse_int_args( int argc, char **argv, int argno, int *nel ) +{ + int *result = malloc( 128 * sizeof(int) ); + assert( result ); + intlist il = { 0, 128, result }; + + #if 0 + if( debug ) + { + printf( "PIA: parsing ints from args %d..%d\n", argno, argc-1 ); + } + #endif + for( int i=argno; i +#include + + +// print_int_array( maxw, nelements, results[], sep, outfile ); +// format results[0..nelements-1] as a separated +// list onto outfile with lines <= maxw chars long. +// produces a whole number of lines of output - without the trailing '\n' +void print_int_array( int maxw, int nel, int *results, char sep, FILE *out ) +{ + int linelen = 0; + for( int i=0; i maxw ) + { + fputc( '\n', out ); + linelen = 0; + } else if( i>0 ) + { + fputc( ' ', out ); + linelen++; + } + + linelen += len; + fprintf( out, "%s", buf ); + if( i0 ) + //{ + // fputc( '\n', out ); + //} +} diff --git a/challenge-199/duncan-c-white/C/printarray.h b/challenge-199/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-199/duncan-c-white/C/printarray.h @@ -0,0 +1 @@ +extern void print_int_array( int maxw, int nel, int * results, char sep, FILE * out ); diff --git a/challenge-199/duncan-c-white/README b/challenge-199/duncan-c-white/README index dffa7605cf..11114219fc 100644 --- a/challenge-199/duncan-c-white/README +++ b/challenge-199/duncan-c-white/README @@ -1,58 +1,78 @@ -Task 1: Max Gap +Task 1: Good Pairs You are given a list of integers, @list. -Write a script to find the total pairs in the sorted list where 2 -consecutive elements has the max gap. If the list contains less then 2 -elements then return 0. +Write a script to find the total count of Good Pairs: A pair (i, j) is +called good if list[i] == list[j] and i < j. + Example 1 -Input: @list = (2,5,8,1) -Output: 2 +Input: @list = (1,2,3,1,1,3) +Output: 4 -Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8) +There are 4 good pairs found as below: +(0,3) +(0,4) +(3,4) +(2,5) Example 2 -Input: @list = (3) +Input: @list = (1,2,3) Output: 0 +Example 3 + +Input: @list = (1,1,1,1) +Output: 6 -MY NOTES: very easy. sort, then sequence through the sorted list, -finding the max gap so far and all pairs with that gap. +Good pairs are below: +(0,1) +(0,2) +(0,3) +(1,2) +(1,3) +(2,3) + +MY NOTES: very easy. two nested for loops.. GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C (look in the C directory for the translation) -Task 2: Prime Count +Task 2: Good Triplets -You are given an integer $n > 0. +You are given an array of integers, @array and three integers $x,$y,$z. -Write a script to print the count of primes less than $n. -Example 1 +Write a script to find out total Good Triplets in the given array. -Input: $n = 10 -Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7. +A triplet array[i], array[j], array[k] is good if it satisfies the following conditions: -Example 2 +a) 0 <= i < j < k <= n (size of given array) +b) abs(array[i] - array[j]) <= x +c) abs(array[j] - array[k]) <= y +d) abs(array[i] - array[k]) <= z -Input: $n = 15 -Output: 6 +Example 1 -Example 3 +Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +Output: 4 + +Good Triplets are as below: +(3,0,1) where (i=0, j=1, k=2) +(3,0,1) where (i=0, j=1, k=3) +(3,1,1) where (i=0, j=2, k=3) +(0,1,1) where (i=1, j=2, k=3) + +Example 2 -Input: $n = 1 +Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 Output: 0 -Example 4 -Input: $n = 25 -Output: 9 -MY NOTES: very easy, specially if you have a prime finding module lying -around:-) +MY NOTES: not quite so easy, start with 3 nested for loops;-) GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C (look in the C directory for the translation) diff --git a/challenge-199/duncan-c-white/perl/ch-1.pl b/challenge-199/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..a088c95e6b --- /dev/null +++ b/challenge-199/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +# +# Task 1: Good Pairs +# +# You are given a list of integers, @list. +# +# Write a script to find the total count of Good Pairs: A pair (i, j) is +# called good if list[i] == list[j] and i < j. +# +# +# Example 1 +# +# Input: @list = (1,2,3,1,1,3) +# Output: 4 +# +# There are 4 good pairs found as below: +# (0,3) +# (0,4) +# (3,4) +# (2,5) +# +# Example 2 +# +# Input: @list = (1,2,3) +# Output: 0 +# +# Example 3 +# +# Input: @list = (1,1,1,1) +# Output: 6 +# +# Good pairs are below: +# (0,1) +# (0,2) +# (0,3) +# (1,2) +# (1,3) +# (2,3) +# +# MY NOTES: very easy. two nested for loops.. +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl +# into C (look in the C directory for the translation) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Data::Dumper; + +my $debug=0; +die "Usage: good-pairs [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my @list = split( /,/, join(',',@ARGV) ); + +die "good-pairs: need at least 2 ints in list\n" unless @list>1; + +my @result; + +for( my $i=0; $i<@list; $i++ ) +{ + for( my $j=$i+1; $j<@list; $j++ ) + { + push @result, [$i,$j] if $list[$i] == $list[$j]; + } +} + +say scalar(@result); + +say "Good pairs are below:"; +foreach my $p (@result) +{ + my( $i, $j ) = @$p; + say "($i,$j)"; +} diff --git a/challenge-199/duncan-c-white/perl/ch-2.pl b/challenge-199/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..a189381668 --- /dev/null +++ b/challenge-199/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,80 @@ +#!/usr/bin/perl +# +# Task 2: Good Triplets +# +# You are given an array of integers, @array and three integers $x,$y,$z. +# +# Write a script to find out total Good Triplets in the given array. +# +# A triplet array[i], array[j], array[k] is good if it satisfies the following conditions: +# +# a) 0 <= i < j < k <= n (size of given array) +# b) abs(array[i] - array[j]) <= x +# c) abs(array[j] - array[k]) <= y +# d) abs(array[i] - array[k]) <= z +# +# Example 1 +# +# Input: @array = (3,0,1,1,9,7) and $x = 7, $y = 2, $z = 3 +# Output: 4 +# +# Good Triplets are as below: +# (3,0,1) where (i=0, j=1, k=2) +# (3,0,1) where (i=0, j=1, k=3) +# (3,1,1) where (i=0, j=2, k=3) +# (0,1,1) where (i=1, j=2, k=3) +# +# Example 2 +# +# Input: @array = (1,1,2,2,3) and $x = 0, $y = 0, $z = 1 +# Output: 0 +# +# MY NOTES: not quite so easy, start with 3 nested for loops;-) +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl +# into C (look in the C directory for the translation) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Function::Parameters; +use Data::Dumper; + +my $debug=0; +die "Usage: good-triplets [--debug] X,Y.and Z intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV > 4; +my( $x, $y, $z, @list ) = @ARGV; + +@list = split( /,/, join(',',@list) ); + +die "good-triples: need at least 3 ints in list\n" unless @list>2; + +my @result; + +for( my $i=0; $i<@list; $i++ ) +{ + for( my $j=$i+1; $j<@list; $j++ ) + { + next if abs($list[$i] - $list[$j]) > $x; + for( my $k=$j+1; $k<@list; $k++ ) + { + push @result, [$list[$i],$list[$j],$list[$k] ] if + abs($list[$j] - $list[$k]) <= $y && + abs($list[$i] - $list[$k]) <= $z; + } + } +} + +say scalar(@result); + +if( @result ) +{ + say "Good triples are below:"; + foreach my $p (@result) + { + my( $i, $j, $k ) = @$p; + say "($i,$j,$k)"; + } +} -- cgit From 51b02ce75ef1021204f6dd192bc027a042c65ee9 Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 16 Jan 2023 16:19:17 -0500 Subject: Week 200 --- challenge-200/zapwai/README | 2 +- challenge-200/zapwai/blog.txt | 1 + challenge-200/zapwai/perl/ch-1.pl | 66 +++++++++++++++++++++++++++++++ challenge-200/zapwai/perl/ch-2.pl | 79 +++++++++++++++++++++++++++++++++++++ challenge-200/zapwai/raku/ch-1.raku | 64 ++++++++++++++++++++++++++++++ challenge-200/zapwai/raku/ch-2.raku | 75 +++++++++++++++++++++++++++++++++++ 6 files changed, 286 insertions(+), 1 deletion(-) create mode 100644 challenge-200/zapwai/blog.txt create mode 100644 challenge-200/zapwai/perl/ch-1.pl create mode 100644 challenge-200/zapwai/perl/ch-2.pl create mode 100644 challenge-200/zapwai/raku/ch-1.raku create mode 100644 challenge-200/zapwai/raku/ch-2.raku diff --git a/challenge-200/zapwai/README b/challenge-200/zapwai/README index 707f6658ea..037b3777ef 100644 --- a/challenge-200/zapwai/README +++ b/challenge-200/zapwai/README @@ -1 +1 @@ -Solution by David Ferrone. +Solutions by David Ferrone. diff --git a/challenge-200/zapwai/blog.txt b/challenge-200/zapwai/blog.txt new file mode 100644 index 0000000000..7227da1633 --- /dev/null +++ b/challenge-200/zapwai/blog.txt @@ -0,0 +1 @@ +https://dev.to/zapwai/weekly-challenge-200-1o38 diff --git a/challenge-200/zapwai/perl/ch-1.pl b/challenge-200/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..be93ae0867 --- /dev/null +++ b/challenge-200/zapwai/perl/ch-1.pl @@ -0,0 +1,66 @@ +use v5.30.0; +my @array = (1,2,3,4); +#my @array = (1,2,3,5,7,9,10,11,14); +#my @array = (1,2,3,5,7,9,10,11,14,5,32,33,40,43,46,49); + +sub wrap { + my @array = @_; + "(" . join(",", @array).")" +} + +# return all sets of size 3+ +sub bundle { + my $len = @_; + my $str; + unless (@_ < 4) { + do { + $len--; + for (0 .. @_ - $len) { + my @list; + for my $i ($_ .. $_ + $len - 1) { + push @list, $_[$i]; + } + $str .= wrap(@list).", "; + } + } while ($len > 3); + } + $str .= wrap(@_); + print $str; +} + +say "Input: \@array = " . wrap(@array); +if ($#array < 2) { + say "Output: No slice found."; exit; +} + +# indices as we traverse list +my $begin = 0; +my $end = 0; + +my @begin; +my @end; + +$begin[0] = 0 if ($array[2] - $array[1] == $array[1] - $array[0]); +for my $i (0 .. $#array - 2) { + next if ($i < $end); + $begin = $i; + my $diff = $array[$i + 1] - $array[$i]; + for my $j ($i + 1 .. $#array - 1) { + if ($array[$j + 1] - $array[$j] == $diff) { + $end = $j + 1; + push @begin, $begin unless ($begin[$#begin] == $begin); + } else { + last if (($end == 0) or ($end[$#end] == $end)); + push @end, $end; + last; + } + } +} +push @end, $end if (@begin != @end); +print "Output: "; +for my $i (0 .. $#begin - 1) { + bundle @array[$begin[$i] .. $end[$i]]; + print ", "; +} +bundle @array[$begin[$#begin] .. $end[$#begin]]; +say; diff --git a/challenge-200/zapwai/perl/ch-2.pl b/challenge-200/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..1d9a7af7e0 --- /dev/null +++ b/challenge-200/zapwai/perl/ch-2.pl @@ -0,0 +1,79 @@ +use v5.30.0; +my @truth = qw( 1110111 0010010 1011101 1011011 0111010 1101011 0101111 1010010 1111111 1111010 ); +my $N = $ARGV[0] || 200; + +my $vert = 2; +my $horiz = 2*$vert + 1; + +sub hor { + my ($newline_flag, $i, @a) = @_; + if ($a[$i]) { + print " "; + print "-" x $horiz; + ($newline_flag) ? { say " " } : { print " " }; + } else { + ($newline_flag) ? { say " " x $horiz } : { print " " x ($horiz + 2) } + } +} + +sub ver { + my ($newline_flag, $i, @a) = @_; + + if ($a[$i]) { + print "|"; + } else { + print " "; + } + print " " x $horiz; + if ($a[$i + 1]) { + ($newline_flag) ? { say "|" } : { print "|" }; + } else { + ($newline_flag) ? { say " " } : { print " " }; + } +} + +sub d { + my @num = @_; + my $last = pop @num; + foreach (@num) { + my @a = split("", $truth[$_]); + hor(0,0, @a); + } + my @b = split("", $truth[$last]); + hor(1,0, @b); + + my $N = $vert; + do { + $N--; + foreach (@num) { + my @a = split("", $truth[$_]); + ver(0,1, @a); + } + ver(1,1,@b); + } while ($N > 0); + + foreach (@num) { + my @a = split("", $truth[$_]); + hor(0,3, @a); + } + hor(1,3, @b); + + $N = $vert; + do { + $N--; + foreach (@num) { + my @a = split("", $truth[$_]); + ver(0,4, @a); + } + ver(1,4, @b); + } while ($N > 0); + + foreach (@num) { + my @a = split("", $truth[$_]); + hor(0,6, @a); + } + hor(1,6, @b); +} + +my @nums = split("",$N); +d(@nums); diff --git a/challenge-200/zapwai/raku/ch-1.raku b/challenge-200/zapwai/raku/ch-1.raku new file mode 100644 index 0000000000..9a3fb33a04 --- /dev/null +++ b/challenge-200/zapwai/raku/ch-1.raku @@ -0,0 +1,64 @@ +my @array = (1,2,3,4); +#my @array = (1,2,3,5,7,9,10,11,14,5,32,33,40,43,46,49); +sub wrap (@arr) { + return "(" ~ join(",", @arr) ~ ")" +} + +sub bundle (@arr) { + my $len = @arr.elems; + my $str; + unless ( @arr.elems < 4 ) { + repeat { + $len--; + for (0 .. @arr.elems - $len) { + my @list; + loop (my $i = $_; $i <= $_ + $len - 1; $i++) { + push @list, @arr[$i]; + } + $str ~= wrap(@list) ~ ", "; + } + } while ($len > 3); + } + $str ~= wrap(@arr); + print $str; +} + +say "Input: \@array = " ~ wrap(@array); +if (@array.elems < 3) { + say "Output: No slice found."; exit; +} + +my $begin = 0; +my $end = 0; + +my @begin = (-1); +my @end = (-1); + +@begin[1] = 0 if (@array[2] - @array[1] == @array[1] - @array[0]); +loop (my $i = 0; $i < @array.elems - 2; $i++) { + next if ($i < $end); + $begin = $i; + my $diff = @array[$i + 1] - @array[$i]; + loop (my $j = $i + 1; $j < @array.elems - 1; $j++) { + if (@array[$j + 1] - @array[$j] == $diff) { + $end = $j + 1; + push @begin, $begin unless (@begin.tail == $begin); + } else { + last if (($end == 0) or (@end.tail == $end)); + push @end, $end; + last; + } + } +} +push @end, $end if (@begin.elems != @end.elems); +print "Output: "; + +shift @begin; +shift @end; + +loop (my $k = 0; $k < @begin.elems - 1; $k++) { + bundle @array[@begin[$k] .. @end[$k]]; + print ", "; +} +bundle @array[@begin.tail .. @end.tail]; +say ""; diff --git a/challenge-200/zapwai/raku/ch-2.raku b/challenge-200/zapwai/raku/ch-2.raku new file mode 100644 index 0000000000..99ba6be888 --- /dev/null +++ b/challenge-200/zapwai/raku/ch-2.raku @@ -0,0 +1,75 @@ +my @truth = qw/ 1110111 0010010 1011101 1011011 0111010 1101011 0101111 1010010 1111111 1111010 /; +my $N = @*ARGS[0] || 200; +my $vert = 2; #size of numbers. +my $horiz = 2*$vert + 1; + +sub hor ($newline_flag, $i, @a) { + if @a[$i] == 1 { + print " "; + print "-" x $horiz; + if $newline_flag { say " " } + else { print " " } + } else { + if $newline_flag { say " " x $horiz } + else { print " " x ($horiz + 2) } + } +} + +sub ver ($newline_flag, $i, @a) { + if @a[$i] == 1 { + print("|"); + } else { + print " "; + } + print " " x $horiz; + if @a[$i + 1] == 1 { + if $newline_flag { say "|" } else { print "|" }; + } else { + if $newline_flag { say " " } else { print " " }; + } +} + +sub d (@num) { + my $last = pop @num; + for (@num) { + my @a = split("", @truth[$_], :skip-empty); + hor(0,0, @a); + } + my @b = split("", @truth[$last], :skip-empty); + hor(1,0,@b); + + my $n = $vert; + repeat { + $n--; + for (@num) { + my @a = split("", @truth[$_], :skip-empty); + ver(0,1, @a); + } + ver(1,1,@b); + } while ($n > 0); + + for @num { + my @a = split("", @truth[$_], :skip-empty); + hor(0,3, @a); + } + hor(1,3, @b); + + $n = $vert; + + repeat { + $n--; + for @num { + my @a = split("", @truth[$_], :skip-empty); + ver(0,4, @a); + } + ver(1,4, @b); + } while ($n > 0); + + for @num { + my @a = split("", @truth[$_], :skip-empty); + hor(0,6, @a); + } + hor(1,6, @b); +} +my @nums = split("",$N, :skip-empty); +d(@nums); -- cgit From a83cbf2d261660817300fdfe3e77b02c743b2678 Mon Sep 17 00:00:00 2001 From: Dave Jacoby Date: Mon, 16 Jan 2023 16:44:29 -0500 Subject: #200 DAJ --- challenge-200/dave-jacoby/blog.txt | 1 + challenge-200/dave-jacoby/perl/ch-1.pl | 49 ++++++++++++++++++++++++++++++++++ challenge-200/dave-jacoby/perl/ch-2.pl | 42 +++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 challenge-200/dave-jacoby/blog.txt create mode 100644 challenge-200/dave-jacoby/perl/ch-1.pl create mode 100644 challenge-200/dave-jacoby/perl/ch-2.pl diff --git a/challenge-200/dave-jacoby/blog.txt b/challenge-200/dave-jacoby/blog.txt new file mode 100644 index 0000000000..a8e037aefa --- /dev/null +++ b/challenge-200/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/01/16/bicentweekly-solution-weekly-challenge-200.html diff --git a/challenge-200/dave-jacoby/perl/ch-1.pl b/challenge-200/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..6d3d9c53b5 --- /dev/null +++ b/challenge-200/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + [ 1, 2, 3, 4 ], + [ 2, 4, 6, 8, 9, 10, 11 ], + [2], + +); + +for my $e (@examples) { + my @out = arithmatic_slices( $e->@* ); + my $out = join ', ', map { "($_)" } map { join ',', $_->@* } @out; + my $in = join ',', $e->@*; + say <<"END"; + Input: \@array = ($in) + Output: ($out) +END +} + +sub arithmatic_slices ( @array ) { + return () if scalar @array < 3; + my @output; + my $max = -1 + scalar @array; +OUTER: for my $i ( 0 .. $max - 1) { + my $diff = abs( $array[$i] - $array[ $i + 1 ] ); + my @slice; + push @slice, $array[$i]; + for my $j ( $i + 1 .. $max ) { + my $ldiff = abs( $array[$j] - $array[ $j - 1 ] ); + if ( $ldiff == $diff ) { + push @slice, $array[$j]; + my @copy = @slice; + push @output, \@copy if scalar @slice > 2; + } + else { + next OUTER; + } + } + } + # first sort makes the arrays numerically sorted by first value + # second sort makes the arrays sorted by length + @output = sort { scalar $a->@* <=> scalar $b->@* } + sort { $a->[0] <=> $b->[0] } @output; + return @output; +} diff --git a/challenge-200/dave-jacoby/perl/ch-2.pl b/challenge-200/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..fb6d690fb1 --- /dev/null +++ b/challenge-200/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; +use Algorithm::Permute; + +my @examples = ( 1, 27, 190 .. 200 ); +@examples = @ARGV if scalar @ARGV; +my @truth = qw; +my @base = map { chomp $_; $_ } ; + +for my $e (@examples) { + seven_segment($e); +} + +sub seven_segment( $num ) { + my @digits = split //, $num; + my @segs = 'a' .. 'g'; + my @out; + for my $digit (@digits) { + my %segs = map { $_ => 1 } split //, $truth[$digit]; + for my $s ( 0 .. 6 ) { + my $line = $base[$s]; + for my $seg (@segs) { + if ( $segs{$seg} ) { $line =~ s/$seg/*/g } + else { $line =~ s/$seg/ /g } + } + push $out[$s]->@*, $line; + } + } + say join "\n", '',map { join '', $_->@* } @out; +} + +__DATA__ + aaaaa +f b +f b + ggggg +e c +e c + ddddd -- cgit From ce40e42581a7cf3c6c366c7311ac32665f4af121 Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Tue, 17 Jan 2023 00:15:38 +0000 Subject: Solutions for challenge #200 --- challenge-200/roger-bell-west/javascript/ch-1.js | 67 ++++++++++++ challenge-200/roger-bell-west/javascript/ch-2.js | 68 ++++++++++++ challenge-200/roger-bell-west/kotlin/ch-1.kt | 40 +++++++ challenge-200/roger-bell-west/kotlin/ch-2.kt | 66 ++++++++++++ challenge-200/roger-bell-west/lua/ch-1.lua | 59 ++++++++++ challenge-200/roger-bell-west/lua/ch-2.lua | 121 +++++++++++++++++++++ challenge-200/roger-bell-west/perl/ch-1.pl | 34 ++++++ challenge-200/roger-bell-west/perl/ch-2.pl | 69 ++++++++++++ challenge-200/roger-bell-west/postscript/ch-1.ps | 132 +++++++++++++++++++++++ challenge-200/roger-bell-west/postscript/ch-2.ps | 113 +++++++++++++++++++ challenge-200/roger-bell-west/python/ch-1.py | 29 +++++ challenge-200/roger-bell-west/python/ch-2.py | 45 ++++++++ challenge-200/roger-bell-west/raku/ch-1.p6 | 32 ++++++ challenge-200/roger-bell-west/raku/ch-2.p6 | 62 +++++++++++ challenge-200/roger-bell-west/ruby/ch-1.rb | 39 +++++++ challenge-200/roger-bell-west/ruby/ch-2.rb | 65 +++++++++++ challenge-200/roger-bell-west/rust/ch-1.rs | 40 +++++++ challenge-200/roger-bell-west/rust/ch-2.rs | 65 +++++++++++ challenge-200/roger-bell-west/tests.yaml | 23 ++++ 19 files changed, 1169 insertions(+) create mode 100755 challenge-200/roger-bell-west/javascript/ch-1.js create mode 100755 challenge-200/roger-bell-west/javascript/ch-2.js create mode 100644 challenge-200/roger-bell-west/kotlin/ch-1.kt create mode 100644 challenge-200/roger-bell-west/kotlin/ch-2.kt create mode 100755 challenge-200/roger-bell-west/lua/ch-1.lua create mode 100755 challenge-200/roger-bell-west/lua/ch-2.lua create mode 100755 challenge-200/roger-bell-west/perl/ch-1.pl create mode 100755 challenge-200/roger-bell-west/perl/ch-2.pl create mode 100644 challenge-200/roger-bell-west/postscript/ch-1.ps create mode 100644 challenge-200/roger-bell-west/postscript/ch-2.ps create mode 100755 challenge-200/roger-bell-west/python/ch-1.py create mode 100755 challenge-200/roger-bell-west/python/ch-2.py create mode 100755 challenge-200/roger-bell-west/raku/ch-1.p6 create mode 100755 challenge-200/roger-bell-west/raku/ch-2.p6 create mode 100755 challenge-200/roger-bell-west/ruby/ch-1.rb create mode 100755 challenge-200/roger-bell-west/ruby/ch-2.rb create mode 100755 challenge-200/roger-bell-west/rust/ch-1.rs create mode 100755 challenge-200/roger-bell-west/rust/ch-2.rs create mode 100644 challenge-200/roger-bell-west/tests.yaml diff --git a/challenge-200/roger-bell-west/javascript/ch-1.js b/challenge-200/roger-bell-west/javascript/ch-1.js new file mode 100755 index 0000000000..b28e2fc2c6 --- /dev/null +++ b/challenge-200/roger-bell-west/javascript/ch-1.js @@ -0,0 +1,67 @@ +#! /usr/bin/node + +"use strict" + +// by Frank Tan +// https://stackoverflow.com/questions/38400594/javascript-deep-comparison +function deepEqual(a,b) +{ + if( (typeof a == 'object' && a != null) && + (typeof b == 'object' && b != null) ) + { + var count = [0,0]; + for( var key in a) count[0]++; + for( var key in b) count[1]++; + if( count[0]-count[1] != 0) {return false;} + for( var key in a) + { + if(!(key in b) || !deepEqual(a[key],b[key])) {return false;} + } + for( var key in b) + { + if(!(key in a) || !deepEqual(b[key],a[key])) {return false;} + } + return true; + } + else + { + return a === b; + } +} + +function arithmeticslices(l) { + let o = []; + if (l.length >= 3) { + for (let a = 0; a <= l.length - 3; a++) { + let valid = false; + for (let b = a + 2; b <= l.length - 1; b++) { + let v = l.slice(a, b + 1); + if (!valid) { + for (let i = 0; i <= v.length - 3; i++) { + if (v[i + 1] - v[i] == v[i + 2] - v[i + 1]) { + valid = true; + break; + } + } + } + if (valid) { + o.push(v); + } + } + } + } + return o; +} + +if (deepEqual(arithmeticslices([1, 2, 3, 4]), [[1, 2, 3], [1, 2, 3, 4], [2, 3, 4]])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write(" "); +if (deepEqual(arithmeticslices([2]), [])) { + process.stdout.write("Pass"); +} else { + process.stdout.write("FAIL"); +} +process.stdout.write("\n"); diff --git a/challenge-200/roger-bell-west/javascript/ch-2.js b/challenge-200/roger-bell-west/javascript/ch-2.js new file mode 100755 index 0000000000..d5b305f702 --- /dev/null +++ b/challenge-200/roger-bell-west/javascript/ch-2.js @@ -0,0 +1,68 @@ +#! /usr/bin/node + +"use strict" + +function sevensegment(l) { + let disp = [0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x67]; + let v = []; + if (l == 0) { + v.push(0); + } else { + let ll = l; + while (ll > 0) { + v.push(ll % 10); + ll = parseInt(ll / 10); + } + v = v.reverse(); + } + let d = []; + for (let r = 1; r <= 7; r++) { + d.push(new Array(6*v.length).fill(" ")); + } + v.forEach((vv, i) => { + let x = 6 * i; + let da = disp[vv]; + if ((da & 1) > 0) { + for (let xa = x + 1; xa <= x + 3; xa++) { + d[0][xa] = '#'; + } + } + if ((da & 2) > 0) { + for (let y = 1; y <= 2; y++) { + d[y][x+4] = '#'; + } + } + if ((da & 4) > 0) { + for (let y = 4; y <= 5; y++) { + d[y][x+4] = '#'; + } + } + if ((da & 8) > 0) { + for (let xa = x + 1; xa <= x + 3; xa++) { + d[6][xa] = '#'; + } + } + if ((da & 16) > 0) { + for (let y = 4; y <= 5; y++) { + d[y][x] = '#'; + } + } + if ((da & 32) > 0) { + for (let y = 1; y <= 2; y++) { + d[y][x] = '#'; + } + } + if ((da & 64) > 0) { + for (let xa = x + 1; xa <= x + 3; xa++) { + d[3][xa] = '#'; + } + } + }); + for (let r of d) { + process.stdout.write(r.join("")); + process.stdout.write("\n"); + } +} + +sevensegment(1234567890); +sevensegment(200); diff --git a/challenge-200/roger-bell-west/kotlin/ch-1.kt b/challenge-200/roger-bell-west/kotlin/ch-1.kt new file mode 100644 index 0000000000..af739de736 --- /dev/null +++ b/challenge-200/roger-bell-west/kotlin/ch-1.kt @@ -0,0 +1,40 @@ +fun arithmeticslices(l: List): List> { + var o = ArrayList>() + if (l.size >= 3) { + for (a in 0..l.size-3) { + var valid = false + for (b in a+2..l.size-1) { + val v = l.slice(a .. b).toList() + if (!va