diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-08-29 22:00:36 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-08-29 22:00:36 +0100 |
| commit | 9bee8320f6c3a0abc2534307ba934e0b09ae5989 (patch) | |
| tree | e3ba66e8cb993517be7c8ddd1226cd6472397daf | |
| parent | 18f637e36406b8bc280807eca3a0a5b16871efea (diff) | |
| parent | 4a209c06153ce5c6634f615183d7899f991ff53d (diff) | |
| download | perlweeklychallenge-club-9bee8320f6c3a0abc2534307ba934e0b09ae5989.tar.gz perlweeklychallenge-club-9bee8320f6c3a0abc2534307ba934e0b09ae5989.tar.bz2 perlweeklychallenge-club-9bee8320f6c3a0abc2534307ba934e0b09ae5989.zip | |
Merge pull request #4812 from dcw803/master
imported this week's tasks - two pretty easy tasks this week
| -rw-r--r-- | challenge-127/duncan-c-white/README | 80 | ||||
| -rwxr-xr-x | challenge-127/duncan-c-white/perl/ch-1.pl | 37 | ||||
| -rwxr-xr-x | challenge-127/duncan-c-white/perl/ch-2.pl | 77 | ||||
| -rw-r--r-- | challenge-127/duncan-c-white/perl/minesweeper.in | 5 |
4 files changed, 154 insertions, 45 deletions
diff --git a/challenge-127/duncan-c-white/README b/challenge-127/duncan-c-white/README index aa79957d19..bc6fe8dd99 100644 --- a/challenge-127/duncan-c-white/README +++ b/challenge-127/duncan-c-white/README @@ -1,57 +1,57 @@ -Task 1: "Count Numbers +Task 1: "Disjoint Sets -You are given a positive integer $N. +You are given two sets with unique integers. -Write a script to print count of numbers from 1 to $N that don't -contain digit 1. +Write a script to figure out if they are disjoint. -Example - -Input: $N = 15 -Output: 8 +The two sets are disjoint if they don't have any common members. - There are 8 numbers between 1 and 15 that don't contain digit 1. - 2, 3, 4, 5, 6, 7, 8, 9. +Example -Input: $N = 25 -Output: 13 + Input: @S1 = (1, 2, 5, 3, 4) + @S2 = (4, 6, 7, 8, 9) + Output: 0 as the given two sets have common member 4. - There are 13 numbers between 1 and 25 that don't contain digit 1. - 2, 3, 4, 5, 6, 7, 8, 9, 20, 22, 23, 24, 25. + Input: @S1 = (1, 3, 5, 7, 9) + @S2 = (0, 2, 4, 6, 8) + Output: 1 as the given two sets do not have common member. " -My notes: very easy. Let's produce the "There are ..." output if --debug -is given, the terse output otherwise. - +My notes: very easy: Intersection is not empty. -Task 2: "Minesweeper Game -You are given a rectangle with points marked with either x or *. -Please consider the x as a land mine (DCW adds: and a * as a non-landmine). +Task 2: "Conflict Intervals -Write a script to print a rectangle with numbers and x as in the -Minesweeper game. +You are given a list of intervals. -A number in a square of the minesweeper game indicates the number -of mines within the neighbouring squares (usually 8), also implies -that there are no bombs on that square. +Write a script to find out if the current interval conflicts with any +of the previous intervals. Example -Input: - x * * * x * x x x x - * * * * * * * * * x - * * * * x * x * x * - * * * x x * * * * * - x * * * x * * * * x - -Output: - x 1 0 1 x 2 x x x x - 1 1 0 2 2 4 3 5 5 x - 0 0 1 3 x 3 x 2 x 2 - 1 1 1 x x 4 1 2 2 2 - x 1 1 3 x 2 0 0 1 x +Input: @Intervals = [ (1,4), (3,5), (6,8), (12, 13), (3,20) ] +Output: [ (3,5), (3,20) ] + + - The 1st interval (1,4) have no previous intervals to compare, skip it. + - The 2nd interval (3,5) conflicts with previous interval (1,4). + - The 3rd interval (6,8) does not conflict with any of the previous + intervals (1,4) and (3,5), so skip it. + - The 4th interval (12,13) again does not conflict with any of the + previous intervals (1,4), (3,5) and (6,8), so skip it. + - The 5th interval (3,20) conflicts with the first interval (1,4). + +Input: @Intervals = [ (3,4), (5,7), (6,9), (10, 12), (13,15) ] +Output: [ (6,9) ] + + - The 1st interval (3,4) has no previous intervals to compare, skip it. + - The 2nd interval (5,7) does not conflict with the previous interval + (3,4), so skip it. + - The 3rd interval (6,9) does conflict with one of the previous intervals + (5,7). + - The 4th interval (10,12) do not conflicts with any of the previous + intervals (3,4), (5,7) and (6,9), so skip it. + - The 5th interval (13,15) do not conflicts with any of the previous + intervals (3,4), (5,7), (6,9) and (10,12), so skip it. " -My notes: also looks pretty easy. It's not the WHOLE minesweeper game, -just the "work out what the finished puzzle looks like" +My notes: also looks pretty easy. I think "conflict" means "overlap". diff --git a/challenge-127/duncan-c-white/perl/ch-1.pl b/challenge-127/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..3e3e2cb4ee --- /dev/null +++ b/challenge-127/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl +# +# Task 1: "Disjoint Sets +# +# You are given two sets with unique integers. +# +# Write a script to figure out if they are disjoint. +# +# The two sets are disjoint if they don't have any common members. +# +# Example +# +# Input: @S1 = (1, 2, 5, 3, 4) +# @S2 = (4, 6, 7, 8, 9) +# Output: 0 as the given two sets have common member 4. +# +# Input: @S1 = (1, 3, 5, 7, 9) +# @S2 = (0, 2, 4, 6, 8) +# Output: 1 as the given two sets do not have common member. +# " +# +# My notes: very easy: Intersection is not empty. +# + +use strict; +use warnings; +use feature 'say'; +use Getopt::Long; +#use Data::Dumper; + +my $debug=0; +die "Usage: disjoint-sets [-d|--debug] SET1 SET2\n" unless + GetOptions( "debug"=>\$debug ) && @ARGV==2; +my %s1 = map { $_ => 1 } split(/,/,$ARGV[0]); +my @common = grep { $s1{$_} } split(/,/,$ARGV[1]); +say join(',',@common) if $debug && @common; +say( (@common==0)?1:0); diff --git a/challenge-127/duncan-c-white/perl/ch-2.pl b/challenge-127/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..be60937942 --- /dev/null +++ b/challenge-127/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,77 @@ +#!/usr/bin/perl +# +# Task 2: "Conflict Intervals +# +# You are given a list of intervals. +# +# Write a script to find out if the current interval conflicts with any +# of the previous intervals. +# +# Example +# +# Input: @Intervals = [ (1,4), (3,5), (6,8), (12, 13), (3,20) ] +# Output: [ (3,5), (3,20) ] +# +# - The 1st interval (1,4) have no previous intervals to compare, skip it. +# - The 2nd interval (3,5) conflicts with previous interval (1,4). +# - The 3rd interval (6,8) does not conflict with any of the previous +# intervals (1,4) and (3,5), so skip it. +# - The 4th interval (12,13) again does not conflict with any of the +# previous intervals (1,4), (3,5) and (6,8), so skip it. +# - The 5th interval (3,20) conflicts with the first interval (1,4). +# +# Input: @Intervals = [ (3,4), (5,7), (6,9), (10, 12), (13,15) ] +# Output: [ (6,9) ] +# +# - The 1st interval (3,4) has no previous intervals to compare, skip it. +# - The 2nd interval (5,7) does not conflict with the previous interval +# (3,4), so skip it. +# - The 3rd interval (6,9) does conflict with one of the previous intervals +# (5,7). +# - The 4th interval (10,12) do not conflicts with any of the previous +# intervals (3,4), (5,7) and (6,9), so skip it. +# - The 5th interval (13,15) do not conflicts with any of the previous +# intervals (3,4), (5,7), (6,9) and (10,12), so skip it. +# " +# +# My notes: also looks pretty easy. I think "conflict" means "overlap". +# +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Getopt::Long; +use Data::Dumper; + +my $debug = 0; + +die "Usage: overlap [-d|--debug] list(from,to) pairs\n" + unless GetOptions( "debug"=>\$debug ) && @ARGV>0; +my @overlap = overlap( @ARGV ); +say join(' ',@overlap); + + +# +# my @overlap = overlap( @pair ); +# Determine whether any of @pair intervals overlap; +# return a list of those that do overlap. +# +fun overlap( @pair ) +{ + my @line; + my @result; + foreach my $xy (@pair) + { + my( $a, $b ) = split(/,/,$xy); + my $add = 0; + foreach my $pos ($a..$b) + { + $add++ if $line[$pos]; + $line[$pos] = 1; + } + push @result, $xy if $add; + } + return @result; +} diff --git a/challenge-127/duncan-c-white/perl/minesweeper.in b/challenge-127/duncan-c-white/perl/minesweeper.in deleted file mode 100644 index acf700e3c2..0000000000 --- a/challenge-127/duncan-c-white/perl/minesweeper.in +++ /dev/null @@ -1,5 +0,0 @@ - x * * * x * x x x x - * * * * * * * * * x - * * * * x * x * x * - * * * x x * * * * * - x * * * x * * * * x |
