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 (limited to 'challenge-199') 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 45bfa9e1ca29c855329c43f51220c2ad991dbd24 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 18 Jan 2023 17:35:35 +0100 Subject: Add solution to James Smith's benchmarks for challenge 199/2 --- challenge-199/jo-37/perl/ch-2.pl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'challenge-199') diff --git a/challenge-199/jo-37/perl/ch-2.pl b/challenge-199/jo-37/perl/ch-2.pl index 292be9b25e..ef5a11b322 100755 --- a/challenge-199/jo-37/perl/ch-2.pl +++ b/challenge-199/jo-37/perl/ch-2.pl @@ -83,3 +83,19 @@ sub run_tests { done_testing; exit; } + +__DATA__ + +Out of curiosity, rerun James Smith's benchmarks with this +implementation added. See +https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-199/james-smith/perl/ch-2.pl + + Rate naive opt range_1 copy_1 copy_2 range_2 fastest pdl +naive 21.5/s -- -49% -52% -53% -56% -64% -84% -90% +opt 41.9/s 95% -- -7% -8% -13% -29% -70% -81% +range_1 45.1/s 110% 8% -- -1% -7% -24% -67% -79% +copy_1 45.8/s 113% 9% 1% -- -5% -22% -67% -79% +copy_2 48.4/s 125% 15% 7% 6% -- -18% -65% -78% +range_2 59.0/s 174% 41% 31% 29% 22% -- -57% -73% +fastest 138/s 543% 230% 207% 202% 186% 134% -- -36% +pdl 218/s 912% 419% 382% 375% 350% 269% 57% -- -- cgit From aa81ae0cb1826eb5df5c281096efb7eb4f2f773a Mon Sep 17 00:00:00 2001 From: Luis Mochan Date: Fri, 20 Jan 2023 19:04:02 -0600 Subject: Correct error in PWC199T1 --- challenge-199/wlmb/perl/ch-1.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-199') diff --git a/challenge-199/wlmb/perl/ch-1.pl b/challenge-199/wlmb/perl/ch-1.pl index 6e6ed7ae55..7373d97249 100755 --- a/challenge-199/wlmb/perl/ch-1.pl +++ b/challenge-199/wlmb/perl/ch-1.pl @@ -9,4 +9,4 @@ say(<<~"FIN"), exit unless @ARGV >= 2; Usage: $0 N1 N2 [N3...] to find all good pairs from the set N1 N2... FIN -say join " ", @ARGV, "->", 0+grep{$ARGV[$_->[0]]==$ARGV[$_->[1]]} combinations(\@ARGV, 2) +say join " ", @ARGV, "->", 0+grep{$_->[0]==$_->[1]} combinations(\@ARGV, 2) -- cgit From dcea4c7ce34c7edc7d0940172e8ffa798f5a509e Mon Sep 17 00:00:00 2001 From: James Smith Date: Sun, 22 Jan 2023 18:42:04 +0000 Subject: Update README.md --- challenge-199/james-smith/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'challenge-199') diff --git a/challenge-199/james-smith/README.md b/challenge-199/james-smith/README.md index 77c550d2b6..b4670b096a 100644 --- a/challenge-199/james-smith/README.md +++ b/challenge-199/james-smith/README.md @@ -1,7 +1,7 @@ [< Previous 198](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-198/james-smith) | [Next 200 >](https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-200/james-smith) -# The Weekly Challenge 198 +# The Weekly Challenge 199 You can find more information about this weeks, and previous weeks challenges at: -- cgit From 85f41d815fb2516f8fa1534cf60c520ab3243f5e Mon Sep 17 00:00:00 2001 From: dcw Date: Mon, 23 Jan 2023 01:13:12 +0000 Subject: belatedly incorporated my solutions to challenge 200, in Perl and C, and fixed a bug in both challenge 199 C solutions (off by one error in my dynamic arrays) --- challenge-199/duncan-c-white/C/ch-1.c | 4 ++-- challenge-199/duncan-c-white/C/ch-2.c | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'challenge-199') diff --git a/challenge-199/duncan-c-white/C/ch-1.c b/challenge-199/duncan-c-white/C/ch-1.c index 2f42f40f25..85c8ef309f 100644 --- a/challenge-199/duncan-c-white/C/ch-1.c +++ b/challenge-199/duncan-c-white/C/ch-1.c @@ -40,6 +40,8 @@ void init_pairdynarray( pairdynarray *p ) void add_pair( pairdynarray *p, int i, int j ) { + p->p[p->npairs].i = i; + p->p[p->npairs].j = j; p->npairs++; if( p->npairs == p->nalloc ) { @@ -47,8 +49,6 @@ void add_pair( pairdynarray *p, int i, int j ) 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; } diff --git a/challenge-199/duncan-c-white/C/ch-2.c b/challenge-199/duncan-c-white/C/ch-2.c index 97cb50b648..c55280b89d 100644 --- a/challenge-199/duncan-c-white/C/ch-2.c +++ b/challenge-199/duncan-c-white/C/ch-2.c @@ -40,6 +40,9 @@ void init_tripledynarray( tripledynarray *t ) void add_triple( tripledynarray *t, int i, int j, int k ) { + t->p[t->ntriples].i = i; + t->p[t->ntriples].j = j; + t->p[t->ntriples].k = k; t->ntriples++; if( t->ntriples == t->nalloc ) { @@ -47,9 +50,6 @@ void add_triple( tripledynarray *t, int i, int j, int k ) 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; } @@ -123,7 +123,7 @@ int main( int argc, char **argv ) printf( "Good triples are below:\n" ); for( int n=0; n Date: Mon, 23 Jan 2023 01:51:15 +0000 Subject: - Added solutions by Roger Bell_West. - Added solutions by Dave Jacoby. - Added solutions by David Ferrone. - Added solutions by Luca Ferrari. - Added solutions by Mark Anderson. - Added solutions by W. Luis Mochan. - Added solutions by Peter Campbell Smith. - Added solutions by Mariano Spadaccini. - Added solutions by Thomas Kohler. - Added solutions by Bob Lied. - Added solutions by Jorg Sommrey. - Added solutions by Flavio Poletti. - Added solutions by Pip Stuart. - Added solutions by E. Choroba. - Added solutions by Stephen G. Lynn. - Added solutions by Matthew Neleigh. - Added solutions by Robert Ransbottom. - Added solutions by Athanasius. - Added solutions by Simon Green. - Added solutions by Cheok-Yin Fung. - Added solutions by Tyler Wardhaugh. - Added solutions by Jan Krnavek. - Added solutions by Bruce Gray. - Added solutions by James Smith. - Added solutions by Robbie Hatley. - Added solutions by Solathian. - Added solutions by Arne Sommer. - Added solutions by Carlos Oliveira. - Added solutions by Marton Polgar. - Added solutions by Adam Russell. - Added solutions by Duncan C. White. - Added solutions by Lars Balker. - Added solutions by Colin Crain. - Added solutions by Laurent Rosenfeld. - Added solutions by Robert DiCicco. - Added solutions by Ulrich Rieke. --- challenge-199/eric-cheung/python/ch-1.py | 20 ++++++++++++++++++++ challenge-199/eric-cheung/python/ch-2.py | 25 +++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100755 challenge-199/eric-cheung/python/ch-1.py create mode 100755 challenge-199/eric-cheung/python/ch-2.py (limited to 'challenge-199') diff --git a/challenge-199/eric-cheung/python/ch-1.py b/challenge-199/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..5227a2c7b1 --- /dev/null +++ b/challenge-199/eric-cheung/python/ch-1.py @@ -0,0 +1,20 @@ + +from itertools import combinations + +def get_GoodPairs_List(arrInput): + + arrGoodPairsList = [] + + nIndxTuple = combinations(range(0, len(arrInput)), 2) + + for nIndxLoop_01, nIndxLoop_02 in list(nIndxTuple): + if arrInput[nIndxLoop_01] == arrInput[nIndxLoop_02]: + arrGoodPairsList.append([nIndxLoop_01, nIndxLoop_02]) + + return arrGoodPairsList + +## arrInputList = [1, 2, 3, 1, 1, 3] ## Example 1 +## arrInputList = [1, 2, 3] ## Example 2 +arrInputList = [1, 1, 1, 1] ## Example 3 + +print (len(get_GoodPairs_List(arrInputList))) diff --git a/challenge-199/eric-cheung/python/ch-2.py b/challenge-199/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..2ed1c89d29 --- /dev/null +++ b/challenge-199/eric-cheung/python/ch-2.py @@ -0,0 +1,25 @@ + +from itertools import combinations + +def get_GoodTriplets_List(arrInput, arrInputInt): + + arrGoodPairsList = [] + + nIndxTuple = combinations(range(0, len(arrInput)), 3) + + for nIndxLoop_01, nIndxLoop_02, nIndxLoop_03 in list(nIndxTuple): + if abs(arrInput[nIndxLoop_01] - arrInput[nIndxLoop_02]) <= arrInputInt[0] and abs(arrInput[nIndxLoop_02] - arrInput[nIndxLoop_03]) <= arrInputInt[1] and abs(arrInput[nIndxLoop_01] - arrInput[nIndxLoop_03]) <= arrInputInt[2]: + arrGoodPairsList.append([nIndxLoop_01, nIndxLoop_02, nIndxLoop_03]) + + return arrGoodPairsList + + +## Example 1 +## arrInputList = [3, 0, 1, 1, 9, 7] +## arrInputList_Int = [7, 2, 3] + +## Example 2 +arrInputList = [1, 1, 2, 2, 3] +arrInputList_Int = [0, 0, 1] + +print (len(get_GoodTriplets_List(arrInputList, arrInputList_Int))) -- cgit