aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-02-20 00:14:02 +0000
committerGitHub <noreply@github.com>2023-02-20 00:14:02 +0000
commit93b3e6637e4158c7aed6bb6b4250c6d5209a0433 (patch)
tree9ae17ea099f26eacbd21f922f44d37b91172dacb
parent7aaabc1a42b375895b7c8c76ec405029a9218e07 (diff)
parentf2e6dc9cabfe67ddc973a8f13ce6c6d716a99412 (diff)
downloadperlweeklychallenge-club-93b3e6637e4158c7aed6bb6b4250c6d5209a0433.tar.gz
perlweeklychallenge-club-93b3e6637e4158c7aed6bb6b4250c6d5209a0433.tar.bz2
perlweeklychallenge-club-93b3e6637e4158c7aed6bb6b4250c6d5209a0433.zip
Merge pull request #7598 from dcw803/master
imported my solutions to this week's tasks, in Perl and C
-rw-r--r--challenge-204/duncan-c-white/C/.cbuild2
-rw-r--r--challenge-204/duncan-c-white/C/Makefile18
-rw-r--r--challenge-204/duncan-c-white/C/README14
-rw-r--r--challenge-204/duncan-c-white/C/args.c207
-rw-r--r--challenge-204/duncan-c-white/C/args.h11
-rw-r--r--challenge-204/duncan-c-white/C/ch-1.c73
-rw-r--r--challenge-204/duncan-c-white/C/ch-2.c153
-rw-r--r--challenge-204/duncan-c-white/C/parseints.c114
-rw-r--r--challenge-204/duncan-c-white/C/parseints.h1
-rw-r--r--challenge-204/duncan-c-white/C/printarray.c39
-rw-r--r--challenge-204/duncan-c-white/C/printarray.h1
-rw-r--r--challenge-204/duncan-c-white/README90
-rwxr-xr-xchallenge-204/duncan-c-white/perl/ch-1.pl75
-rwxr-xr-xchallenge-204/duncan-c-white/perl/ch-2.pl132
14 files changed, 878 insertions, 52 deletions
diff --git a/challenge-204/duncan-c-white/C/.cbuild b/challenge-204/duncan-c-white/C/.cbuild
index fd6334a425..a14ec76520 100644
--- a/challenge-204/duncan-c-white/C/.cbuild
+++ b/challenge-204/duncan-c-white/C/.cbuild
@@ -1,4 +1,4 @@
BUILD = ch-1 ch-2
CFLAGS = -Wall -g
-LDFLAGS = -lm
+#LDFLAGS = -lm
#CFLAGS = -g
diff --git a/challenge-204/duncan-c-white/C/Makefile b/challenge-204/duncan-c-white/C/Makefile
new file mode 100644
index 0000000000..54d3599c53
--- /dev/null
+++ b/challenge-204/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
+ch-2.o: ch-2.c args.h
+parseints.o: parseints.c args.h parseints.h printarray.h
+printarray.o: printarray.c
+
diff --git a/challenge-204/duncan-c-white/C/README b/challenge-204/duncan-c-white/C/README
new file mode 100644
index 0000000000..79d015ffe4
--- /dev/null
+++ b/challenge-204/duncan-c-white/C/README
@@ -0,0 +1,14 @@
+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
+- an int-array printing module printarray.[ch].
diff --git a/challenge-204/duncan-c-white/C/args.c b/challenge-204/duncan-c-white/C/args.c
new file mode 100644
index 0000000000..d4a2d38b9a
--- /dev/null
+++ b/challenge-204/duncan-c-white/C/args.c
@@ -0,0 +1,207 @@
+#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 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-204/duncan-c-white/C/args.h b/challenge-204/duncan-c-white/C/args.h
new file mode 100644
index 0000000000..8844a8f9c4
--- /dev/null
+++ b/challenge-204/duncan-c-white/C/args.h
@@ -0,0 +1,11 @@
+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_unsigned_real( char * val, double * n );
diff --git a/challenge-204/duncan-c-white/C/ch-1.c b/challenge-204/duncan-c-white/C/ch-1.c
new file mode 100644
index 0000000000..eb174ec1e9
--- /dev/null
+++ b/challenge-204/duncan-c-white/C/ch-1.c
@@ -0,0 +1,73 @@
+//
+// Task 1: Monotonic Array
+//
+// C version.
+//
+
+#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"
+
+
+// bool ismon = ismonotonic( list, nel );
+// Return true iff @list[0..nel-1] is monotonically increasing, ie.
+// for i <= j , list[i] <= list[j]. Else return false.
+//
+bool ismonotonic( int *list, int nel )
+{
+ for( int a=0; a<nel-1; a++ )
+ {
+ for( int b=a+1; b<nel; b++ )
+ {
+ if( list[a] > list[b] ) return false;
+ }
+ }
+ return true;
+}
+
+
+int main( int argc, char **argv )
+{
+ int argno = process_flag_n_m_args( "monotonic-array", argc, argv,
+ 1, 1000, "intlist" );
+
+ int nel;
+ int *list = parse_int_args( argc, argv, argno, &nel );
+
+ if( nel < 2 )
+ {
+ fprintf( stderr, "monotonic-array: need a list of at least 2 elements\n" );
+ exit(1);
+ }
+
+ if( debug )
+ {
+ printf( "debug: initial list: " );
+ print_int_array( 60, nel, list, ',', stdout );
+ putchar( '\n' );
+ }
+
+ bool ismon = ismonotonic( list, nel );
+
+ // reverse list
+ for( int i=0, j=nel-1; i<j; i++, j-- )
+ {
+ int t = list[i];
+ list[i] = list[j];
+ list[j] = t;
+ }
+
+ ismon |= ismonotonic( list, nel );
+ printf( "%d\n", ismon?1:0 );
+
+ free( list );
+
+ return 0;
+}
diff --git a/challenge-204/duncan-c-white/C/ch-2.c b/challenge-204/duncan-c-white/C/ch-2.c
new file mode 100644
index 0000000000..dd06482423
--- /dev/null
+++ b/challenge-204/duncan-c-white/C/ch-2.c
@@ -0,0 +1,153 @@
+//
+// Task 2: Reshape Matrix
+//
+// C version.
+//
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "args.h"
+
+
+// int *el = parse_matrix( matstr, &nel );
+// Parse a matrix string in $matstr, and produce
+// the 1-D flattened array of elements in $matstr,
+// and set nel to the number of elements - or
+// return NULL for failure.
+//
+int * parse_matrix( char *matstr, int *nel )
+{
+ // count commas
+ int n = 1;
+ for( char *s=matstr; *s; s++ )
+ {
+ if( *s == ',' )
+ {
+ n++;
+ }
+ }
+ if( debug )
+ {
+ printf( "debug: parse_matrix: n=%d\n", n );
+ }
+ *nel = n;
+
+ char *digcomma = malloc( strlen(matstr) * sizeof(char) );
+ assert( digcomma != NULL );
+
+ // build a copy of the matstr containing only digits and commas..
+ char *d = digcomma;
+ *d = '\0';
+ for( char *s=matstr; *s; s++ )
+ {
+ if( isdigit(*s) || *s == ',' )
+ {
+ *d++ = *s;
+ *d = '\0';
+ }
+ }
+
+ if( debug )
+ {
+ printf( "debug: parse_matrix: matstr=%s, digcomma=%s\n",
+ matstr, digcomma );
+ }
+
+ int *result = malloc( n * sizeof(int) );
+ assert( result != NULL );
+
+ // walk digcomma, atoi()ing each digit sequence
+ int i = 0;
+ char *s = digcomma;
+ char *p;
+ while( (p=strchr(s,',')) != NULL )
+ {
+ *p = '\0'; // null-terminate string here (for now)
+ result[i++] = atoi(s);
+ *p = ','; // change it back for neatness
+ if( debug )
+ {
+ printf( "debug: s=%s, p=%s, result[%d]=%d\n",
+ s, p, i-1, result[i-1] );
+ }
+ s = p+1;
+ }
+ result[i++] = atoi(s);
+
+ free( digcomma );
+ return result;
+}
+
+
+// print_resized_matrix( r2, c2, el, nel );
+// print out el[0..nel-1] as if it were a 2d matrix
+// is this cheating? haven't formed a 2d matrix in memory
+// at all, just printing the right string out..
+// nah; not cheating:-)
+//
+void print_resized_matrix( int r2, int c2, int *el, int nel )
+{
+ int elno=0;
+ for( int r=0; r<r2; r++ )
+ {
+ printf( "[ " );
+ for( int c=0; c<c2; c++ )
+ {
+ printf( "%d%c ", el[elno++], c==c2-1?' ':',' );
+ }
+ printf( "]\n" );
+ }
+}
+
+
+int main( int argc, char **argv )
+{
+ int argno = process_flag_n_args( "resize-matrix", argc, argv,
+ 3, "r c matrix" );
+
+ int r2 = atoi(argv[argno++]);
+ int c2 = atoi(argv[argno++]);
+ char * mat = argv[argno++];
+
+ if( r2 < 1 )
+ {
+ fprintf( stderr, "resize-matrix: r (%d) must be +ve\n", r2 );
+ exit(1);
+ }
+ if( c2 < 1 )
+ {
+ fprintf( stderr, "resize-matrix: c (%d) must be +ve\n", c2 );
+ exit(1);
+ }
+ int nel;
+ int *el = parse_matrix( mat, &nel );
+ if( el == NULL )
+ {
+ fprintf( stderr, "resize-matrix: bad matrix (%s)\n", mat );
+ exit(1);
+ }
+
+ // ok, print out the elements as a matrix
+ int n2 = r2 * c2;
+ if( debug )
+ {
+ printf( "debug: nel=%d, r2=%d, c2=%d, n2=%d\n",
+ nel, r2, c2, n2 );
+ }
+ if( nel == n2 )
+ {
+ print_resized_matrix( r2, c2, el, nel );
+ } else
+ {
+ printf( "0\n" );
+ }
+
+ free( el );
+
+ return 0;
+}
diff --git a/challenge-204/duncan-c-white/C/parseints.c b/challenge-204/duncan-c-white/C/parseints.c
new file mode 100644
index 0000000000..0fb9985633
--- /dev/null
+++ b/challenge-204/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 );
+ }
+ #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<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-204/duncan-c-white/C/parseints.h b/challenge-204/duncan-c-white/C/parseints.h
new file mode 100644
index 0000000000..da5e145a86
--- /dev/null
+++ b/challenge-204/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-204/duncan-c-white/C/printarray.c b/challenge-204/duncan-c-white/C/printarray.c
new file mode 100644
index 0000000000..ddee597df3
--- /dev/null
+++ b/challenge-204/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-204/duncan-c-white/C/printarray.h b/challenge-204/duncan-c-white/C/printarray.h
new file mode 100644
index 0000000000..40efb83277
--- /dev/null
+++ b/challenge-204/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-204/duncan-c-white/README b/challenge-204/duncan-c-white/README
index 22c27ddfb8..f38be3a4b5 100644
--- a/challenge-204/duncan-c-white/README
+++ b/challenge-204/duncan-c-white/README
@@ -1,84 +1,72 @@
-Task 1: Special Quadruplets
+Task 1: Monotonic Array
You are given an array of integers.
-Write a script to find out the total special quadruplets for the given array.
-Special Quadruplets are such that satisfies the following 2 rules.
-1) nums[a] + nums[b] + nums[c] == nums[d]
-2) a < b < c < d
+Write a script to find out if the given array is Monotonic. Print 1 if
+it is otherwise 0.
+
+An array is Monotonic if it is either monotone increasing or decreasing.
+
+Monotone increasing: for i <= j , nums[i] <= nums[j]
+Monotone decreasing: for i <= j , nums[i] >= nums[j]
Example 1
- Input: @nums = (1,2,3,6)
+ Input: @nums = (1,2,2,3)
Output: 1
- Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3].
-
Example 2
- Input: @nums = (1,1,1,3,5)
- Output: 4
-
- $nums[0] + $nums[1] + $nums[2] == $nums[3]
- $nums[0] + $nums[1] + $nums[3] == $nums[4]
- $nums[0] + $nums[2] + $nums[3] == $nums[4]
- $nums[1] + $nums[2] + $nums[3] == $nums[4]
+ Input: @nums = (1,3,2)
+ Output: 0
Example 3
- Input: @nums = (3,3,6,4,5)
- Output: 0
+ Input: @nums = (6,5,5,4)
+ Output: 1
-MY NOTES: seems pretty easy. condition (2) implies 4 for loops, a, b, c and d
+MY NOTES: seems very easy. use reverse() in order not to detect
+monotonically increasing and decreasing..
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: Copy Directory
-Submitted by: Julien Fiegehenn
+Task 2: Reshape Matrix
-You are given path to two folders, $source and $target.
+You are given a matrix (m x n) and two integers (r) and (c).
-Write a script that recursively copy the directory from $source to
-$target except any files.
+Write a script to reshape the given matrix in form (r x c) with the
+original value in the given matrix. If you can't reshape print 0.
-Example
+Example 1
- Input: $source = '/a/b/c' and $target = '/x/y'
+Input: $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
+ $r = 1
+ $c = 4
- Source directory structure:
+Output: [ 1 2 3 4 ]
-|___ a
-| |___ b
-| |___ c
-| |___ 1
-| | |___ 1.txt
-| |___ 2
-| | |___ 2.txt
-| |___ 3
-| | |___ 3.txt
-| |___ 4
-| |___ 5
-| | |___ 5.txt
+Example 2
-Target directory structure:
+Input: $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
+ $r = 3
+ $c = 2
-|___ x
-| |___ y
+Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
-Expected Result:
+Example 3
-|___ x
-| |___ y
-| |___ 1
-| |___ 2
-| |___ 3
-| |___ 4
-| |___ 5
+Input: $matrix = [ [ 1, 2 ] ]
+ $r = 3
+ $c = 2
+Output: 0
-MY NOTES: also quite easy, especially using File::Find.
+MY NOTES: also quite easy, can be done if R1*C1==R2*C
+(of course we have to discover R1 and C1, the dimensions o
+the original matrix). Also have to read the matrix in..
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)
+into C (look in the C directory for the translation). Needed to
+pass original sizes into C routine..
diff --git a/challenge-204/duncan-c-white/perl/ch-1.pl b/challenge-204/duncan-c-white/perl/ch-1.pl
new file mode 100755
index 0000000000..afa6489c87
--- /dev/null
+++ b/challenge-204/duncan-c-white/perl/ch-1.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl
+#
+# Task 1: Monotonic Array
+#
+# You are given an array of integers.
+#
+# Write a script to find out if the given array is Monotonic. Print 1 if
+# it is otherwise 0.
+#
+# An array is Monotonic if it is either monotone increasing or decreasing.
+#
+# Monotone increasing: for i <= j , nums[i] <= nums[j]
+# Monotone decreasing: for i <= j , nums[i] >= nums[j]
+#
+# Example 1
+#
+# Input: @nums = (1,2,2,3)
+# Output: 1
+#
+# Example 2
+#
+# Input: @nums = (1,3,2)
+# Output: 0
+#
+# Example 3
+#
+# Input: @nums = (6,5,5,4)
+# Output: 1
+#
+# MY NOTES: seems very easy. use reverse() in order not to detect
+# monotonically increasing and decreasing..
+#
+# 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: monotonic-array [--debug] intlist\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV>0;
+
+=pod
+
+=head2 my $ismon = ismonotonic( @list );
+
+Return 1 iff @list is monotonically increasing, ie.
+for i <= j , nums[i] <= nums[j]. Else return 0.
+
+=cut
+sub ismonotonic
+{
+ my( @list ) = @_;
+ foreach my $a (0..$#list-1)
+ {
+ foreach my $b ($a+1..$#list)
+ {
+ return 0 if $list[$a] > $list[$b];
+ }
+ }
+ return 1;
+}
+
+
+my @list = split( /,/, join(',',@ARGV) );
+
+die "monotonic-array: need at least 2 ints in list\n" unless @list>1;
+
+my $ismon = ismonotonic( @list );
+$ismon |= ismonotonic( reverse @list );
+say $ismon;
diff --git a/challenge-204/duncan-c-white/perl/ch-2.pl b/challenge-204/duncan-c-white/perl/ch-2.pl
new file mode 100755
index 0000000000..a1058d2e57
--- /dev/null
+++ b/challenge-204/duncan-c-white/perl/ch-2.pl
@@ -0,0 +1,132 @@
+#!/usr/bin/perl
+#
+# Task 2: Reshape Matrix
+#
+# You are given a matrix (m x n) and two integers (r) and (c)
+#
+# Write a script to reshape the given matrix in form (r x c) with th
+# original value in the given matrix. If you can't reshape print 0
+#
+# Example 1
+#
+# Input: $matrix = [ [ 1, 2 ], [ 3, 4 ] ]
+# $r = 1
+# $c = 4
+#
+# Output: [ 1 2 3 4 ]
+#
+# Example 2
+#
+# Input: $matrix = [ [ 1, 2, 3 ] , [ 4, 5, 6 ] ]
+# $r = 3
+# $c = 2
+#
+# Output: [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
+#
+#
+# Example 3
+#
+# Input: $matrix = [ [ 1, 2 ] ]
+# $r = 3
+# $c = 2
+#
+# Output: 0
+#
+# MY NOTES: also quite easy, can be done if R1*C1==R2*C
+# (of course we have to discover R1 and C1, the dimensions o
+# the original matrix). Also have to read the matrix in..
+#
+# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.p
+# into C (look in the C directory for the translation). Needed t
+# pass original sizes into C routine..
+#
+
+use strict;
+use warnings;
+use feature 'say';
+use Getopt::Long;
+use Function::Parameters;
+use Data::Dumper;
+
+my $debug=0;
+die "Usage: resize-matrix [--debug] r c matrix\n"
+ unless GetOptions( "debug"=>\$debug ) && @ARGV == 3;
+
+my( $r2, $c2, $mat ) = @ARGV;
+
+die "resize-matrix: r ($r2) must be +ve\n" unless $r2 > 0;
+die "resize-matrix: c ($c2) must be +ve\n" unless $c2 > 0;
+my @el = parse_matrix( $mat );
+die "resize-matrix: bad matrix ($mat)\n" unless @el;
+
+my $n1 = @el;
+
+say "debug: n1=$n1, r2=$r2, c2=$c2" if $debug;
+my $n2 = $r2 * $c2;
+
+if( $n1 == $n2 )
+{
+ $mat = resize_matrix( $r2, $c2, @el );
+ print_matrix( $mat );
+} else
+{
+ say 0;
+}
+
+
+=pod
+
+=head2 my @el = parse_matrix( $matstr );
+
+Parse a matrix string in $matstr, and produce
+the 1-D flattened array of elements in $matstr.
+
+=cut
+fun parse_matrix( $matstr )
+{
+ $matstr =~ tr/[] //d;
+ say "debug: matstr=$matstr" if $debug;
+ return split(/,/,$matstr);
+}
+
+
+=pod
+
+=head2 my $mat = resize_matrix( $r, $c, @el );
+
+Resize @el list of elements to a matrix with
+$r rows and $c columns. Return it.
+
+=cut
+fun resize_matrix( $r, $c, @el )
+{
+ my $result = [];
+ foreach my $i (0..$r-1)
+ {
+ my @row;
+ foreach my $j (0..$c-1)
+ {
+ push @row, shift @el;
+ }
+ push @$result, \@row;
+ }
+ return $result;
+}
+
+
+
+=pod
+
+=head2 print_matrix( $mat );
+
+Print matrix $mat out.
+
+=cut
+fun print_matrix( $mat )
+{
+ foreach my $row (@$mat)
+ {
+ my $inner = join( ',', @$row );
+ say "[ $inner ]";
+ }
+}