From 999265fd360b23c90f2071c5be32ad1e7e2db834 Mon Sep 17 00:00:00 2001 From: dcw Date: Mon, 6 Feb 2023 09:50:40 +0000 Subject: (belatedly did and) committed my solutions to this challenge, both tasks, in C and Perl as usual --- challenge-202/duncan-c-white/C/Makefile | 19 +++ challenge-202/duncan-c-white/C/README | 9 ++ challenge-202/duncan-c-white/C/args.c | 207 ++++++++++++++++++++++++++++ challenge-202/duncan-c-white/C/args.h | 11 ++ challenge-202/duncan-c-white/C/ch-1.c | 84 +++++++++++ challenge-202/duncan-c-white/C/ch-2.c | 90 ++++++++++++ challenge-202/duncan-c-white/C/parseints.c | 114 +++++++++++++++ challenge-202/duncan-c-white/C/parseints.h | 1 + challenge-202/duncan-c-white/C/printarray.c | 39 ++++++ challenge-202/duncan-c-white/C/printarray.h | 1 + challenge-202/duncan-c-white/README | 78 +++++++---- challenge-202/duncan-c-white/perl/ch-1.pl | 93 +++++++++++++ challenge-202/duncan-c-white/perl/ch-2.pl | 103 ++++++++++++++ 13 files changed, 820 insertions(+), 29 deletions(-) create mode 100644 challenge-202/duncan-c-white/C/Makefile create mode 100644 challenge-202/duncan-c-white/C/README create mode 100644 challenge-202/duncan-c-white/C/args.c create mode 100644 challenge-202/duncan-c-white/C/args.h create mode 100644 challenge-202/duncan-c-white/C/ch-1.c create mode 100644 challenge-202/duncan-c-white/C/ch-2.c create mode 100644 challenge-202/duncan-c-white/C/parseints.c create mode 100644 challenge-202/duncan-c-white/C/parseints.h create mode 100644 challenge-202/duncan-c-white/C/printarray.c create mode 100644 challenge-202/duncan-c-white/C/printarray.h create mode 100755 challenge-202/duncan-c-white/perl/ch-1.pl create mode 100755 challenge-202/duncan-c-white/perl/ch-2.pl diff --git a/challenge-202/duncan-c-white/C/Makefile b/challenge-202/duncan-c-white/C/Makefile new file mode 100644 index 0000000000..bf2d32d1a1 --- /dev/null +++ b/challenge-202/duncan-c-white/C/Makefile @@ -0,0 +1,19 @@ +# Makefile rules generated by CB +CC = gcc +CFLAGS = -Wall -g +LDFLAGS = -lm +BUILD = ch-1 ch-2 + +all: $(BUILD) + +clean: + /bin/rm -f $(BUILD) *.o core a.out + +args.o: args.c +ch-1: ch-1.o args.o parseints.o printarray.o +ch-1.o: ch-1.c args.h parseints.h printarray.h +ch-2: ch-2.o args.o parseints.o printarray.o +ch-2.o: ch-2.c args.h parseints.h printarray.h +parseints.o: parseints.c args.h parseints.h printarray.h +printarray.o: printarray.c + diff --git a/challenge-202/duncan-c-white/C/README b/challenge-202/duncan-c-white/C/README new file mode 100644 index 0000000000..e0fb8e8359 --- /dev/null +++ b/challenge-202/duncan-c-white/C/README @@ -0,0 +1,9 @@ +Thought I'd also have a go at translating ch-1.pl and ch-2.pl into C.. + +Both C versions produce near-identical (non-debugging and even debugging) +output to the Perl originals. + +They use 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-202/duncan-c-white/C/args.c b/challenge-202/duncan-c-white/C/args.c new file mode 100644 index 0000000000..d4a2d38b9a --- /dev/null +++ b/challenge-202/duncan-c-white/C/args.c @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include + + +bool debug = false; + + +// process_flag_noarg( name, argc, argv ); +// Process the -d flag, and check that there are no +// remaining arguments. +void process_flag_noarg( char *name, int argc, char **argv ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != 0 ) + { + fprintf( stderr, "Usage: %s [-d]\n", name ); + exit(1); + } +} + + +// int argno = process_flag_n_args( name, argc, argv, n, argmsg ); +// Process the -d flag, and check that there are exactly +// n remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_args( char *name, int argc, char **argv, int n, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != n ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Exactly %d " + "arguments needed\n", name, argmsg, n ); + exit(1); + } + return arg; +} + + +// int argno = process_flag_n_m_args( name, argc, argv, min, max, argmsg ); +// Process the -d flag, and check that there are between +// min and max remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_m_args( char *name, int argc, char **argv, int min, int max, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < min || left > max ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Between %d and %d " + "arguments needed\n", name, argmsg, min, max ); + exit(1); + } + return arg; +} + + +// process_onenumarg_default( name, argc, argv, defvalue, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument (or no arguments, in which case +// we use the defvalue), putting it into n +void process_onenumarg_default( char *name, int argc, char **argv, int defvalue, int *n ) +{ + char argmsg[100]; + sprintf( argmsg, "[int default %d]", defvalue ); + int arg = process_flag_n_m_args( name, argc, argv, 0, 1, argmsg ); + + *n = arg == argc ? defvalue : atoi( argv[arg] ); +} + + +// process_onenumarg( name, argc, argv, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument, putting it into n +void process_onenumarg( char *name, int argc, char **argv, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 1, "int" ); + + // argument is in argv[arg] + *n = atoi( argv[arg] ); +} + + +// process_twonumargs( name, argc, argv, &m, &n ); +// Process the -d flag, and check that there are 2 +// remaining numeric arguments, putting them into m and n +void process_twonumargs( char *name, int argc, char **argv, int *m, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 2, "int" ); + + // arguments are in argv[arg] and argv[arg+1] + *m = atoi( argv[arg++] ); + *n = atoi( argv[arg] ); +} + + +// process_twostrargs() IS DEPRECATED: use process_flag_n_m_args() instead + + +// int arr[100]; +// int nel = process_listnumargs( name, argc, argv, arr, 100 ); +// Process the -d flag, and check that there are >= 2 +// remaining numeric arguments, putting them into arr[0..nel-1] +// and returning nel. +int process_listnumargs( char *name, int argc, char **argv, int *arr, int maxel ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < 2 ) + { + fprintf( stderr, "Usage: %s [-d] list_of_numeric_args\n", name ); + exit(1); + } + if( left > maxel ) + { + fprintf( stderr, "%s: more than %d args\n", name, maxel ); + exit(1); + } + + // elements are in argv[arg], argv[arg+1]... + + if( debug ) + { + printf( "debug: remaining arguments are in arg=%d, " + "firstn=%s, secondn=%s..\n", + arg, argv[arg], argv[arg+1] ); + } + + int nel = 0; + for( int i=arg; i +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +// bool isodd = isodd( x ); +// Return 1 iff x is odd; else 0. +// +bool isodd( int x ) +{ + return x % 2 == 1; +} + + +// +// bool hasodds = has3odds( list, nel ); +// Return true iff list has 3 consecutive odd numbers; else false. +// +bool has3odds( int *list, int nel ) +{ + for( int i=0; i 2 elements\n" ); + exit(1); + } + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + bool hasodds = has3odds( list, nel ); + printf( "%d\n", (int)hasodds ); + + free( list ); + + return 0; +} diff --git a/challenge-202/duncan-c-white/C/ch-2.c b/challenge-202/duncan-c-white/C/ch-2.c new file mode 100644 index 0000000000..af2845861e --- /dev/null +++ b/challenge-202/duncan-c-white/C/ch-2.c @@ -0,0 +1,90 @@ +// +// Task 2: Widest Valley +// +// C version. +// + +#include +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +// int len = find_valley( spos, list, nel ); +// find the leftmost longest sequence of one or more adjacent numbers +// where initially the numbers go down (or stay the same) and then they +// go up (or stay the same). +// +int find_valley( int spos, int *list, int nel ) +{ + int curr = list[spos]; + int p = spos+1; + for( ; p= curr; curr=list[p++] ) + { + } + if( debug ) + { + printf( "debug: found valley from pos %d to pos %d\n", + spos, p-1 ); + } + return p-spos; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "widest-valley", argc, argv, + 1, 1000, "intlist" ); + int nel; + int *list = parse_int_args( argc, argv, argno, &nel ); + + if( nel == 0 ) + { + fprintf( stderr, "widest-valley: need a list of > 0 elements\n" ); + exit(1); + } + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + int maxw = 0; + int maxw_spos = 0; + + for( int spos=0; spos maxw ) + { + if( debug ) + { + printf( "debug: found new widest valley " + "starting at %d: width %d\n", spos, w ); + } + maxw = w; + maxw_spos = spos; + } + } + + for( int i=0; i0 ) printf( ", " ); + printf( "%d", list[maxw_spos+i] ); + } + putchar( '\n' ); + + free( list ); + return 0; +} diff --git a/challenge-202/duncan-c-white/C/parseints.c b/challenge-202/duncan-c-white/C/parseints.c new file mode 100644 index 0000000000..0fb9985633 --- /dev/null +++ b/challenge-202/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 +#include +#include +#include +#include +#include + +#include "args.h" +#include "printarray.h" +#include "parseints.h" + +typedef struct +{ + int nel; // current number of elements + int maxel; // maximum number of elements allocated + int *list; // malloc()d list of integers +} intlist; + + +// +// intlist il.. then initialize il.. then: +// add_one( element, &il ); +// +static void add_one( int x, intlist *p ) +{ + if( p->nel > p->maxel ) + { + p->maxel += 128; + p->list = realloc( p->list, p->maxel ); + assert( p->list ); + } + #if 0 + if( debug ) + { + printf( "PIA: appending %d to result at " + "pos %d\n", x, p->nel ); + } + #endif + p->list[p->nel++] = x; +} + + +// +// intlist il.. then initialize il.. then: +// add_one_arg( argstr, &il ); +// +static void add_one_arg( char *argstr, intlist *p ) +{ + int x; + if( !check_unsigned_int(argstr,&x) ) + { + fprintf( stderr, "PIA: arg %s must be +int\n", argstr ); + exit(1); + } + add_one( x, p ); +} + + +// +// int nel; +// int *ilist = parse_int_args( argc, argv, argno, &nel ); +// process all arguments argv[argno..argc-1], extracting either +// single ints or comma-separated lists of ints from those arguments, +// accumulate all integers in a dynarray list, storing the total number +// of elements in nel. This list must be freed by the caller. +// Note that the list of elements used to be terminated by a -1 value, +// but I've commented this out from now on. +// +int *parse_int_args( int argc, char **argv, int argno, int *nel ) +{ + int *result = malloc( 128 * sizeof(int) ); + assert( result ); + intlist il = { 0, 128, result }; + + #if 0 + if( debug ) + { + printf( "PIA: parsing ints from args %d..%d\n", argno, argc-1 ); + } + #endif + for( int i=argno; i +#include + + +// print_int_array( maxw, nelements, results[], sep, outfile ); +// format results[0..nelements-1] as a separated +// list onto outfile with lines <= maxw chars long. +// produces a whole number of lines of output - without the trailing '\n' +void print_int_array( int maxw, int nel, int *results, char sep, FILE *out ) +{ + int linelen = 0; + for( int i=0; i maxw ) + { + fputc( '\n', out ); + linelen = 0; + } else if( i>0 ) + { + fputc( ' ', out ); + linelen++; + } + + linelen += len; + fprintf( out, "%s", buf ); + if( i0 ) + //{ + // fputc( '\n', out ); + //} +} diff --git a/challenge-202/duncan-c-white/C/printarray.h b/challenge-202/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-202/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-202/duncan-c-white/README b/challenge-202/duncan-c-white/README index 28f5718a9a..c6dd8b8b1c 100644 --- a/challenge-202/duncan-c-white/README +++ b/challenge-202/duncan-c-white/README @@ -1,52 +1,72 @@ -Task 1: Missing Numbers +Task 1: Consecutive Odds -You are given an array of unique numbers. Write a script to find out -all missing numbers in the range 0..$n where $n is the array size. +You are given an array of integers. -Example 1 +Write a script to print 1 if there are THREE consecutive odds in the +given array otherwise print 0. - Input: @array = (0,1,3) - Output: 2 +Example 1 - The array size i.e. total element count is 3, so the range is 0..3. - The missing number is 2 in the given array. +Input: @array = (1,5,3,6) +Output: 1 Example 2 - Input: @array = (0,1) - Output: 2 +Input: @array = (2,6,3,5) +Output: 0 + +Example 3 + +Input: @array = (1,2,3,4) +Output: 0 - The array size is 2, therefore the range is 0..2. - The missing number is 2. +Example 4 -MY NOTES: pretty easy. +Input: @array = (2,3,5,7) +Output: 1 + +MY NOTES: 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 the translation) -Task 2: Penny Piles +Task 2: Widest Valley + +Given a profile as a list of altitudes, return the leftmost widest +valley. A valley is defined as a subarray of the profile consisting +of two parts: the first part is non-increasing and the second part is +non-decreasing. Either part can be empty. + +Example 1 + +Input: 1, 5, 5, 2, 8 +Output: 5, 5, 2, 8 + +Example 2 + +Input: 2, 6, 8, 5 +Output: 2, 6, 8 + +Example 3 -You are given an integer, $n > 0. +Input: 9, 8, 13, 13, 2, 2, 15, 17 +Output: 13, 13, 2, 2, 15, 17 -Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right. -Example +Example 4 -Input: $n = 5 -Output: 7 +Input: 2, 1, 2, 1, 3 +Output: 2, 1, 2 -Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +Example 5 - 1 1 1 1 1 - 1 1 1 2 - 1 2 2 - 1 1 3 - 2 3 - 1 4 - 5 +Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +Output: 3, 3, 2, 1, 2, 3, 3 -MY NOTES: not quite so easy, but doable. Sounds recursive to me. -Let's produce the array of answers too.. +MY NOTES: also quite easy, once we remove the negative "non-increasing" +language and realise we mean "find the leftmost longest sequence of one +or more adjacent numbers where initially the numbers go down (or stay +the same) and then they go up (or stay the same)." GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C (look in the C directory for the translation) diff --git a/challenge-202/duncan-c-white/perl/ch-1.pl b/challenge-202/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..3536e2a0b7 --- /dev/null +++ b/challenge-202/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,93 @@ +#!/usr/bin/perl +# +# Task 1: Consecutive Odds +# +# You are given an array of integers. +# +# Write a script to print 1 if there are THREE consecutive odds in the +# given array otherwise print 0. +# +# Example 1 +# +# Input: @array = (1,5,3,6) +# Output: 1 +# +# Example 2 +# +# Input: @array = (2,6,3,5) +# Output: 0 +# +# Example 3 +# +# Input: @array = (1,2,3,4) +# Output: 0 +# +# Example 4 +# +# Input: @array = (2,3,5,7) +# Output: 1 +# +# MY NOTES: 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 the translation) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Data::Dumper; + +my $debug=0; +die "Usage: 3-consec-odds [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my @list = split( /,/, join(',',@ARGV) ); + +die "3-consec-odds: need at least 3 ints in list\n" unless @list>2; + +=pod + +=head2 my $isodd = isodd( $x ); + +Return 1 iff $x is odd; else 0. + +=cut +sub isodd +{ + my( $x ) = @_; + return $x % 2 == 1 ? 1 : 0; +} + + +=pod + +=head2 my $has3odds = has3odds( @list ); + +Return 1 iff @list has 3 consecutive odd numbers; else 0. + +=cut +sub has3odds +{ + my( @list ) = @_; + my $n = @list; + + for( my $spos=0; $spos<$n-2; $spos++ ) + { + next unless isodd( $list[$spos] ); + say "debug: found odd element $list[$spos] at pos $spos" + if $debug; + if( isodd( $list[$spos+1] ) && isodd( $list[$spos+2] ) ) + { + say "debug: found 3 consec odd elements $list[$spos], ". + "$list[$spos+1], $list[$spos+2] starting at ". + "pos $spos" if $debug; + return 1; + } + } + return 0; +} + +my $has3odds = has3odds( @list ); +say $has3odds; diff --git a/challenge-202/duncan-c-white/perl/ch-2.pl b/challenge-202/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..4da1d39a78 --- /dev/null +++ b/challenge-202/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl +# +# Task 2: Widest Valley +# +# Given a profile as a list of altitudes, return the leftmost widest +# valley. A valley is defined as a subarray of the profile consisting +# of two parts: the first part is non-increasing and the second part is +# non-decreasing. Either part can be empty. +# +# Example 1 +# +# Input: 1, 5, 5, 2, 8 +# Output: 5, 5, 2, 8 +# +# Example 2 +# +# Input: 2, 6, 8, 5 +# Output: 2, 6, 8 +# +# Example 3 +# +# Input: 9, 8, 13, 13, 2, 2, 15, 17 +# Output: 13, 13, 2, 2, 15, 17 +# +# Example 4 +# +# Input: 2, 1, 2, 1, 3 +# Output: 2, 1, 2 +# +# Example 5 +# +# Input: 1, 3, 3, 2, 1, 2, 3, 3, 2 +# Output: 3, 3, 2, 1, 2, 3, 3 +# +# MY NOTES: also quite easy, once we remove the negative "non-increasing" +# language and realise we mean "find the leftmost longest sequence of one +# or more adjacent numbers where initially the numbers go down (or stay +# the same) and then they go up (or stay the same)." +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl +# into C (look in the C directory for the translation) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Function::Parameters; +use Data::Dumper; + +my $debug=0; +die "Usage: widest-valley [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV > 0; + +my @list = split( /,/, join(',',@ARGV) ); + +my $n = @list; + +die "widest-valley: need at least 1 int in list\n" unless $n>0; + + +=pod + +=head2 my $len = find_valley( $spos, @list ); + +find the leftmost longest sequence of one or more adjacent numbers +where initially the numbers go down (or stay the same) and then they +go up (or stay the same). + +=cut +fun find_valley( $spos, @list ) +{ + my $n = @list; + my $curr = $list[$spos]; + my $p = $spos+1; + for( ; $p<$n && $list[$p] <= $curr; $curr=$list[$p++] ) + { + } + for( ; $p<$n && $list[$p] >= $curr; $curr=$list[$p++] ) + { + } + my $p1 = $p-1; + say "debug: found valley from pos $spos to pos $p1" if $debug; + return $p-$spos; +} + + +my $maxw = 0; +my $maxw_spos = 0; + +for( my $spos=0; $spos<$n; $spos++ ) +{ + my $w = find_valley( $spos, @list ); + if( $w > $maxw ) + { + say "debug: found new widest valley starting at $spos: width $w" if $debug; + $maxw = $w; + $maxw_spos = $spos; + } +} + + +say join( ', ', @list[$maxw_spos..$maxw_spos+$maxw-1] ); -- cgit From 5cc0824bfab2dd34ae48c3144d017f0d2b3b400a Mon Sep 17 00:00:00 2001 From: dcw Date: Sun, 12 Feb 2023 23:16:36 +0000 Subject: imported my solutions to this week's tasks, in Perl and C as usual --- challenge-203/duncan-c-white/C/Makefile | 19 +++ challenge-203/duncan-c-white/C/README | 13 ++ challenge-203/duncan-c-white/C/args.c | 207 ++++++++++++++++++++++++++++ challenge-203/duncan-c-white/C/args.h | 11 ++ challenge-203/duncan-c-white/C/ch-1.c | 78 +++++++++++ challenge-203/duncan-c-white/C/ch-2.c | 174 +++++++++++++++++++++++ challenge-203/duncan-c-white/C/parseints.c | 114 +++++++++++++++ challenge-203/duncan-c-white/C/parseints.h | 1 + challenge-203/duncan-c-white/C/printarray.c | 39 ++++++ challenge-203/duncan-c-white/C/printarray.h | 1 + challenge-203/duncan-c-white/README | 86 ++++++++---- challenge-203/duncan-c-white/perl/ch-1.pl | 92 +++++++++++++ challenge-203/duncan-c-white/perl/ch-2.pl | 81 +++++++++++ challenge-203/duncan-c-white/perl/ch-2a.pl | 130 +++++++++++++++++ 14 files changed, 1019 insertions(+), 27 deletions(-) create mode 100644 challenge-203/duncan-c-white/C/Makefile create mode 100644 challenge-203/duncan-c-white/C/README create mode 100644 challenge-203/duncan-c-white/C/args.c create mode 100644 challenge-203/duncan-c-white/C/args.h create mode 100644 challenge-203/duncan-c-white/C/ch-1.c create mode 100644 challenge-203/duncan-c-white/C/ch-2.c create mode 100644 challenge-203/duncan-c-white/C/parseints.c create mode 100644 challenge-203/duncan-c-white/C/parseints.h create mode 100644 challenge-203/duncan-c-white/C/printarray.c create mode 100644 challenge-203/duncan-c-white/C/printarray.h create mode 100755 challenge-203/duncan-c-white/perl/ch-1.pl create mode 100755 challenge-203/duncan-c-white/perl/ch-2.pl create mode 100755 challenge-203/duncan-c-white/perl/ch-2a.pl diff --git a/challenge-203/duncan-c-white/C/Makefile b/challenge-203/duncan-c-white/C/Makefile new file mode 100644 index 0000000000..bf2d32d1a1 --- /dev/null +++ b/challenge-203/duncan-c-white/C/Makefile @@ -0,0 +1,19 @@ +# Makefile rules generated by CB +CC = gcc +CFLAGS = -Wall -g +LDFLAGS = -lm +BUILD = ch-1 ch-2 + +all: $(BUILD) + +clean: + /bin/rm -f $(BUILD) *.o core a.out + +args.o: args.c +ch-1: ch-1.o args.o parseints.o printarray.o +ch-1.o: ch-1.c args.h parseints.h printarray.h +ch-2: ch-2.o args.o parseints.o printarray.o +ch-2.o: ch-2.c args.h parseints.h printarray.h +parseints.o: parseints.c args.h parseints.h printarray.h +printarray.o: printarray.c + diff --git a/challenge-203/duncan-c-white/C/README b/challenge-203/duncan-c-white/C/README new file mode 100644 index 0000000000..312de0114c --- /dev/null +++ b/challenge-203/duncan-c-white/C/README @@ -0,0 +1,13 @@ +Thought I'd also have a go at translating ch-1.pl and ch-2a.pl into C.. + +Both C versions produce near-identical (non-debugging and even debugging) +output to the Perl originals. + +The C translation of ch-2a.pl was quite hard to write, mainly on storage +allocation, to minimise that I changed it to a per-list callback +(from building a list of paths) + +They 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-203/duncan-c-white/C/args.c b/challenge-203/duncan-c-white/C/args.c new file mode 100644 index 0000000000..d4a2d38b9a --- /dev/null +++ b/challenge-203/duncan-c-white/C/args.c @@ -0,0 +1,207 @@ +#include +#include +#include +#include +#include +#include + + +bool debug = false; + + +// process_flag_noarg( name, argc, argv ); +// Process the -d flag, and check that there are no +// remaining arguments. +void process_flag_noarg( char *name, int argc, char **argv ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != 0 ) + { + fprintf( stderr, "Usage: %s [-d]\n", name ); + exit(1); + } +} + + +// int argno = process_flag_n_args( name, argc, argv, n, argmsg ); +// Process the -d flag, and check that there are exactly +// n remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_args( char *name, int argc, char **argv, int n, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left != n ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Exactly %d " + "arguments needed\n", name, argmsg, n ); + exit(1); + } + return arg; +} + + +// int argno = process_flag_n_m_args( name, argc, argv, min, max, argmsg ); +// Process the -d flag, and check that there are between +// min and max remaining arguments, return the index position of the first +// argument. If not, generate a fatal Usage error using the argmsg. +// +int process_flag_n_m_args( char *name, int argc, char **argv, int min, int max, char *argmsg ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < min || left > max ) + { + fprintf( stderr, "Usage: %s [-d] %s\n Between %d and %d " + "arguments needed\n", name, argmsg, min, max ); + exit(1); + } + return arg; +} + + +// process_onenumarg_default( name, argc, argv, defvalue, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument (or no arguments, in which case +// we use the defvalue), putting it into n +void process_onenumarg_default( char *name, int argc, char **argv, int defvalue, int *n ) +{ + char argmsg[100]; + sprintf( argmsg, "[int default %d]", defvalue ); + int arg = process_flag_n_m_args( name, argc, argv, 0, 1, argmsg ); + + *n = arg == argc ? defvalue : atoi( argv[arg] ); +} + + +// process_onenumarg( name, argc, argv, &n ); +// Process the -d flag, and check that there is a single +// remaining numeric argument, putting it into n +void process_onenumarg( char *name, int argc, char **argv, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 1, "int" ); + + // argument is in argv[arg] + *n = atoi( argv[arg] ); +} + + +// process_twonumargs( name, argc, argv, &m, &n ); +// Process the -d flag, and check that there are 2 +// remaining numeric arguments, putting them into m and n +void process_twonumargs( char *name, int argc, char **argv, int *m, int *n ) +{ + int arg = process_flag_n_args( name, argc, argv, 2, "int" ); + + // arguments are in argv[arg] and argv[arg+1] + *m = atoi( argv[arg++] ); + *n = atoi( argv[arg] ); +} + + +// process_twostrargs() IS DEPRECATED: use process_flag_n_m_args() instead + + +// int arr[100]; +// int nel = process_listnumargs( name, argc, argv, arr, 100 ); +// Process the -d flag, and check that there are >= 2 +// remaining numeric arguments, putting them into arr[0..nel-1] +// and returning nel. +int process_listnumargs( char *name, int argc, char **argv, int *arr, int maxel ) +{ + int arg=1; + if( argc>1 && strcmp( argv[arg], "-d" ) == 0 ) + { + debug = true; + arg++; + } + + int left = argc-arg; + if( left < 2 ) + { + fprintf( stderr, "Usage: %s [-d] list_of_numeric_args\n", name ); + exit(1); + } + if( left > maxel ) + { + fprintf( stderr, "%s: more than %d args\n", name, maxel ); + exit(1); + } + + // elements are in argv[arg], argv[arg+1]... + + if( debug ) + { + printf( "debug: remaining arguments are in arg=%d, " + "firstn=%s, secondn=%s..\n", + arg, argv[arg], argv[arg+1] ); + } + + int nel = 0; + for( int i=arg; i +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +// int n = count_special_quads( list[], nel ); +// Find all special quads in list[0..nel-1], and count them. +// In debug mode, list all the special quads we find. +int count_special_quads( int *list, int nel ) +{ + int result = 0; + for( int a = 0; a < nel-3; a++ ) + { + for( int b = a+1; b < nel-2; b++ ) + { + for( int c = b+1; c < nel-1; c++ ) + { + int sum = list[a] + list[b] + list[c]; + for( int d = c+1; d < nel; d++ ) + { + if( list[d] == sum ) + { + if( debug ) + { + printf( "debug: found sq %d-%d-%d-%d: %d+%d+%d==%d\n", + a, b, c, d, list[a], list[b], list[c], list[d] ); + } + result++; + } + } + } + } + } + return result; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "3-consec-odds", argc, argv, + 1, 1000, "intlist" ); + + int nel; + int *list = parse_int_args( argc, argv, argno, &nel ); + + if( nel < 4 ) + { + fprintf( stderr, "special-quads: need a list of > 3 elements\n" ); + exit(1); + } + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + int n = count_special_quads( list, nel ); + printf( "%d\n", n ); + + free( list ); + + return 0; +} diff --git a/challenge-203/duncan-c-white/C/ch-2.c b/challenge-203/duncan-c-white/C/ch-2.c new file mode 100644 index 0000000000..c7b307448a --- /dev/null +++ b/challenge-203/duncan-c-white/C/ch-2.c @@ -0,0 +1,174 @@ +// +// Task 2: Copy Directory +// +// C version. +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "args.h" + + +typedef char filename[256]; + + +// +// make_path( path ); +// mkdir path (and all it's parental dirs +void make_path( char *path ) +{ + assert( strlen(path)>1 ); + char *p = strchr(path+1,'/'); + if( p != NULL ) + { + *p = '\0'; + if( debug ) printf( "mkdir %s\n", path ); + mkdir( path, 0700 ); + *p = '/'; + while( (p = strchr(p+1, '/')) != NULL ) + { + *p = '\0'; + if( debug ) printf( "mkdir %s\n", path ); + mkdir( path, 0700 ); + *p = '/'; + } + } + if( debug ) printf( "mkdir %s\n", path ); + mkdir( path, 0700 ); + //exit(1); +} + + +// +// bool isd = isdir( path ); +// Return true iff path is a directory; false otherwise +// +bool isdir( char *path ) +{ + struct stat buf; + if( stat(path,&buf) == 0 ) + { + if( S_ISDIR(buf.st_mode) ) + { + return true; + } + } + return false; +} + + +typedef void (*callback)( char *dir ); + + +int strcompare( const void *a, const void *b ) +{ + return strcmp( (char *)a, (char *)b ); +} + + +#define MAXSUBDIRSPERDIR 1024 + + +// +// find_dirs( srcpath, callback ); +// Recursively find all directories inside srcpath, +// calling callback( dir ) for each one. +// +void find_dirs( char *srcpath, callback cb ) +{ + if( ! isdir( srcpath ) ) return; + DIR *dh = opendir( srcpath ); + if( dh == NULL ) + { + fprintf( stderr, "Can't opendir %s\n", srcpath ); + return; + } + struct dirent *dinfo; + filename subdirs[MAXSUBDIRSPERDIR]; + int ndirs = 0; + while( (dinfo = readdir( dh )) != NULL ) + { + char *name = dinfo->d_name; + filename dname; + if( strcmp(name,".")==0 || strcmp(name,"..")==0 ) continue; + + sprintf( dname, "%s/%s", srcpath, name ); + if( isdir(dname) ) + { + strcpy( subdirs[ndirs++], dname ); + assert( ndirs +#include +#include +#include +#include +#include + +#include "args.h" +#include "printarray.h" +#include "parseints.h" + +typedef struct +{ + int nel; // current number of elements + int maxel; // maximum number of elements allocated + int *list; // malloc()d list of integers +} intlist; + + +// +// intlist il.. then initialize il.. then: +// add_one( element, &il ); +// +static void add_one( int x, intlist *p ) +{ + if( p->nel > p->maxel ) + { + p->maxel += 128; + p->list = realloc( p->list, p->maxel ); + assert( p->list ); + } + #if 0 + if( debug ) + { + printf( "PIA: appending %d to result at " + "pos %d\n", x, p->nel ); + } + #endif + p->list[p->nel++] = x; +} + + +// +// intlist il.. then initialize il.. then: +// add_one_arg( argstr, &il ); +// +static void add_one_arg( char *argstr, intlist *p ) +{ + int x; + if( !check_unsigned_int(argstr,&x) ) + { + fprintf( stderr, "PIA: arg %s must be +int\n", argstr ); + exit(1); + } + add_one( x, p ); +} + + +// +// int nel; +// int *ilist = parse_int_args( argc, argv, argno, &nel ); +// process all arguments argv[argno..argc-1], extracting either +// single ints or comma-separated lists of ints from those arguments, +// accumulate all integers in a dynarray list, storing the total number +// of elements in nel. This list must be freed by the caller. +// Note that the list of elements used to be terminated by a -1 value, +// but I've commented this out from now on. +// +int *parse_int_args( int argc, char **argv, int argno, int *nel ) +{ + int *result = malloc( 128 * sizeof(int) ); + assert( result ); + intlist il = { 0, 128, result }; + + #if 0 + if( debug ) + { + printf( "PIA: parsing ints from args %d..%d\n", argno, argc-1 ); + } + #endif + for( int i=argno; i +#include + + +// print_int_array( maxw, nelements, results[], sep, outfile ); +// format results[0..nelements-1] as a separated +// list onto outfile with lines <= maxw chars long. +// produces a whole number of lines of output - without the trailing '\n' +void print_int_array( int maxw, int nel, int *results, char sep, FILE *out ) +{ + int linelen = 0; + for( int i=0; i maxw ) + { + fputc( '\n', out ); + linelen = 0; + } else if( i>0 ) + { + fputc( ' ', out ); + linelen++; + } + + linelen += len; + fprintf( out, "%s", buf ); + if( i0 ) + //{ + // fputc( '\n', out ); + //} +} diff --git a/challenge-203/duncan-c-white/C/printarray.h b/challenge-203/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-203/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-203/duncan-c-white/README b/challenge-203/duncan-c-white/README index 28f5718a9a..22c27ddfb8 100644 --- a/challenge-203/duncan-c-white/README +++ b/challenge-203/duncan-c-white/README @@ -1,52 +1,84 @@ -Task 1: Missing Numbers +Task 1: Special Quadruplets -You are given an array of unique numbers. Write a script to find out -all missing numbers in the range 0..$n where $n is the array size. +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 Example 1 - Input: @array = (0,1,3) - Output: 2 + Input: @nums = (1,2,3,6) + Output: 1 - The array size i.e. total element count is 3, so the range is 0..3. - The missing number is 2 in the given array. + Since the only special quadruplets found is $nums[0] + $nums[1] + $nums[2] == $nums[3]. Example 2 - Input: @array = (0,1) - Output: 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] + +Example 3 - The array size is 2, therefore the range is 0..2. - The missing number is 2. + Input: @nums = (3,3,6,4,5) + Output: 0 -MY NOTES: pretty easy. +MY NOTES: seems pretty easy. condition (2) implies 4 for loops, a, b, c and d 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: Penny Piles +Task 2: Copy Directory +Submitted by: Julien Fiegehenn -You are given an integer, $n > 0. +You are given path to two folders, $source and $target. + +Write a script that recursively copy the directory from $source to +$target except any files. -Write a script to determine the number of ways of putting $n pennies in a row of piles of ascending heights from left to right. Example -Input: $n = 5 -Output: 7 + Input: $source = '/a/b/c' and $target = '/x/y' + + Source directory structure: + +|___ a +| |___ b +| |___ c +| |___ 1 +| | |___ 1.txt +| |___ 2 +| | |___ 2.txt +| |___ 3 +| | |___ 3.txt +| |___ 4 +| |___ 5 +| | |___ 5.txt + +Target directory structure: + +|___ x +| |___ y + +Expected Result: -Since $n=5, there are 7 ways of stacking 5 pennies in ascending piles: +|___ x +| |___ y +| |___ 1 +| |___ 2 +| |___ 3 +| |___ 4 +| |___ 5 - 1 1 1 1 1 - 1 1 1 2 - 1 2 2 - 1 1 3 - 2 3 - 1 4 - 5 -MY NOTES: not quite so easy, but doable. Sounds recursive to me. -Let's produce the array of answers too.. +MY NOTES: also quite easy, especially using File::Find. GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl into C (look in the C directory for the translation) diff --git a/challenge-203/duncan-c-white/perl/ch-1.pl b/challenge-203/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..0e824267a9 --- /dev/null +++ b/challenge-203/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,92 @@ +#!/usr/bin/perl +# +# Task 1: Special Quadruplets +# +# 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 +# +# Example 1 +# +# Input: @nums = (1,2,3,6) +# 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] +# +# Example 3 +# +# Input: @nums = (3,3,6,4,5) +# Output: 0 +# +# MY NOTES: seems pretty easy. condition (2) implies 4 for loops, a, b, c and d +# +# 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: special-quads [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +=pod + +=head2 my $n = count_special_quads( @list ); + +Find all special quads in @list, and count them. +In debug mode, list all the special quads we find. + +=cut +sub count_special_quads +{ + my( @list ) = @_; + my $nel = @list; + my $result = 0; + for( my $a = 0; $a < $nel-3; $a++ ) + { + for( my $b = $a+1; $b < $nel-2; $b++ ) + { + for( my $c = $b+1; $c < $nel-1; $c++ ) + { + my $sum = $list[$a] + $list[$b] + $list[$c]; + for( my $d = $c+1; $d < $nel; $d++ ) + { + next unless $list[$d] == $sum; + if( $debug ) + { + say "found sq $a-$b-$c-$d: $list[$a]+$list[$b]+$list[$c]==$list[$d]"; + } + $result++; + } + } + } + } + return $result; +} + + + +my @list = split( /,/, join(',',@ARGV) ); + +die "special-quads: need at least 4 ints in list\n" unless @list>3; + +my $n = count_special_quads( @list ); +say $n; diff --git a/challenge-203/duncan-c-white/perl/ch-2.pl b/challenge-203/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..2557670b66 --- /dev/null +++ b/challenge-203/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl +# +# Task 2: Copy Directory +# Submitted by: Julien Fiegehenn +# +# You are given path to two folders, $source and $target. +# +# Write a script that recursively copy the directory from $source to +# $target except any files. +# +# Example +# +# Input: $source = '/a/b/c' and $target = '/x/y' +# +# Source directory structure: +# +# |___ a +# | |___ b +# | |___ c +# | |___ 1 +# | | |___ 1.txt +# | |___ 2 +# | | |___ 2.txt +# | |___ 3 +# | | |___ 3.txt +# | |___ 4 +# | |___ 5 +# | | |___ 5.txt +# +# Target directory structure: +# +# |___ x +# | |___ y +# +# Expected Result: +# +# |___ x +# | |___ y +# | |___ 1 +# | |___ 2 +# | |___ 3 +# | |___ 4 +# | |___ 5 +# +# MY NOTES: also quite easy, especially using File::Find. even using +# readdir() and recursion isn't too bad. Might try both.. +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl +# into C (look in the C directory for the translation) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Function::Parameters; +use File::Find; +no warnings 'File::Find'; +use File::Path qw(make_path); +use Data::Dumper; + +my $debug=0; +die "Usage: copy-dir-structure [--debug] srcpath dstpath\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV == 2; + +my( $srcpath, $dstpath ) = @ARGV; + +die "copy-dir-structure: no src path $srcpath\n" unless -d $srcpath; +die "copy-dir-structure: dst path $dstpath exists\n" if -d $dstpath; + +make_path( $dstpath ); + +die "copy-dir-structure: no dst path $dstpath after make_path!\n" + unless -d $dstpath; + +find( {wanted=> sub { + return unless -d; + s|^$srcpath|$dstpath|; + mkdir( $_ ) unless -d; + say if $debug; +}, no_chdir=>1}, $srcpath ); diff --git a/challenge-203/duncan-c-white/perl/ch-2a.pl b/challenge-203/duncan-c-white/perl/ch-2a.pl new file mode 100755 index 0000000000..c06ddaa9fe --- /dev/null +++ b/challenge-203/duncan-c-white/perl/ch-2a.pl @@ -0,0 +1,130 @@ +#!/usr/bin/perl +# +# Task 2: Copy Directory +# Submitted by: Julien Fiegehenn +# +# You are given path to two folders, $source and $target. +# +# Write a script that recursively copy the directory from $source to +# $target except any files. +# +# This version (2a) does it without using File::Find.. +# +# Example +# +# Input: $source = '/a/b/c' and $target = '/x/y' +# +# Source directory structure: +# +# |___ a +# | |___ b +# | |___ c +# | |___ 1 +# | | |___ 1.txt +# | |___ 2 +# | | |___ 2.txt +# | |___ 3 +# | | |___ 3.txt +# | |___ 4 +# | |___ 5 +# | | |___ 5.txt +# +# Target directory structure: +# +# |___ x +# | |___ y +# +# Expected Result: +# +# |___ x +# | |___ y +# | |___ 1 +# | |___ 2 +# | |___ 3 +# | |___ 4 +# | |___ 5 +# +# MY NOTES: also quite easy, especially using File::Find. even using +# readdir() and recursion isn't too bad. Might try both.. +# +# GUEST LANGUAGE: As a bonus, I also had a go at translating ch-2.pl +# into C (look in the C directory for the translation) +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +use Function::Parameters; +use File::Path qw(make_path); +use Data::Dumper; + +my $debug=0; +die "Usage: copy-dir-structure [--debug] srcpath dstpath\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV == 2; + +my( $srcpath, $dstpath ) = @ARGV; + +die "copy-dir-structure: no src path $srcpath\n" unless -d $srcpath; +die "copy-dir-structure: dst path $dstpath exists\n" if -d $dstpath; + + +=pod + +=head2 find_dirs_rec( $srcpath, $dirs ); + +Recursively Find all directories inside $srcpath, accumulating in @$dirs. + +=cut +fun find_dirs_rec( $srcpath, $dirs ) +{ + return unless -d $srcpath; + my $dh; + unless( opendir($dh, $srcpath) ) + { + warn "Can't opendir $srcpath: $!\n"; + return; + } + my @subdirs = map { "$srcpath/$_" } + grep { $_ ne "." && $_ ne ".." && -d "$srcpath/$_" } + sort readdir($dh); + closedir $dh; + + #say "read: $_" for @subdirs; + + foreach my $dir (@subdirs) + { + push @$dirs, $dir; + #say "adding $dir" if $debug; + find_dirs_rec( $dir, $dirs ); + } +} + + +=pod + +=head2 +my @dirs = find_dirs( $srcpath ); + +Find all directories inside $srcpath; return a list of paths. + +=cut +fun find_dirs( $srcpath ) +{ + my $dirs = []; + find_dirs_rec( $srcpath, $dirs ); + return @$dirs; +} + + +make_path( $dstpath ); + +die "copy-dir-structure: no dst path $dstpath after make_path!\n" + unless -d $dstpath; + +my @dirs = map { s|^$srcpath/|$dstpath/|; $_ } find_dirs( $srcpath ); +if( $debug ) +{ + say "about to mkdir $_" for @dirs; +} +mkdir( $_ ) for @dirs; -- cgit