diff options
| -rw-r--r-- | challenge-207/duncan-c-white/C/.cbuild | 4 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/Makefile | 18 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/README | 12 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/args.c | 207 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/args.h | 11 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/ch-1.c | 90 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/ch-2.c | 63 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/parseints.c | 114 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/parseints.h | 1 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/printarray.c | 39 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/C/printarray.h | 1 | ||||
| -rw-r--r-- | challenge-207/duncan-c-white/README | 83 | ||||
| -rwxr-xr-x | challenge-207/duncan-c-white/perl/ch-1.pl | 64 | ||||
| -rwxr-xr-x | challenge-207/duncan-c-white/perl/ch-2.pl | 69 |
14 files changed, 735 insertions, 41 deletions
diff --git a/challenge-207/duncan-c-white/C/.cbuild b/challenge-207/duncan-c-white/C/.cbuild new file mode 100644 index 0000000000..a14ec76520 --- /dev/null +++ b/challenge-207/duncan-c-white/C/.cbuild @@ -0,0 +1,4 @@ +BUILD = ch-1 ch-2 +CFLAGS = -Wall -g +#LDFLAGS = -lm +#CFLAGS = -g diff --git a/challenge-207/duncan-c-white/C/Makefile b/challenge-207/duncan-c-white/C/Makefile new file mode 100644 index 0000000000..34d8a4d520 --- /dev/null +++ b/challenge-207/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 +ch-1.o: ch-1.c args.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-207/duncan-c-white/C/README b/challenge-207/duncan-c-white/C/README new file mode 100644 index 0000000000..7f30f346ea --- /dev/null +++ b/challenge-207/duncan-c-white/C/README @@ -0,0 +1,12 @@ +Thought I'd also have a go at translating ch-1.pl and ch-2.pl into C.. + +Both C versions produce identical (non-debugging and debugging) +output to the Perl originals. + +ch-1.c has to implement a routine to match /^[charset]+$/, where charset +is the parameter. + +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-207/duncan-c-white/C/args.c b/challenge-207/duncan-c-white/C/args.c new file mode 100644 index 0000000000..d4a2d38b9a --- /dev/null +++ b/challenge-207/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-207/duncan-c-white/C/args.h b/challenge-207/duncan-c-white/C/args.h new file mode 100644 index 0000000000..8844a8f9c4 --- /dev/null +++ b/challenge-207/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-207/duncan-c-white/C/ch-1.c b/challenge-207/duncan-c-white/C/ch-1.c new file mode 100644 index 0000000000..3fe9b6b63b --- /dev/null +++ b/challenge-207/duncan-c-white/C/ch-1.c @@ -0,0 +1,90 @@ +// +// Task 1: Keyboard Word +// +// C version. +// + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <ctype.h> +#include <assert.h> + +#include "args.h" + + +// bool matched = matchoneofchar( ch, chars ); +// Find whether or not <ch> is any of the characters from <chars>. +// Return true iff it does. +// +bool matchoneofchar( char ch, char *s ) +{ + for( ; *s; s++ ) + { + if( *s == tolower(ch) ) return true; + } + return false; +} + + +// bool matched = match( w, chars ); +// Find whether or not <w> matches only characters from <chars>. +// Return true iff it does. +// +bool match( char *w, char *chars ) +{ + for( ; *w; w++ ) + { + if( !matchoneofchar( *w, chars ) ) return false; + } + return true; +} + + +// bool matched = matchword( w ); +// Find whether or not <w> matches any of the keyboard rows. +// Return true iff it does. +// +bool matchword( char *w ) +{ + if( match( w, "qwertyuiop" ) ) return true; + if( match( w, "asdfghjkl" ) ) return true; + if( match( w, "zxcvbnm" ) ) return true; + return false; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "keyboard-word", argc, argv, + 1, 1000, "wordlist" ); + + int nel = argc-argno; + + if( debug ) + { + printf( "debug: initial list: nel=%d, ", nel ); + for( int i=argno; i<argc; i++ ) + { + if( i>argno ) putchar(','); + printf( "%s", argv[i] ); + } + putchar( '\n' ); + } + + printf( "(" ); + bool first = true; + for( int i=argno; i<argc; i++ ) + { + if( matchword( argv[i] ) ) + { + if( !first ) putchar(','); + first = false; + printf( "\"%s\"", argv[i] ); + } + } + printf( ")\n" ); + + return 0; +} diff --git a/challenge-207/duncan-c-white/C/ch-2.c b/challenge-207/duncan-c-white/C/ch-2.c new file mode 100644 index 0000000000..7e26d293b0 --- /dev/null +++ b/challenge-207/duncan-c-white/C/ch-2.c @@ -0,0 +1,63 @@ +// +// Task 2: H-Index +// +// 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" + + +int intcompare( const void *ap, const void *bp ) +{ + int a = *((int *)ap); + int b = *((int *)bp); + return b-a; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "h-index", argc, argv, + 1, 1000, "intlist" ); + + int nel; + int *list = parse_int_args( argc, argv, argno, &nel ); + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + qsort( list, nel, sizeof(int), &intcompare ); + + if( debug ) + { + printf( "debug: sorted list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + int h = -1; + + for( int pos=0; pos<nel; pos++ ) + { + if( list[pos] >= pos+1 ) h = pos+1; + } + + printf( "%d\n", h ); + + free( list ); + + return 0; +} diff --git a/challenge-207/duncan-c-white/C/parseints.c b/challenge-207/duncan-c-white/C/parseints.c new file mode 100644 index 0000000000..80408d3382 --- /dev/null +++ b/challenge-207/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_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 != 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-207/duncan-c-white/C/parseints.h b/challenge-207/duncan-c-white/C/parseints.h new file mode 100644 index 0000000000..da5e145a86 --- /dev/null +++ b/challenge-207/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-207/duncan-c-white/C/printarray.c b/challenge-207/duncan-c-white/C/printarray.c new file mode 100644 index 0000000000..ddee597df3 --- /dev/null +++ b/challenge-207/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-207/duncan-c-white/C/printarray.h b/challenge-207/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-207/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-207/duncan-c-white/README b/challenge-207/duncan-c-white/README index 3a95b6badb..c6faef5c45 100644 --- a/challenge-207/duncan-c-white/README +++ b/challenge-207/duncan-c-white/README @@ -1,66 +1,67 @@ -Task 1: Shortest Time +Task 1: Keyboard Word -You are given a list of time points, at least 2, in the 24-hour clock -format HH:MM. Write a script to find out the shortest time in minutes -between any two time points. +You are given an array of words. Write a script to print all the words +in the given array that can be typed using alphabet on only one row of +the keyboard. -Example 1 +Let us assume the keys are arranged as below: + +Row 1: qwertyuiop +Row 2: asdfghjkl +Row 3: zxcvbnm - Input: @time = ("00:00", "23:55", "20:00") - Output: 5 +Example 1 - Since the difference between "00:00" and "23:55" is the shortest (5 minutes). + Input: @words = ("Hello","Alaska","Dad","Peace") + Output: ("Alaska","Dad") Example 2 - Input: @array = ("01:01", "00:50", "00:57") - Output: 4 + Input: @array = ("OMG","Bye") + Output: () -Example 3 +MY NOTES: very easy. Lower case each word, then match it against /^[qwertyuiop]+$/ for the first row, similarly regexes for the second and third rows. - Input: @array = ("10:10", "09:30", "09:00", "09:55") - Output: 15 +GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C +(look in the C directory for that). replaced regexes with a custom: match a +sequence of <these chars:string>. -MY NOTES: reasonably easy, although of course wraparound has to taken -into account. Might convert each hh:mm time into a number of minutes, -then sort the array.. then check every adjacant pair (including the -wraparound pair, i.e the last and the first).. -No C solutions today. Might do them later.. +Task 2: H-Index +You are given an array of integers containing citations a researcher +has received for each paper. Write a script to compute the researcher's +H-Index. For more information please checkout: -Task 2: Array Pairings +https://en.wikipedia.org/wiki/H-index + +The H-Index is the largest number h such that h articles have at least +h citations each. For example, if an author has five publications, with +9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the +author's H-index is 3, because the author has three publications with 3 +or more citations. However, the author does not have four publications +with 4 or more citations. -You are given an array of integers having even number of elements.. -Write a script to find the maximum sum of the minimum of each pairs. Example 1 - Input: @array = (1,2,3,4) + Input: @citations = (10,8,5,4,3) Output: 4 - Possible Pairings are as below: - a) (1,2) and (3,4). So min(1,2) + min(3,4) => 1 + 3 => 4 - b) (1,3) and (2,4). So min(1,3) + min(2,4) => 1 + 2 => 3 - c) (1,4) and (2,3). So min(1,4) + min(2,3) => 2 + 1 => 3 - - So the maxium sum is 4. +Because the 4th publication has 4 citations and the 5th has only 3. Example 2 - Input: @array = (0,2,1,3) - Output: 2 + Input: @citations = (25,8,5,3,3) + Output: 3 + +The H-Index is 3 because the fourth paper has only 3 citations. - Possible Pairings are as below: - a) (0,2) and (1,3). So min(0,2) + min(1,3) => 0 + 1 => 1 - b) (0,1) and (2,3). So min(0,1) + min(2,3) => 0 + 2 => 2 - c) (0,3) and (2,1). So min(0,3) + min(2,1) => 0 + 1 => 1 - - So the maximum sum is 2. +MY NOTES: also pretty easy. The Wikipedia entry sheds more light: -MY NOTES: hmm. I wish one of the examples had 6 elements. It seems -to me that this sounds like a recursive solution.. Pick each possible -pair involving the first element and each of the others (in turn), -remove them, calculate and total up the minimum, then recurse. +"First we order the citations from the largest to the lowest value. + Then, we look for the last position in which citation[h] >= h." + (This assumes array indexes start at 1, I think). -No C solutions today. Might do them later.. +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-207/duncan-c-white/perl/ch-1.pl b/challenge-207/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..7243fab745 --- /dev/null +++ b/challenge-207/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!/usr/bin/perl +# +# Task 1: Keyboard Word +# +# You are given an array of words. Write a script to print all the words +# in the given array that can be typed using alphabet on only one row of +# the keyboard. +# +# Let us assume the keys are arranged as below: +# +# Row 1: qwertyuiop +# Row 2: asdfghjkl +# Row 3: zxcvbnm +# +# Example 1 +# +# Input: @words = ("Hello","Alaska","Dad","Peace") +# Output: ("Alaska","Dad") +# +# Example 2 +# +# Input: @array = ("OMG","Bye") +# Output: () +# +# MY NOTES: very easy. Lower case each word, then match it against /^[qwertyuiop]+$/ for the first row, similarly regexes for the second and third rows. +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-1.pl into C +# (look in the C directory for that). replaced regexes with a custom: match a +# sequence of <these chars:string>. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Data::Dumper; + +my $debug=0; +die "Usage: keyboard-word [--debug] word-list\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my @list = split( /,/, join(',',@ARGV) ); + +say "list: ", join(',',@list) if $debug; + + +# +# my $matched = matchword( $w ); +# Find whether or not $w matches any of the keyboard rows. +# Return true iff it does. +# +sub matchword +{ + my( $w ) = @_; + return 1 if /^[qwertyuiop]+$/i; + return 1 if /^[asdfghjkl]+$/i; + return 1 if /^[zxcvbnm]+$/i; + return 0; +} + + +my @result = grep { matchword($_) } @list; + +say "(", join(',', map { qq("$_") } @result), ")"; diff --git a/challenge-207/duncan-c-white/perl/ch-2.pl b/challenge-207/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..7d9eafa3f9 --- /dev/null +++ b/challenge-207/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/perl +# +# Task 2: H-Index +# +# You are given an array of integers containing citations a researcher +# has received for each paper. Write a script to compute the researcher's +# H-Index. For more information please checkout: +# +# https://en.wikipedia.org/wiki/H-index +# +# The H-Index is the largest number h such that h articles have at least +# h citations each. For example, if an author has five publications, with +# 9, 7, 6, 2, and 1 citations (ordered from greatest to least), then the +# author's H-index is 3, because the author has three publications with 3 +# or more citations. However, the author does not have four publications +# with 4 or more citations. +# +# +# Example 1 +# +# Input: @citations = (10,8,5,4,3) +# Output: 4 +# +# Because the 4th publication has 4 citations and the 5th has only 3. +# +# Example 2 +# +# Input: @citations = (25,8,5,3,3) +# Output: 3 +# +# The H-Index is 3 because the fourth paper has only 3 citations. +# +# MY NOTES: also pretty easy. The Wikipedia entry sheds more light: +# +# "First we order the citations from the largest to the lowest value. +# Then, we look for the last position in which citation[h] >= h." +# (This assumes array indexes start at 1, I think). +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.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(min max); + +my $debug=0; +die "Usage: h-index [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV > 0; + +my @list = split( /,/, join(',',@ARGV) ); + +say "list: ", join(',',@list) if $debug; + +@list = sort { $b <=> $a } @list; + +say "sorted list: ", join(',',@list) if $debug; + +my $h = -1; + +foreach my $pos (0..$#list) +{ + $h = $pos+1 if $list[$pos] >= $pos+1; +} + +say $h; |
