diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-04-30 23:30:38 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-30 23:30:38 +0100 |
| commit | d71a165350cf250f9dc411ee04f1910172aefde1 (patch) | |
| tree | 5e13cd2aaa93161240ac788bb3272ed1f174a36e | |
| parent | 8d8d6d9ba0ab373f39bb49981b47bd905dd9057f (diff) | |
| parent | 597a829fecd63da1ca25a8349683141ae6a3046e (diff) | |
| download | perlweeklychallenge-club-d71a165350cf250f9dc411ee04f1910172aefde1.tar.gz perlweeklychallenge-club-d71a165350cf250f9dc411ee04f1910172aefde1.tar.bz2 perlweeklychallenge-club-d71a165350cf250f9dc411ee04f1910172aefde1.zip | |
Merge pull request #7993 from dcw803/master
imported my solutions to this week's tasks (both in Perl, first in C)
| -rw-r--r-- | challenge-213/duncan-c-white/README | 6 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/Makefile | 17 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/README | 10 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/args.c | 234 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/args.h | 12 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/ch-1.c | 93 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/parseints.c | 114 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/parseints.h | 1 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/printarray.c | 39 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/printarray.h | 1 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/rank.c | 46 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/C/rank.h | 1 | ||||
| -rw-r--r-- | challenge-214/duncan-c-white/README | 123 | ||||
| -rwxr-xr-x | challenge-214/duncan-c-white/perl/ch-1.pl | 93 | ||||
| -rwxr-xr-x | challenge-214/duncan-c-white/perl/ch-2.pl | 159 |
15 files changed, 905 insertions, 44 deletions
diff --git a/challenge-213/duncan-c-white/README b/challenge-213/duncan-c-white/README index bf3941e44d..036a8e0734 100644 --- a/challenge-213/duncan-c-white/README +++ b/challenge-213/duncan-c-white/README @@ -21,7 +21,7 @@ Example 3 MY NOTES: sounds very easy. Select evens; sort them; select odds; sort them, then append sorted lists together. -GUEST LANGUAGE: As a bonus, I've had a go at translating ch-2.pl into C, +GUEST LANGUAGE: As a bonus, I've had a go at translating ch-1.pl into C, look in the C/ directory for that. @@ -67,5 +67,5 @@ MY NOTES: hmmm.. weird "list of routes" data format. Easier if we can convert the data into "normal" node -> list possible next nodes form. Shortest route means: find all routes and min() them? -GUEST LANGUAGE: I will translate ch-2.pl into C tomorrow. Run out of time -to do it today. +GUEST LANGUAGE: As a bonus, I've had a go at translating ch-2.pl into C, +look in the C/ directory for that. diff --git a/challenge-214/duncan-c-white/C/Makefile b/challenge-214/duncan-c-white/C/Makefile new file mode 100644 index 0000000000..3a6ebb224c --- /dev/null +++ b/challenge-214/duncan-c-white/C/Makefile @@ -0,0 +1,17 @@ +# Makefile rules generated by CB +CC = gcc +CFLAGS = -Wall -g +BUILD = ch-1 + +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 rank.o +ch-1.o: ch-1.c args.h parseints.h printarray.h rank.h +parseints.o: parseints.c args.h parseints.h printarray.h +printarray.o: printarray.c +rank.o: rank.c rank.h + diff --git a/challenge-214/duncan-c-white/C/README b/challenge-214/duncan-c-white/C/README new file mode 100644 index 0000000000..ba67141dfe --- /dev/null +++ b/challenge-214/duncan-c-white/C/README @@ -0,0 +1,10 @@ +Thought I'd also have a go at translating ch-1.pl and ch-2.pl (coming soon) into C.. + +Both C versions produce very similar (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]. +- a NEW clone of printarray.[ch] that prints RANKS: rank.[ch] diff --git a/challenge-214/duncan-c-white/C/args.c b/challenge-214/duncan-c-white/C/args.c new file mode 100644 index 0000000000..20c21e6c30 --- /dev/null +++ b/challenge-214/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-214/duncan-c-white/C/args.h b/challenge-214/duncan-c-white/C/args.h new file mode 100644 index 0000000000..df765fa21e --- /dev/null +++ b/challenge-214/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-214/duncan-c-white/C/ch-1.c b/challenge-214/duncan-c-white/C/ch-1.c new file mode 100644 index 0000000000..09c42a0e18 --- /dev/null +++ b/challenge-214/duncan-c-white/C/ch-1.c @@ -0,0 +1,93 @@ +// Task 1: Rank Score +// +// 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" +#include "rank.h" + + +static int intcmp_desc( const void *a, const void *b ) +{ + int *ap = (int *)a; + int *bp = (int *)b; + return *bp - *ap; +} + + +// +// bool in = isin( v, list, nel ); +// Return true iff v is in list[nel]. +// +bool isin( int v, int *list, int nel ) +{ + for( int i=0; i<nel; i++ ) + { + if( list[i] == v ) return true; + } + return false; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "rank-score", 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 sorted[nel]; + memcpy( sorted, list, nel*sizeof(int) ); + + // sort in DESCENDING sorted order + qsort( sorted, nel, sizeof(int), &intcmp_desc ); + + // allocate ranks, coping with joint ranks.. + int rank = 1; + int result[ nel ]; + + // foreach distinct value <val> in sorted order + for( int i=0; i<nel; i++ ) + { + if( i==0 || ! isin( sorted[i], sorted, i ) ) // distinct + { + int val = sorted[i]; + if( debug ) + { + printf( "distinct value %d\n", val ); + } + + int n = 0; + for( int j=0; j<nel; j++ ) + { + if( list[j] == val ) + { + result[j] = rank; + n++; + } + } + rank+=n; + } + } + + print_rank_array( 60, nel, result, ',', stdout ); + putchar( '\n' ); + + free( list ); + return 0; +} diff --git a/challenge-214/duncan-c-white/C/parseints.c b/challenge-214/duncan-c-white/C/parseints.c new file mode 100644 index 0000000000..3e820eb334 --- /dev/null +++ b/challenge-214/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-214/duncan-c-white/C/parseints.h b/challenge-214/duncan-c-white/C/parseints.h new file mode 100644 index 0000000000..da5e145a86 --- /dev/null +++ b/challenge-214/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-214/duncan-c-white/C/printarray.c b/challenge-214/duncan-c-white/C/printarray.c new file mode 100644 index 0000000000..ddee597df3 --- /dev/null +++ b/challenge-214/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-214/duncan-c-white/C/printarray.h b/challenge-214/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-214/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-214/duncan-c-white/C/rank.c b/challenge-214/duncan-c-white/C/rank.c new file mode 100644 index 0000000000..be697c6219 --- /dev/null +++ b/challenge-214/duncan-c-white/C/rank.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <string.h> + + +#include "rank.h" + + +// print_rank_array( maxw, nelements, results[], sep, outfile ); +// format results[0..nelements-1] as a <sep> separated +// list onto outfile with lines <= maxw chars long. +// Replace 1 with G, 2 with S, and 3 with B. +// produces a whole number of lines of output. +// +void print_rank_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] ); + switch( results[i] ) + { + case 1: strcpy( buf, "G" ); break; + case 2: strcpy( buf, "S" ); break; + case 3: strcpy( buf, "B" ); break; + } + 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++; + } + } +} diff --git a/challenge-214/duncan-c-white/C/rank.h b/challenge-214/duncan-c-white/C/rank.h new file mode 100644 index 0000000000..846d2fbde0 --- /dev/null +++ b/challenge-214/duncan-c-white/C/rank.h @@ -0,0 +1 @@ +extern void print_rank_array( int maxw, int nel, int * results, char sep, FILE * out ); diff --git a/challenge-214/duncan-c-white/README b/challenge-214/duncan-c-white/README index bf3941e44d..17df43b304 100644 --- a/challenge-214/duncan-c-white/README +++ b/challenge-214/duncan-c-white/README @@ -1,71 +1,112 @@ -Task 1: Fun Sort +Task 1: Rank Score -You are given a list of positive integers. Write a script to sort the -all even integers first then all odds in ascending order. +You are given a list of scores (>=1). + +Write a script to rank each score in descending order. First three will +get medals i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get +the ranking number. + +Use the standard model of giving equal scores equal rank, then advancing +that number of ranks. Example 1 - Input: @list = (1,2,3,4,5,6) - Output: (2,4,6,1,3,5) + Input: @scores = (1,2,4,3,5) + Output: (5,4,S,B,G) + + Score 1 is the 5th rank. + Score 2 is the 4th rank. + Score 4 is the 2nd rank i.e. Silver (S). + Score 3 is the 3rd rank i.e. Bronze (B). + Score 5 is the 1st rank i.e. Gold (G). Example 2 - Input: @list = (1,2) - Output: (2,1) + Input: @scores = (8,5,6,7,4) + Output: (G,4,B,S,5) + + Score 8 is the 1st rank i.e. Gold (G). + Score 4 is the 4th rank. + Score 6 is the 3rd rank i.e. Bronze (B). + Score 7 is the 2nd rank i.e. Silver (S). + Score 4 is the 5th rank. Example 3 - Input: @list = (1) - Output: (1) + Input: @list = (3,5,4,2) + Output: (B,G,S,4) + +Example 4 -MY NOTES: sounds very easy. Select evens; sort them; select odds; sort them, -then append sorted lists together. + Input: @scores = (2,5,2,1,7,5,1) + Output: (4,S,4,6,G,S,6) -GUEST LANGUAGE: As a bonus, I've had a go at translating ch-2.pl into C, +MY NOTES: sounds pretty easy, although scores (high is best) and ranks (low +is best) are a little confusing. Even worse is joint ranks for equal scores. +Start from the end: Gold is a label for rank 1, Single is a label for rank 2, +Bronze is a label for rank 3. So work out the ranks (including the nasty +joint ranks thing) and then apply the G/S/B stuff at the end. + +GUEST LANGUAGE: As a bonus, I've had a go at translating ch-1.pl into C, look in the C/ directory for that. -Task 2: Shortest Route +Task 2: Collect Points -You are given a list of bidirectional routes defining a network of nodes, -as well as source and destination node numbers. Write a script to find -the route from source to destination that passes through fewest nodes. +You are given a list of numbers. +You will perform a series of removal operations. For each operation, +you remove from the list N (N >= 1) equal and consecutive numbers, +and add to your score N x N. +Determine the maximum possible score. Example 1: - Input: @routes = ([1,2,6], [5,6,7]) - $source = 1 - $destination = 7 - - Output: (1,2,6,7) + Input: @numbers = (2,4,3,3,3,4,5,4,2) + Output: 23 - Source (1) is part of route [1,2,6] so the journey looks like 1 -> 2 -> 6 - then jump to route [5,6,7] and takes the route 6 -> 7. - So the final route is (1,2,6,7) + We see three 3's next to each other so let us remove that first and + collect 3 x 3 points. + So now the list is (2,4,4,5,4,2). + Let us now remove 5 so that all 4's can be next to each other and + collect 1 x 1 point. + So now the list is (2,4,4,4,2). + Time to remove three 4's and collect 3 x 3 points. + Now the list is (2,2). + Finally remove both 2's and collect 2 x 2 points. + So the total points collected is 9 + 1 + 9 + 4 => 23. Example 2: - Input: @routes = ([1,2,3], [4,5,6]) - $source = 2 - $destination = 5 + Input: @numbers = (1,2,2,2,2,1) + Output: 20 - Output: -1 + Remove four 2's first and collect 4 x 4 points. + Now the list is (1,1). + Finally remove the two 1's and collect 2 x 2 points. + So the total points collected is 16 + 4 => 20. Example 3: - Input: @routes = ([1,2,3], [4,5,6], [3,8,9], [7,8]) - $source = 1 - $destination = 7 - Output: (1,2,3,8,7) + Input: @numbers = (1) + Output: 1 + +Example 4: + + Input: @numbers = (2,2,2,1,1,2,2,2) + Output: 40 - Source (1) is part of route [1,2,3] so the journey looks like 1 -> 2 -> 3 - then jump to route [3,8,9] and takes the route 3 -> 8 - then jump to route [7,8] and takes the route 8 -> 7 - So the final route is (1,2,3,8,7) + Remove two 1's = 2 x 2 points. + Now the list is (2,2,2,2,2,2). + Then remove six 2's = 6 x 6 points. -MY NOTES: hmmm.. weird "list of routes" data format. Easier if we can -convert the data into "normal" node -> list possible next nodes form. -Shortest route means: find all routes and min() them? +MY NOTES: hmmm.. that seems a bit tricky, in particular I can't immediately +see whether we have do try all possible first moves (etc), or just the first +move (or nth move more generally) with the greatest score contribution.. +Actually, eg 4 shows that we CAN'T just take the individual move with the +greatest score contribution, cos otherwise we'd take one of the "length 3" +sequences and pass up the "length 6" sequence that becomes available later.. +So, yes, it's a brute force "find all" type problem.. -GUEST LANGUAGE: I will translate ch-2.pl into C tomorrow. Run out of time -to do it today. +GUEST LANGUAGE: As a bonus, I will soon have a go at translating ch-2.pl +into C, but I run out of time today.. when it's done, look in the C/ +directory for that. diff --git a/challenge-214/duncan-c-white/perl/ch-1.pl b/challenge-214/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..fd75ff0b18 --- /dev/null +++ b/challenge-214/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl +# +# Task 1: Rank Score +# +# You are given a list of scores (>=1). +# +# Write a script to rank each score in descending order. First three will +# get medals i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get +# the ranking number. +# +# Use the standard model of giving equal scores equal rank, then advancing +# that number of ranks. +# +# Example 1 +# +# Input: @scores = (1,2,4,3,5) +# Output: (5,4,S,B,G) +# +# Score 1 is the 5th rank. +# Score 2 is the 4th rank. +# Score 4 is the 2nd rank i.e. Silver (S). +# Score 3 is the 3rd rank i.e. Bronze (B). +# Score 5 is the 1st rank i.e. Gold (G). +# +# Example 2 +# +# Input: @scores = (8,5,6,7,4) +# Output: (G,4,B,S,5) +# +# Score 8 is the 1st rank i.e. Gold (G). +# Score 4 is the 4th rank. +# Score 6 is the 3rd rank i.e. Bronze (B). +# Score 7 is the 2nd rank i.e. Silver (S). +# Score 4 is the 5th rank. +# +# Example 3 +# +# Input: @list = (3,5,4,2) +# Output: (B,G,S,4) +# +# Example 4 +# +# Input: @scores = (2,5,2,1,7,5,1) +# Output: (4,S,4,6,G,S,6) +# +# MY NOTES: sounds pretty easy, although scores (high is best) and ranks (low +# is best) are a little confusing. Even worse is joint ranks for equal scores. +# Start from the end: Gold is a label for rank 1, Single is a label for rank 2, +# Bronze is a label for rank 3. So work out the ranks (including the nasty +# joint ranks thing) and then apply the G/S/B stuff at the end. +# +# GUEST LANGUAGE: As a bonus, I've 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: rank-score [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my @list = split( /,/, join(',',@ARGV) ); + +say "debug: list: ", join(',',@list) if $debug; + +# distinct values in sorted order +my %set = map { $_ => 1 } @list; +my @distinctsortval = sort { $b <=> $a } keys %set; + +# allocate ranks, coping with joint ranks.. +my $rank = 1; +my @result; +foreach my $val (@distinctsortval) +{ + my @pos = grep { $list[$_] == $val } 0..$#list; + $result[$_] = $rank for @pos; + $rank += @pos; +} + +# change ranks 1..3 -> G,S,B +foreach my $v (@result) +{ + $v = 'G' if $v eq 1; + $v = 'S' if $v eq 2; + $v = 'B' if $v eq 3; +} + +say join(',', @result); diff --git a/challenge-214/duncan-c-white/perl/ch-2.pl b/challenge-214/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..ad6d7facf5 --- /dev/null +++ b/challenge-214/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,159 @@ +#!/usr/bin/perl +# +# Task 2: Collect Points +# +# You are given a list of numbers. +# You will perform a series of removal operations. For each operation, +# you remove from the list N (N >= 1) equal and consecutive numbers, +# and add to your score N x N. +# Determine the maximum possible score. +# +# Example 1: +# +# Input: @numbers = (2,4,3,3,3,4,5,4,2) +# Output: 23 +# +# We see three 3's next to each other so let us remove that first and +# collect 3 x 3 points. +# So now the list is (2,4,4,5,4,2). +# Let us now remove 5 so that all 4's can be next to each other and +# collect 1 x 1 point. +# So now the list is (2,4,4,4,2). +# Time to remove three 4's and collect 3 x 3 points. +# Now the list is (2,2). +# Finally remove both 2's and collect 2 x 2 points. +# So the total points collected is 9 + 1 + 9 + 4 => 23. +# +# Example 2: +# +# Input: @numbers = (1,2,2,2,2,1) +# Output: 20 +# +# Remove four 2's first and collect 4 x 4 points. +# Now the list is (1,1). +# Finally remove the two 1's and collect 2 x 2 points. +# So the total points collected is 16 + 4 => 20. +# +# Example 3: +# +# Input: @numbers = (1) +# Output: 1 +# +# Example 4: +# +# Input: @numbers = (2,2,2,1,1,2,2,2) +# Output: 40 +# +# Remove two 1's = 2 x 2 points. +# Now the list is (2,2,2,2,2,2). +# Then remove six 2's = 6 x 6 points. +# +# MY NOTES: hmmm.. that seems a bit tricky, in particular I can't immediately +# see whether we have do try all possible first moves (etc), or just the first +# move (or nth move more generally) with the greatest score contribution.. +# Actually, eg 4 shows that we CAN'T just take the individual move with the +# greatest score contribution, cos otherwise we'd take one of the "length 3" +# sequences and pass up the "length 6" sequence that becomes available later.. +# So, yes, it's a brute force "find all" type problem.. +# +# GUEST LANGUAGE: As a bonus, I will soon have a go at translating ch-2.pl +# into C, but I run out of time today.. when it's done, look in the C/ +# directory for that. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Data::Dumper; +use Function::Parameters; + +my $debug=0; +die "Usage: collect-points [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV > 0; + + +# +# my @moves = all_possible_first_moves( @list ); +# Find all possible first (immediate) moves, return +# a list of moves where each move is a [ startpos, len ] pair. +# +fun all_possible_first_moves( @list ) +{ < |
