aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-16 03:27:56 +0100
committerGitHub <noreply@github.com>2023-04-16 03:27:56 +0100
commit6da6b994c1d9be0dda157b5814ccaf3f7fe603a8 (patch)
treef7626ff718f90d6bacb9c25101042562ccb21696
parentde6efb458321048789b255c353a6123cf7058bc4 (diff)
parent63f06bef21dd2dc21224f67c9cda265a7e0402f0 (diff)
downloadperlweeklychallenge-club-6da6b994c1d9be0dda157b5814ccaf3f7fe603a8.tar.gz
perlweeklychallenge-club-6da6b994c1d9be0dda157b5814ccaf3f7fe603a8.tar.bz2
perlweeklychallenge-club-6da6b994c1d9be0dda157b5814ccaf3f7fe603a8.zip
Merge pull request #7899 from dcw803/master
belatedly did and committed my solutions to challenge 211
-rw-r--r--challenge-205/duncan-c-white/C/README5
-rw-r--r--challenge-210/duncan-c-white/README2
-rw-r--r--challenge-211/duncan-c-white/C/.cbuild1
-rw-r--r--challenge-211/duncan-c-white/C/Makefile18
-rw-r--r--challenge-211/duncan-c-white/C/README9
-rw-r--r--challenge-211/duncan-c-white/C/args.c234
-rw-r--r--challenge-211/duncan-c-white/C/args.h12
-rw-r--r--challenge-211/duncan-c-white/C/ch-1.c145
-rw-r--r--challenge-211/duncan-c-white/C/ch-2.c166
-rw-r--r--challenge-211/duncan-c-white/C/parseints.c114
-rw-r--r--challenge-211/duncan-c-white/C/parseints.h1
-rw-r--r--challenge-211/duncan-c-white/C/printarray.c39
-rw-r--r--challenge-211/duncan-c-white/C/printarray.h1
-rw-r--r--challenge-211/duncan-c-white/README87
-rwxr-xr-xchallenge-211/duncan-c-white/perl/ch-1.pl87
-rwxr-xr-xchallenge-211/duncan-c-white/perl/ch-2.pl137
16 files changed, 1001 insertions, 57 deletions
diff --git a/challenge-205/duncan-c-white/C/README b/challenge-205/duncan-c-white/C/README
index 79d015ffe4..1e1acd8915 100644
--- a/challenge-205/duncan-c-white/C/README
+++ b/challenge-205/duncan-c-white/C/README
@@ -3,11 +3,6 @@ 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.
-The C translation of ch-2.pl was quite tricky to write, mainly on storage
-allocation (as usual), although I found a neat (slightly cheaty?)
-way of doing it. specifically: I create a 1d-array of all the elements,
-and then I print it out as if it were an RxC matrix..
-
These C versions use most of my regular support modules:
- a command-line argument processing module args.[ch],
- a csvlist-of-int parsing module parseints.[ch], and
diff --git a/challenge-210/duncan-c-white/README b/challenge-210/duncan-c-white/README
index 6013657b55..8bb0290387 100644
--- a/challenge-210/duncan-c-white/README
+++ b/challenge-210/duncan-c-white/README
@@ -1,4 +1,4 @@
-Task 1: Task 1: Kill and Win
+Task 1: Kill and Win
You are given a list of integers. Write a script to get the maximum points.
You are allowed to take out (kill) any integer and remove from the list.
diff --git a/challenge-211/duncan-c-white/C/.cbuild b/challenge-211/duncan-c-white/C/.cbuild
index 835981f6f1..a14ec76520 100644
--- a/challenge-211/duncan-c-white/C/.cbuild
+++ b/challenge-211/duncan-c-white/C/.cbuild
@@ -1,5 +1,4 @@
BUILD = ch-1 ch-2
-BUILD = ch-1
CFLAGS = -Wall -g
#LDFLAGS = -lm
#CFLAGS = -g
diff --git a/challenge-211/duncan-c-white/C/Makefile b/challenge-211/duncan-c-white/C/Makefile
new file mode 100644
index 0000000000..1b34ccd3b2
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/Makefile
@@ -0,0 +1,18 @@
+# Makefile rules generated by CB
+CC = gcc
+CFLAGS = -Wall -g
+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-211/duncan-c-white/C/README b/challenge-211/duncan-c-white/C/README
new file mode 100644
index 0000000000..341becc1e1
--- /dev/null
+++ b/challenge-211/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 debugging)
+output to the Perl originals.
+
+These C versions use some of my regular support modules:
+- my command-line argument processing module args.[ch],
+- my csvlist-of-int parsing module parseints.[ch], and
+- my int-array printing module printarray.[ch].
diff --git a/challenge-211/duncan-c-white/C/args.c b/challenge-211/duncan-c-white/C/args.c
new file mode 100644
index 0000000000..20c21e6c30
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/args.c
@@ -0,0 +1,234 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+
+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<argc; i++ )
+ {
+ arr[nel++] = atoi( argv[i] );
+ }
+ arr[nel] = -1;
+ return nel;
+}
+
+
+//
+// bool isint = check_unsigned_int( char *val, int *n );
+// Given an string val, check that there's an unsigned integer
+// in it (after optional whitespace). If there is a valid
+// unsigned integer value, store that integer value in *n and
+// return true; otherwise return false (and don't alter *n).
+bool check_unsigned_int( char *val, int *n )
+{
+ // skip whitespace in val
+ char *p;
+ for( p=val; isspace(*p); p++ )
+ {
+ /*EMPTY*/
+ }
+ if( ! isdigit(*p) ) return false;
+ *n = atoi(p);
+ return true;
+}
+
+
+//
+// bool isint = check_int( char *val, int *n );
+// Given an string val, check that there's an integer
+// in it (after optional whitespace). If there is a valid
+// integer value, store that integer value in *n and
+// return true; otherwise return false (and don't alter *n).
+bool check_int( char *val, int *n )
+{
+ // skip whitespace in val
+ char *p;
+ for( p=val; isspace(*p); p++ )
+ {
+ /*EMPTY*/
+ }
+ int sign = 1;
+ if( *p == '+' ) p++;
+ else if( *p == '-' )
+ {
+ sign = -1;
+ p++;
+ }
+ if( ! isdigit(*p) ) return false;
+ *n = atoi(p) * sign;
+ return true;
+}
+
+
+//
+// bool ok = check_unsigned_real( char *val, double *n );
+// Given an string val, check that there's an unsigned real
+// in it (after optional whitespace). If there is a valid
+// unsigned real value, store that value in *n and
+// return true; otherwise return false (and don't alter *n).
+bool check_unsigned_real( char *val, double *n )
+{
+ // skip whitespace in val
+ char *p;
+ for( p=val; isspace(*p); p++ )
+ {
+ /*EMPTY*/
+ }
+ if( ! isdigit(*p) ) return false;
+ *n = atof(p);
+ return true;
+}
diff --git a/challenge-211/duncan-c-white/C/args.h b/challenge-211/duncan-c-white/C/args.h
new file mode 100644
index 0000000000..df765fa21e
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/args.h
@@ -0,0 +1,12 @@
+extern bool debug;
+
+extern void process_flag_noarg( char * name, int argc, char ** argv );
+extern int process_flag_n_args( char * name, int argc, char ** argv, int n, char * argmsg );
+extern int process_flag_n_m_args( char * name, int argc, char ** argv, int min, int max, char * argmsg );
+extern void process_onenumarg_default( char * name, int argc, char ** argv, int defvalue, int * n );
+extern void process_onenumarg( char * name, int argc, char ** argv, int * n );
+extern void process_twonumargs( char * name, int argc, char ** argv, int * m, int * n );
+extern int process_listnumargs( char * name, int argc, char ** argv, int * arr, int maxel );
+extern bool check_unsigned_int( char * val, int * n );
+extern bool check_int( char * val, int * n );
+extern bool check_unsigned_real( char * val, double * n );
diff --git a/challenge-211/duncan-c-white/C/ch-1.c b/challenge-211/duncan-c-white/C/ch-1.c
new file mode 100644
index 0000000000..75cc4e6611
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/ch-1.c
@@ -0,0 +1,145 @@
+// Task 1: Toeplitz Matrix
+//
+// C translation
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "args.h"
+#include "parseints.h"
+#include "printarray.h"
+
+
+// int nch = countch( target, str );
+// Count and return the number of occurrences of target in str.
+//
+int countch( char target, char *str )
+{
+ int n=0;
+ for( ; *str; str++ )
+ {
+ if( *str == target ) n++;
+ }
+ return n;
+}
+
+
+// init_row( rp, rowdata, ncols );
+// initialize the ncols values in the row (rp) from rowdata,
+// modifying rowdata slightly (replacing ','s with '\0's)
+//
+void init_row( int *rp, char *rowdata, int ncols )
+{
+ int n = 1 + countch(',', rowdata);
+ if( n != ncols )
+ {
+ fprintf( stderr, "All rows need %d columns - not %d\n",
+ ncols, n );
+ exit(1);
+ }
+
+ char *p, *q;
+ for( p = rowdata; (q = strchr(p,',')) != NULL; p=q+1 )
+ {
+ *q = '\0';
+ *rp++ = atoi(p);
+ }
+ *rp++ = atoi(p);
+}
+
+
+// show_matrix( m, nrows, ncols );
+// Display the given matrix.
+//
+void show_matrix( int **m, int nrows, int ncols )
+{
+ for( int i=0; i<nrows; i++ )
+ {
+ printf( "[ " );
+ for( int j=0; j<ncols; j++ )
+ {
+ if( j>0 ) putchar(',');
+ printf( "%d", m[i][j] );
+ }
+ printf( " ]\n" );
+ }
+}
+
+
+//
+// bool same = samediag(r,c,rows,cols,m);
+// Return true iff all matrix cells in m[][] down the
+// top-left->bottom-right diagonal starting at (r,c)
+// are the same value; false otherwise.
+//
+bool samediag( int r, int c, int rows, int cols, int **m )
+{
+ for( int v = m[r][c]; r<rows && c<cols; r++, c++ )
+ {
+ if( m[r][c] != v ) return false;
+ }
+ return true;
+}
+
+
+//
+// bool istp = toeplitz( rows, cols, m );
+// return true iff m[rows][cols] is a toeplitz matrix, false otherwise
+//
+bool toeplitz( int rows, int cols, int **m )
+{
+ for( int r=rows-2; r>=0; r-- )
+ {
+ if( ! samediag(r,0,rows,cols,m) ) return false;
+ }
+ for( int c=1; c<cols-1; c++ )
+ {
+ if( ! samediag(0,c,rows,cols,m) ) return false;
+ }
+ return true;
+}
+
+
+int main( int argc, char **argv )
+{
+ int argno = process_flag_n_m_args( "toeplitz-matrix", argc, argv,
+ 1, 1000, "list(row)" );
+
+ int nrows = argc-argno;
+ int ncols = 1 + countch(',', argv[argno] );
+ if( debug )
+ {
+ printf( "debug: nrows = %d, ncols = %d\n", nrows, ncols );
+ }
+
+ int **m = malloc( nrows * sizeof(int *) );
+ assert( m != NULL );
+ for( int i=0; i<nrows; i++ )
+ {
+ m[i] = malloc( ncols * sizeof(int) );
+ assert( m[i] != NULL );
+ init_row( m[i], argv[argno++], ncols );
+ }
+
+ if( debug )
+ {
+ printf( "debug: matrix:\n" );
+ show_matrix( m, nrows, ncols );
+ putchar( '\n' );
+ }
+
+ bool is = toeplitz( nrows, ncols, m );
+ printf( "%s\n", is ? "true" : "false" );
+
+ for( int i=0; i<nrows; i++ )
+ {
+ free( m[i] );
+ }
+ free( m );
+
+ return 0;
+}
diff --git a/challenge-211/duncan-c-white/C/ch-2.c b/challenge-211/duncan-c-white/C/ch-2.c
new file mode 100644
index 0000000000..858d9c14f3
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/ch-2.c
@@ -0,0 +1,166 @@
+//
+// Task 2: Split Same Average
+//
+// C version.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <assert.h>
+#include <math.h>
+
+#include "args.h"
+#include "parseints.h"
+#include "printarray.h"
+
+
+#define EPSILON 0.00001
+
+
+//
+// void subset_select( comb, nval, val[], &nsel, sel[], &nnotsel, notsel[] );
+// Consider comb as an nsel-bit binary unsigned number.
+// Select all the values from val[] whose corresponding bit in comb is 1,
+// store them into sel[nsel] (setting nsel), and store all the unselected
+// values into notsel[nnotsel], setting nnotsel.
+//
+void subset_select( int comb, int nval, int *val, int *nsel, int *sel, int *nnotsel, int *notsel )
+{
+ *nsel = *nnotsel = 0;
+ for( int i=0; i<nval; i++ )
+ {
+ int v = val[i];
+ if( comb%2 == 1 )
+ {
+ sel[(*nsel)++] = v;
+ } else
+ {
+ notsel[(*nnotsel)++] = v;
+ }
+ comb >>= 1;
+ }
+}
+
+
+//
+// bool found = count_val_avg( goalavg, nval, val[], &nsel, sel[], &nnotsel, notsel[] );
+// Attempt to find a subset of val[nval] whose average is goalavg.
+// If we find one, copy the number of subset elements into nsel,
+// and all subset elements into sel[nsel], and all other elements into
+// notsel[], setting notnsel to nval - nsel, and return true.
+// If we can't find one, return false.
+// Do it by iterating over all D-bit binary numbers from 1..2**nval-2
+// subset-summing each selected combination
+//
+bool count_val_avg( double goalavg, int nval, int *val, int *nsel, int *sel, int *nnotsel, int *notsel )
+{
+ // d = 2**nval - 2
+ int d = 1;
+ for( int i=0; i<nval; i++ ) d *= 2;
+ d -= 2;
+
+ if( debug )
+ {
+ printf( "debug: nval = %d, d = %d\n", nval, d );
+ }
+
+ for( int comb=1; comb<=d; comb++ )
+ {
+ *nsel = 0;
+ *nnotsel = 0;
+ subset_select( comb, nval, val, nsel, sel, nnotsel, notsel );
+ if( debug )
+ {
+ printf( "debug: selecting comb=%d, nsel=%d, "
+ "nnotsel=%d, selected = ",
+ comb, *nsel, *nnotsel );
+ print_int_array( 60, *nsel, sel, ',', stdout );
+ }
+
+ if( *nsel == 0 )
+ {
+ if( debug ) printf( " : rejected\n" );
+ continue;
+ }
+
+ int sum = 0;
+ for( int i=0; i<*nsel; i++ )
+ {
+ sum += sel[i];
+ }
+ double avg = ((double)sum) / *nsel;
+ if( debug )
+ {
+ //printf( ", not selected = " );
+ //print_int_array( 60, *nnotsel, notsel, ',', stdout );
+ printf( ", sum=%d, avg=%g\n", sum, avg );
+ }
+ if( fabs(avg-goalavg) < EPSILON )
+ {
+ #if 0
+ printf( "debug: returning true, fabs(a-ga)=%g\n",
+ fabs(avg-goalavg) );
+ #endif
+
+ return true;
+ }
+ }
+ return false;
+}
+
+
+int main( int argc, char **argv )
+{
+ int argno = process_flag_n_m_args( "split-sum-average", argc, argv,
+ 1, 1000, "intlist" );
+ int nel;
+ int *list = parse_int_args( argc, argv, argno, &nel );
+
+ if( debug )
+ {
+ printf( "debug: list: " );
+ print_int_array( 60, nel, list, ',', stdout );
+ putchar( '\n' );
+ }
+
+ int sel[nel];
+ int notsel[nel];
+ int nsel = 0;
+ int nnotsel = 0;
+
+ int sum = 0;
+ for( int i=0; i<nel; i++ )
+ {
+ sum += list[i];
+ }
+ double goalavg = ((double)sum)/nel;
+ if( debug )
+ {
+ printf( "debug: goal average is %g\n", goalavg );
+ }
+
+
+ bool found = count_val_avg( goalavg, nel, list,
+ &nsel, sel, &nnotsel, notsel );
+ if( found )
+ {
+ if( debug )
+ {
+ printf( "debug: found selected " );
+ print_int_array( 60, nsel, sel, ',', stdout );
+ printf( " and not selected " );
+ print_int_array( 60, nnotsel, notsel, ',', stdout );
+ putchar( '\n' );
+ }
+ printf( "true\n" );
+ } else
+ {
+ if( debug )
+ {
+ printf( "debug: no subsets found\n" );
+ }
+ printf( "false\n" );
+ }
+ return 0;
+}
diff --git a/challenge-211/duncan-c-white/C/parseints.c b/challenge-211/duncan-c-white/C/parseints.c
new file mode 100644
index 0000000000..3e820eb334
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/parseints.c
@@ -0,0 +1,114 @@
+// Simple routine to parse one or more arguments,
+// looking for individual ints or comma-separated
+// lists of ints.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#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 != NULL );
+ }
+ #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_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 != NULL );
+ 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<argc; i++ )
+ {
+ assert( strlen(argv[i]) < 1024 );
+ char copy[1024];
+ strcpy( copy, argv[i] );
+ char *com;
+ char *s;
+ for( s=copy; (com = strchr(s,',')) != NULL; s=com+1 )
+ {
+ *com = '\0';
+ add_one_arg( s, &il );
+ }
+ add_one_arg( s, &il );
+ }
+
+ //add_one( -1, &il );
+
+ #if 0
+ if( debug )
+ {
+ printf( "PIA: final list is " );
+ print_int_array( 80, il.nel, il.list, ',', stdout );
+ putchar( '\n' );
+ }
+ #endif
+
+ *nel = il.nel;
+ return il.list;
+}
diff --git a/challenge-211/duncan-c-white/C/parseints.h b/challenge-211/duncan-c-white/C/parseints.h
new file mode 100644
index 0000000000..da5e145a86
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/parseints.h
@@ -0,0 +1 @@
+extern int * parse_int_args( int argc, char ** argv, int argno, int * nel );
diff --git a/challenge-211/duncan-c-white/C/printarray.c b/challenge-211/duncan-c-white/C/printarray.c
new file mode 100644
index 0000000000..ddee597df3
--- /dev/null
+++ b/challenge-211/duncan-c-white/C/printarray.c
@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <string.h>
+
+
+// print_int_array( maxw, nelements, results[], sep, outfile );
+// format results[0..nelements-1] as a <sep> 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<nel; i++ )
+ {
+ char buf[100];
+ sprintf( buf, "%d", results[i] );
+ int len = strlen(buf);
+ if( linelen + len + 2 > maxw )
+ {
+ fputc( '\n', out );
+ linelen = 0;
+ } else if( i>0 )
+ {
+ fputc( ' ', out );
+ linelen++;
+ }
+
+ linelen += len;
+ fprintf( out, "%s", buf );
+ if( i<nel-1 )
+ {
+ fputc( sep, out );
+ linelen++;
+ }
+ }
+ //if( linelen>0 )
+ //{
+ // fputc( '\n', out );
+ //}
+}
diff --git a/challenge-211/duncan-c-white/C/printarray.h b/challenge-211/duncan-c-white/C/printarray.h
new file mode 100644
index 0000000000..40efb83277
--- /dev/null
+++ b/challenge-211/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-211/duncan-c-white/README b/challenge-211/duncan-c-white/README
index a7d82c8b31..a5f748da28 100644
--- a/challenge-211/duncan-c-white/README
+++ b/challenge-211/duncan-c-white/README
@@ -1,71 +1,58 @@
-Task 1: Special Bit Characters
+Task 1: Toeplitz Matrix
-You are given an array of binary bits that ends with 0.
-
-Valid sequences in the bit string are:
-
-[0] -decodes-to-> "a"
-[1, 0] -> "b"
-[1, 1] -> "c"
-
-Write a script to print 1 if the last character is an 'a' otherwise
-print 0.
+You are given an m x n matrix. Write a script to find out if the given
+matrix is a Toeplitz Matrix - a matrix is Toeplitz if every diagonal
+from top-left to bottom-right has the same elements.
Example 1
- Input: @bits = (1, 0, 0)
- Output: 1
-
- The given array bits can be decoded as 2-bits character (10) followed
- by 1-bit character (0).
+ Input: @matrix = [ [4, 3, 2, 1],
+ [5, 4, 3, 2],
+ [6, 5, 4, 3],
+ ]
+ Output: true
Example 2
- Input: @bits = (1, 1, 1, 0)
- Output: 0
-
- Possible decode can be 2-bits character (11) followed by 2-bits
- character (10) i.e. the last character is not 1-bit character.
+ Input: @matrix = [ [1, 2, 3],
+ [3, 2, 1],
+ ]
+ Output: false
-MY NOTES: very easy. decode string then check last letter of decoded version.
-I wonder if there's a way of decoding-and-checking together, though.
+MY NOTES: sounds very easy.
GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C
(look in the C directory for that).
-Task 2: Merge Account
+Task 2: Split Same Average
-You are given an array of accounts i.e. name with list of email addresses.
-
-Write a script to merge the accounts where possible. The accounts can
-only be merged if they have at least one email address in common.
+You are given an array of integers. Write a script to find out if the
+given can be split into two separate arrays whose average are the same.
Example 1:
- Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"],
- ["B", "b1@b.com"],
- ["A", "a3@a.com", "a1@a.com"] ]
- ]
+ Input: @nums = (1, 2, 3, 4, 5, 6, 7, 8)
+ Output: true
- Output: [ ["A", "a1@a.com", "a2@a.com", "a3@a.com"],
- ["B", "b1@b.com"] ]
+ We can split the given array into (1, 4, 5, 8) and (2, 3, 6, 7).
+ The average of the two arrays are the same i.e. 4.5.
Example 2:
- Input: @accounts = [ ["A", "a1@a.com", "a2@a.com"],
- ["B", "b1@b.com"],
- ["A", "a3@a.com"],
- ["B", "b2@b.com", "b1@b.com"] ]
-
- Output: [ ["A", "a1@a.com", "a2@a.com"],
- ["A", "a3@a.com"],
- ["B", "b1@b.com", "b2@b.com"] ]
-
-MY NOTES: fiddly and rather inelegant, especially only being allowed to
-merge two entries if the intersection of the email lists is non empty.
-Will also need to choose an input format, how about a list of words of the
-form A:a1@a.com,a2@a.com, B:b1@b.com, A:a3@a.com and B:b2@b.com,b1@b.com
-
-(TODO) GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C
-(TODO) (look in the C directory for that)
+ Input: @list = (1, 3)
+ Output: false
+
+MY NOTES: sounds easy enough as a brute force search.
+First observation: the "average of each sub-array" (our goal) must be
+the overall average, as that's the only way the sub-array averages can be
+the same value.
+Second observation: each element is either in the first subarray or in the
+second subarray, a binary counting approach is the obvious brute force method.
+(Note: I last did this in challenge 136, task 2, when we were summing subsets
+of Fibonacci numbers, so I've reused a little code from there).
+Third observation: we need only check one of the two sub-arrays (eg the first)
+as having the right sub-array average - the overall average.
+
+GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C
+(look in the C directory for that)
diff --git a/challenge-211/duncan-c-white/perl/ch-1.pl b/challenge-211/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..46284c921c
--- /dev/null
+++ b/challenge-211/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,87 @@
+#!/usr/bin/perl
+#
+# Task 1: Toeplitz Matrix
+#
+# You are given an m x n matrix. Write a script to find out if the given
+# matrix is a Toeplitz Matrix - a matrix is Toeplitz if every diagonal
+# from top-left to bottom-right has the same elements.
+#
+# Example 1
+#
+# Input: @matrix = [ [4, 3, 2, 1],
+# [5, 4, 3, 2],
+# [6, 5, 4, 3],
+# ]
+# Output: true
+#
+# Example 2
+#
+# Input: @matrix = [ [1, 2, 3],
+# [3, 2, 1],
+# ]
+# Output: false
+#
+# MY NOTES: sounds very easy.
+#
+# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C
+# (look in the C directory for that).
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Data::Dumper;
+use List::Util qw(any);
+
+my $debug=0;
+die "Usage: toeplitz [--debug] list-of-rows\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV>0;
+
+my @m = map { [ split( /,/, $_ ) ] } @ARGV;
+
+#
+# my $same = samediag($r,$c,$rows,$cols,$mref);
+# Return 1 iff all matrix cells in @$mref down the
+# top-left->bottom-right diagonal starting at ($r,$c)
+# are the same value; 0 otherwise.
+#
+sub samediag
+{
+ my( $r, $c, $rows, $cols, $mref ) = @_;
+ for( my $v = $mref->[$r][$c]; $r<$rows && $c<$cols; $r++, $c++ )
+ {
+ return 0 unless $mref->[$r][$c] == $v;