From 85f41d815fb2516f8fa1534cf60c520ab3243f5e Mon Sep 17 00:00:00 2001 From: dcw Date: Mon, 23 Jan 2023 01:13:12 +0000 Subject: belatedly incorporated my solutions to challenge 200, in Perl and C, and fixed a bug in both challenge 199 C solutions (off by one error in my dynamic arrays) --- challenge-199/duncan-c-white/C/ch-1.c | 4 +- challenge-199/duncan-c-white/C/ch-2.c | 8 +- challenge-200/duncan-c-white/C/Makefile | 19 +++ challenge-200/duncan-c-white/C/README | 9 ++ challenge-200/duncan-c-white/C/args.c | 207 ++++++++++++++++++++++++++++ challenge-200/duncan-c-white/C/args.h | 11 ++ challenge-200/duncan-c-white/C/ch-1.c | 140 +++++++++++++++++++ challenge-200/duncan-c-white/C/ch-2.c | 133 ++++++++++++++++++ challenge-200/duncan-c-white/C/parseints.c | 114 +++++++++++++++ challenge-200/duncan-c-white/C/parseints.h | 1 + challenge-200/duncan-c-white/C/printarray.c | 39 ++++++ challenge-200/duncan-c-white/C/printarray.h | 1 + challenge-200/duncan-c-white/README | 76 ++++++---- challenge-200/duncan-c-white/perl/ch-1.pl | 76 ++++++++++ challenge-200/duncan-c-white/perl/ch-2.pl | 146 ++++++++++++++++++++ 15 files changed, 948 insertions(+), 36 deletions(-) create mode 100644 challenge-200/duncan-c-white/C/Makefile create mode 100644 challenge-200/duncan-c-white/C/README create mode 100644 challenge-200/duncan-c-white/C/args.c create mode 100644 challenge-200/duncan-c-white/C/args.h create mode 100644 challenge-200/duncan-c-white/C/ch-1.c create mode 100644 challenge-200/duncan-c-white/C/ch-2.c create mode 100644 challenge-200/duncan-c-white/C/parseints.c create mode 100644 challenge-200/duncan-c-white/C/parseints.h create mode 100644 challenge-200/duncan-c-white/C/printarray.c create mode 100644 challenge-200/duncan-c-white/C/printarray.h create mode 100755 challenge-200/duncan-c-white/perl/ch-1.pl create mode 100755 challenge-200/duncan-c-white/perl/ch-2.pl diff --git a/challenge-199/duncan-c-white/C/ch-1.c b/challenge-199/duncan-c-white/C/ch-1.c index 2f42f40f25..85c8ef309f 100644 --- a/challenge-199/duncan-c-white/C/ch-1.c +++ b/challenge-199/duncan-c-white/C/ch-1.c @@ -40,6 +40,8 @@ void init_pairdynarray( pairdynarray *p ) void add_pair( pairdynarray *p, int i, int j ) { + p->p[p->npairs].i = i; + p->p[p->npairs].j = j; p->npairs++; if( p->npairs == p->nalloc ) { @@ -47,8 +49,6 @@ void add_pair( pairdynarray *p, int i, int j ) p->p = realloc( p->p, p->nalloc * sizeof(pair) ); assert( p->p != NULL ); } - p->p[p->npairs].i = i; - p->p[p->npairs].j = j; } diff --git a/challenge-199/duncan-c-white/C/ch-2.c b/challenge-199/duncan-c-white/C/ch-2.c index 97cb50b648..c55280b89d 100644 --- a/challenge-199/duncan-c-white/C/ch-2.c +++ b/challenge-199/duncan-c-white/C/ch-2.c @@ -40,6 +40,9 @@ void init_tripledynarray( tripledynarray *t ) void add_triple( tripledynarray *t, int i, int j, int k ) { + t->p[t->ntriples].i = i; + t->p[t->ntriples].j = j; + t->p[t->ntriples].k = k; t->ntriples++; if( t->ntriples == t->nalloc ) { @@ -47,9 +50,6 @@ void add_triple( tripledynarray *t, int i, int j, int k ) t->p = realloc( t->p, t->nalloc * sizeof(triple) ); assert( t->p != NULL ); } - t->p[t->ntriples].i = i; - t->p[t->ntriples].j = j; - t->p[t->ntriples].k = k; } @@ -123,7 +123,7 @@ int main( int argc, char **argv ) printf( "Good triples are below:\n" ); for( int n=0; n +#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" + + +typedef struct { + int i, j; +} pair; + + +typedef struct { + int npairs; // number of pairs. + int nalloc; // number of pairs allocated (nalloc >= npairs) + pair *p; // block to store up to pairs, of which + // the first are in use at present. +} pairdynarray; + + +void init_pairdynarray( pairdynarray *p ) +{ + p->npairs = 0; + p->nalloc = 10; + p->p = malloc( p->nalloc * sizeof(pair) ); + assert( p->p != NULL ); +} + + +void add_pair( pairdynarray *p, int i, int j ) +{ + if( debug ) + { + printf( "debug: adding pair %d..%d\n", i, j ); + } + p->p[p->npairs].i = i; + p->p[p->npairs].j = j; + p->npairs++; + if( p->npairs == p->nalloc ) + { + p->nalloc += 10; + p->p = realloc( p->p, p->nalloc * sizeof(pair) ); + assert( p->p != NULL ); + } +} + + +void free_pairdynarray( pairdynarray *p ) +{ + if( p->p != NULL ) free( p->p ); + p->p = NULL; +} + + +bool isarith( int *list, int from, int to ) +{ + int v = list[from]; + for( int i=from+1; i<=to; i++ ) + { + v++; + if( list[i] != v ) return false; + } + return true; +} + + +int main( int argc, char **argv ) +{ + int argno = process_flag_n_m_args( "arithmetic-slices", argc, argv, + 1, 1000, "intlist" ); + + int nel; + int *list = parse_int_args( argc, argv, argno, &nel ); + + if( nel < 3 ) + { + fprintf( stderr, "arithmetic-slices: need a list of > 2 elements\n" ); + exit(1); + } + + if( debug ) + { + printf( "debug: initial list: " ); + print_int_array( 60, nel, list, ',', stdout ); + putchar( '\n' ); + } + + pairdynarray p; + init_pairdynarray( &p ); + + for( int i=0; ifrom ) putchar( ',' ); + printf( "%d", list[i] ); + } + printf( ")\n" ); + } + + free( list ); + free_pairdynarray( &p ); + + return 0; +} diff --git a/challenge-200/duncan-c-white/C/ch-2.c b/challenge-200/duncan-c-white/C/ch-2.c new file mode 100644 index 0000000000..f6febbfa2c --- /dev/null +++ b/challenge-200/duncan-c-white/C/ch-2.c @@ -0,0 +1,133 @@ +// +// Task 2: Seven Segment 200 +// +// C version. +// + +#include +#include +#include +#include +#include +#include + +#include "args.h" +#include "parseints.h" +#include "printarray.h" + + +// +// init_blank( arr, ndigits ); +// Initialize and blank the 7 element array of char * strings. +// +void init_blank( char **arr, int ndigits ) +{ + for( int i=0; i<7; i++ ) + { + int len = 9 * ndigits + 1; + arr[i] = malloc( len ); + assert( arr[i] != NULL ); + for( int j=0; j9; i/=10 ) + { + ndigits++; + pow10 *= 10; + } + + if( debug ) + { + printf( "debug: n=%d, ndigits=%d, pow10=%d\n", n, ndigits, pow10 ); + } + + // ok, blank the results.. + char *results[7]; + init_blank( results, ndigits ); + + for( int i=0; i +#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-200/duncan-c-white/C/printarray.h b/challenge-200/duncan-c-white/C/printarray.h new file mode 100644 index 0000000000..40efb83277 --- /dev/null +++ b/challenge-200/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-200/duncan-c-white/README b/challenge-200/duncan-c-white/README index dffa7605cf..280aa5aefd 100644 --- a/challenge-200/duncan-c-white/README +++ b/challenge-200/duncan-c-white/README @@ -1,58 +1,74 @@ -Task 1: Max Gap +Task 1: Arithmetic Slices -You are given a list of integers, @list. +You are given an array of integers. -Write a script to find the total pairs in the sorted list where 2 -consecutive elements has the max gap. If the list contains less then 2 -elements then return 0. +Write a script to find out all Arithmetic Slices for the given array +of integers. An integer array is called arithmetic if it has at least +3 elements and the differences between any three consecutive elements +are the same. -Example 1 -Input: @list = (2,5,8,1) -Output: 2 +Example 1 -Since the sorted list (1,2,5,8) has 2 such pairs (2,5) and (5,8) +Input: @array = (1,2,3,4) +Output: (1,2,3), (2,3,4), (1,2,3,4) Example 2 -Input: @list = (3) -Output: 0 +Input: @array = (2) +Output: () as no slice found. -MY NOTES: very easy. sort, then sequence through the sorted list, -finding the max gap so far and all pairs with that gap. +MY NOTES: pretty easy. generate and test: generate all subarrays of len > 2 +via two nested for loops. then test for all-elements-one-apart. (Actually, +that's not quite what the spec said, but I noticed that too late). 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: Prime Count +Task 2: Seven Segment 200 +Submitted by: Ryan J Thompson -You are given an integer $n > 0. +A seven segment display is an electronic component, usually used to +display digits. The segments are labeled 'a' through 'g' as shown: -Write a script to print the count of primes less than $n. -Example 1 + a + --- + | | + f | | b + | g | + --- + | | + e | | c + | d | + --- + +Seven Segment -Input: $n = 10 -Output: 4 as in there are 4 primes less than 10 are 2, 3, 5 ,7. +The encoding of each digit can thus be represented compactly as a truth table: -Example 2 +my @truth = qw; -Input: $n = 15 -Output: 6 +For example, $truth[1] = "bc". The digit 1 would have segments "b" and "c" +enabled. -Example 3 +Write a program that accepts any decimal number and draws that number as a horizontal sequence of ASCII seven segment displays, similar to the following: -Input: $n = 1 -Output: 0 +------- ------- ------- + | | | | | + | | | | | +------- +| | | | | +| | | | | +------- ------- ------- -Example 4 +To qualify as a seven segment display, each segment must be drawn (or +not drawn) according to your @truth table. -Input: $n = 25 -Output: 9 +The number "200" was of course chosen to celebrate our 200th week! -MY NOTES: very easy, specially if you have a prime finding module lying -around:-) +MY NOTES: not quite so easy, but doable. 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-200/duncan-c-white/perl/ch-1.pl b/challenge-200/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..8403d47bbb --- /dev/null +++ b/challenge-200/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl +# +# Task 1: Arithmetic Slices +# +# You are given an array of integers. +# +# Write a script to find out all Arithmetic Slices for the given array +# of integers. An integer array is called arithmetic if it has at least +# 3 elements and the differences between any three consecutive elements +# are the same. +# +# +# Example 1 +# +# Input: @array = (1,2,3,4) +# Output: (1,2,3), (2,3,4), (1,2,3,4) +# +# Example 2 +# +# Input: @array = (2) +# Output: () as no slice found. +# +# +# MY NOTES: pretty easy. generate and test: generate all subarrays of len > 2 +# via two nested for loops. then test for all-elements-one-apart. (Actually, +# that's not quite what the spec said, but I noticed that too late). +# +# 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: arithmetic-slices [--debug] intlist\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my @list = split( /,/, join(',',@ARGV) ); + +die "arithmetic-slices: need at least 3 ints in list\n" unless @list>2; + +my @result; + +sub isarith +{ + my( @a ) = @_; + die "need at least 3 elements in @a\n", if @a<3; + my $v = shift @a; + foreach my $e (@a) + { + $v++; + return 0 unless $e == $v; + } + return 1; +} + + +for( my $i=0; $i<@list-2; $i++ ) +{ + for( my $j=$i+2; $j<@list; $j++ ) + { + my @sub = @list[$i..$j]; + say "debug: sub($i,$j)=". join(',',@sub) if $debug; + push @result, \@sub if isarith(@sub); + } +} + +say "Results:"; +foreach my $p (@result) +{ + say "(".join(',',@$p).")"; +} diff --git a/challenge-200/duncan-c-white/perl/ch-2.pl b/challenge-200/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..98cf4281c6 --- /dev/null +++ b/challenge-200/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,146 @@ +#!/usr/bin/perl +# +# Task 2: Seven Segment 200 +# Submitted by: Ryan J Thompson +# +# A seven segment display is an electronic component, usually used to +# display digits. The segments are labeled 'a' through 'g' as shown: +# +# a +# --- +# | | +# f | | b +# | g | +# --- +# | | +# e | | c +# | d | +# --- +# +# Seven Segment +# +# The encoding of each digit can thus be represented compactly as a truth table: +# +# my @truth = qw; +# +# For example, $truth[1] = "bc". The digit 1 would have segments "b" and "c" +# enabled. +# +# Write a program that accepts any decimal number and draws that number +# as a horizontal sequence of ASCII seven segment displays, similar to +# the following: +# +# ------- ------- ------- +# | | | | | +# | | | | | +# ------- +# | | | | | +# | | | | | +# ------- ------- ------- +# +# To qualify as a seven segment display, each segment must be drawn (or +# not drawn) according to your @truth table. +# +# The number "200" was of course chosen to celebrate our 200th week! +# +# MY NOTES: not quite so easy, but doable. +# +# 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: 7-segment [--debug] N\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV == 1; +my $n = shift; + +my @truth = qw; + + +=pod +=head2 append_blank( $result ); + +Append a blank canvas 9 chars wide to @$result, +ready for drawing a 7-segment digit on top of. + +=cut +fun append_blank( $result ) +{ + foreach my $i (0..$#$result) + { + $result->[$i] .= " "; + } +} + + +=pod +=head2 hline( $result, $sx, $ex, $row ); + +Draw a horizontal line into the last 9 chars of @$result +from (sx,y) to (ex,y). + +=cut +fun hline( $result, $sx, $ex, $y ) +{ + my $l = length($result->[0]); + for( my $i=$sx; $i<=$ex; $i++ ) + { + substr( $result->[$y], $l-9+$i, 1 ) = '-'; + } +} + + +=pod +=head2 vline( $result, $x, $sy, $ey ); + +Draw a vertical line into the last 9 chars of @$result +from (x,sy) to (x,ey). + +=cut +fun vline( $result, $x, $sy, $ey ) +{ + my $l = length($result->[0]); + for( my $i=$sy; $i<=$ey; $i++ ) + { + substr( $result->[$i], $l-9+$x, 1 ) = '|'; + } +} + + +=pod +=head2 append_digit( $digit, $result ); + +Append the 7-segment version of $digit to @$result + +=cut +fun append_digit( $digit, $result ) +{ + append_blank( $result ); + my $bitmap = $truth[$digit]; + + hline( $result, 1, 5, 0 ) if $bitmap =~ /a/; + vline( $result, 6, 1, 2 ) if $bitmap =~ /b/; + vline( $result, 0, 1, 2 ) if $bitmap =~ /f/; + hline( $result, 1, 5, 3 ) if $bitmap =~ /g/; + vline( $result, 6, 4, 5 ) if $bitmap =~ /c/; + vline( $result, 0, 4, 5 ) if $bitmap =~ /e/; + hline( $result, 1, 5, 6 ) if $bitmap =~ /d/; +} + + +my @result = ("") x 7; +#die Dumper(\@result); + +foreach my $digit (split(//,$n)) +{ + append_digit( $digit, \@result ); +} + +say for @result; -- cgit