diff options
| author | dcw <d.white@imperial.ac.uk> | 2021-09-26 21:52:16 +0100 |
|---|---|---|
| committer | dcw <d.white@imperial.ac.uk> | 2021-09-26 21:52:16 +0100 |
| commit | aef5a4276a21a5e2d981952e822b1ec6176e1752 (patch) | |
| tree | decd74b3c27dd261609dfcafc8739709b2952bea | |
| parent | 60e4e26817bc4a51d12651aef8b52a1d8779e8e3 (diff) | |
| download | perlweeklychallenge-club-aef5a4276a21a5e2d981952e822b1ec6176e1752.tar.gz perlweeklychallenge-club-aef5a4276a21a5e2d981952e822b1ec6176e1752.tar.bz2 perlweeklychallenge-club-aef5a4276a21a5e2d981952e822b1ec6176e1752.zip | |
imported my solutions to this weeks tasks.. surprisingly easy
| -rw-r--r-- | challenge-131/duncan-c-white/README | 89 | ||||
| -rwxr-xr-x | challenge-131/duncan-c-white/perl/ch-1.pl | 71 | ||||
| -rwxr-xr-x | challenge-131/duncan-c-white/perl/ch-2.pl | 71 |
3 files changed, 180 insertions, 51 deletions
diff --git a/challenge-131/duncan-c-white/README b/challenge-131/duncan-c-white/README index d883669750..1df544175c 100644 --- a/challenge-131/duncan-c-white/README +++ b/challenge-131/duncan-c-white/README @@ -1,74 +1,61 @@ -Task 1: "Odd Number +Task 1: "Consecutive Arrays -You are given an array of positive integers, such that all the numbers -appear even number of times except one number. +You are given a sorted list of unique positive integers. -Write a script to find that integer. +Write a script to return list of arrays where the arrays are consecutive +integers. -Example 1 +Example 1: - Input: @N = (2, 5, 4, 4, 5, 5, 2) - Output: 5 - as it appears 3 times in the array where as all other numbers 2 and - 4 appears exactly twice. + Input: (1, 2, 3, 6, 7, 8, 9) + Output: ([1, 2, 3], [6, 7, 8, 9]) -Example 2 +Example 2: - Input: @N = (1, 2, 3, 4, 3, 2, 1, 4, 4) - Output: 4 -" + Input: (11, 12, 14, 17, 18, 19) + Output: ([11, 12], [14], [17, 18, 19]) + +Example 3: + + Input: (2, 4, 6, 8) + Output: ([2], [4], [6], [8]) -My notes: easy, let's use a frequency hash. +Example 4: + Input: (1, 2, 3, 4, 5) + Output: ([1, 2, 3, 4, 5]) +" -Task 2: "Binary Search Tree +My notes: easy, should be able to do this in 1-pass. -You are given a tree. -Write a script to find out if the given tree is Binary Search Tree (BST). +Task 2: "Find Pairs -According to wikipedia, the definition of BST: +You are given a string of delimiter pairs and a string to search. -A binary search tree is a rooted binary tree, whose internal nodes -each store a key (and optionally, an associated value), and each has -two distinguished sub-trees, commonly denoted left and right. The tree -additionally satisfies the binary search property: the key in each node -is greater than or equal to any key stored in the left sub-tree, and less -than or equal to any key stored in the right sub-tree. The leaves (final -nodes) of the tree contain no key and have no structure to distinguish -them from one another. +Write a script to return two strings, the first with any characters +matching the 'opening character' set, the second with any matching +the 'closing character' set. -Example 1 +Example 1: Input: - 8 - / \ - 5 9 - / \ - 4 6 + Delimiter pairs: ""[]() + Search String: "I like (parens) and the Apple ][+" they said. -Output: 1 as the given tree is a BST. +Output: + "([" + ")]" -Example 2 +Example 2: Input: - 5 - / \ - 4 7 - / \ - 3 6 + Delimiter pairs: **//<> + Search String: /* This is a comment (in some languages) */ <could be a tag> -Output: 0 as the given tree is a not BST. +Output: + /**/< + /**/> " -My notes: yet another tree question, the hardest part is to read the tree -from input, so let's reuse some tree reading logic from an earlier challenge.. - -To determine whether a given tree is a BST, we need to pass around a list of -constraints, where each constraint is either of the form "<=N" or ">=N", and -apply those constraints to each value we find in the tree. - -I also tried a second variation (ch-2-with-constraintfunctions.pl) where the -list of textual constraints was replaced by an on-the-fly constructed constraint -function. But that wasn't as clear, even though it was a few lines shorter, -and doesn't offer as good debugging support. +My notes: also pretty easy, if I've understood it right. Also doable in 1-pass. diff --git a/challenge-131/duncan-c-white/perl/ch-1.pl b/challenge-131/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..3e16d1d4cc --- /dev/null +++ b/challenge-131/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +# +# Task 1: "Consecutive Arrays +# +# You are given a sorted list of unique positive integers. +# +# Write a script to return list of arrays where the arrays are consecutive +# integers. +# +# Example 1: +# +# Input: (1, 2, 3, 6, 7, 8, 9) +# Output: ([1, 2, 3], [6, 7, 8, 9]) +# +# Example 2: +# +# Input: (11, 12, 14, 17, 18, 19) +# Output: ([11, 12], [14], [17, 18, 19]) +# +# Example 3: +# +# Input: (2, 4, 6, 8) +# Output: ([2], [4], [6], [8]) +# +# Example 4: +# +# Input: (1, 2, 3, 4, 5) +# Output: ([1, 2, 3, 4, 5]) +# " +# +# My notes: easy, should be able to do this in 1-pass. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +#use Data::Dumper; + +my $debug=0; +die "Usage: consecutive-arrays [-d|--debug] list_numbers\n". + " eg. 1 2 3 4 5 6 7 8 9\n" unless + GetOptions( "debug"=>\$debug ) && @ARGV>0; + +my $prev = shift @ARGV; +my @curr = ($prev); +my @all; +say "debug: curr = ($prev)" if $debug; + +# processing +foreach my $item (@ARGV) +{ + if( $item == $prev+1 ) + { + say "debug: extend curr by $item" if $debug; + push @curr, $item; + $prev++; + } else { + push @all, [@curr]; + say "debug: add curr - ". join(',',@curr). " to all" if $debug; + say "debug: start new curr - ($item)" if $debug; + @curr = $item; + $prev = $item; + } +} +push @all, [@curr]; +say "debug: \@end add curr - ". join(',',@curr). " to all" if $debug; + +# output +my $out = join(', ', map { "[".join(', ',@$_)."]" } @all ); +say "($out)"; diff --git a/challenge-131/duncan-c-white/perl/ch-2.pl b/challenge-131/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..5eddc7fa05 --- /dev/null +++ b/challenge-131/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,71 @@ +#!/usr/bin/perl +# +# Task 2: "Find Pairs +# +# You are given a string of delimiter pairs and a string to search. +# +# Write a script to return two strings, the first with any characters +# matching the 'opening character' set, the second with any matching +# the 'closing character' set. +# +# Example 1: +# +# Input: +# Delimiter pairs: ""[]() +# Search String: "I like (parens) and the Apple ][+" they said. +# +# Output: +# "([" +# ")]" +# +# Example 2: +# +# Input: +# Delimiter pairs: **//<> +# Search String: /* This is a comment (in some languages) */ <could be a tag> +# +# Output: +# /**/< +# /**/> +# " +# +# My notes: also pretty easy, if I've understood it right. Also should be +# doable in 1-pass. +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; + +die "Usage: find-pairs [-d|--debug] listofpairs searchstring\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV==2; +my $pairlist = shift; +my $searchstring = shift; + +my $len = length($pairlist); +die "find-pairs: listofpairs $pairlist must have EVEN length (not $len)" + unless $len % 2 == 0; + +my %open = + map { substr($pairlist,$_,1) => 1 } + grep { $_ % 2 == 0 } 0..$len-1; + +my %close = + map { substr($pairlist,$_,1) => 1 } + grep { $_ % 2 == 1 } 0..$len-1; + +#say "open=".Dumper(\%open).", close=".Dumper(\%close) if $debug; + +my $first = my $second = ''; +foreach my $letter (split(//,$searchstring)) +{ + $first .= $letter if $open{$letter}; + $second .= $letter if $close{$letter}; +} + +say "$first\n$second"; |
