diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-08 23:39:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-08 23:39:42 +0000 |
| commit | 031ad71bd84ec4ee127a2582c9a4e5ad4f6163f7 (patch) | |
| tree | 4b7886baece3ca55d92ada2b243dc1040d289e1e /challenge-085 | |
| parent | 1d65fed5d9627c01852cf53c3790b919d4bd5f85 (diff) | |
| parent | ff06f9b18b96641f70cfe86959b2d963b9ee0a29 (diff) | |
| download | perlweeklychallenge-club-031ad71bd84ec4ee127a2582c9a4e5ad4f6163f7.tar.gz perlweeklychallenge-club-031ad71bd84ec4ee127a2582c9a4e5ad4f6163f7.tar.bz2 perlweeklychallenge-club-031ad71bd84ec4ee127a2582c9a4e5ad4f6163f7.zip | |
Merge pull request #2729 from dcw803/master
committed my solutions to this week's questions
Diffstat (limited to 'challenge-085')
| -rw-r--r-- | challenge-085/duncan-c-white/README | 89 | ||||
| -rwxr-xr-x | challenge-085/duncan-c-white/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-085/duncan-c-white/perl/ch-2.pl | 61 |
3 files changed, 150 insertions, 65 deletions
diff --git a/challenge-085/duncan-c-white/README b/challenge-085/duncan-c-white/README index 45d0cc6f4e..53a59c4fc8 100644 --- a/challenge-085/duncan-c-white/README +++ b/challenge-085/duncan-c-white/README @@ -1,91 +1,50 @@ -Task 1: "Reverse Integer +Task 1: "Triplet Sum -You are given an integer $N. +You are given an array of real numbers greater than zero. -Write a script to reverse the given integer and print the result. Print -0 if the result doesn't fit in 32-bit signed integer. - -The number 2,147,483,647 is the maximum positive value for a 32-bit -signed binary integer in computing. +Write a script to find if there exists a triplet (a,b,c) such that 1 < +a+b+c < 2. Print 1 if you succeed otherwise 0. Example 1: - Input: 1234 - Output: 4321 + Input: @R = (1.2, 0.4, 0.1, 2.5) + Output: 1 as 1 < 1.2 + 0.4 + 0.1 < 2 Example 2: - Input: -1234 - Output: -4321 + Input: @R = (0.2, 1.5, 0.9, 1.1) + Output: 0 Example 3: - Input: 1231230512 - Output: 0 - + Input: @R = (0.5, 1.1, 0.3, 0.7) + Output: 1 as 1 < 0.5 + 1.1 + 0.3 < 2 -My notes: simple, clearly defined: only tricky bit is the overflow detection -as Perl **doesn't** use 32bit integers. +My notes: simple, I think it means "pick any 3 elements a, b and c from the +array and determine if any a,b,c triple sums to between 1 and 2", although both +successful examples show 3 ADJACENT elements a, b and c... -Task 2: "Find Square +Task 2: "Power of Two Integers -You are given a binary matrix of size m x n (all elements are 1 or 0). +You are given a positive integer $N. -Write a script to find the count of squares having all four corners set as 1. +Write a script to find if it can be expressed as a ** b where a > 0 and +b > 1. Print 1 if you succeed otherwise 0. Example 1: -Input: [ 0 1 0 1 ] - [ 0 0 1 0 ] - [ 1 1 0 1 ] - [ 1 0 0 1 ] - -Output: 1 - -Explanation: - There is one square (3x3) in the given matrix with four corners - as 1 starts at r=1;c=2. - - [ 1 0 1 ] - [ 0 1 0 ] - [ 1 0 1 ] +Input: 8 +Output: 1 as 8 = 2 ** 3 Example 2: -Input: [ 1 1 0 1 ] - [ 1 1 0 0 ] - [ 0 1 1 1 ] - [ 1 0 1 1 ] - -Output: 4 - -Explanation: - There is one square (4x4) in the given matrix with four corners - as 1 starts at r=1;c=1. - There is one square (3x3) in the given matrix with four corners as 1 - starts at r=1;c=2. - There are two squares (2x2) in the given matrix with four corners as - 1. First starts at r=1;c=1 and second starts at r=3;c=3. - -Example 3: - -Input: [ 0 1 0 1 ] - [ 1 0 1 0 ] - [ 0 1 0 0 ] - [ 1 0 0 1 ] - +Input: 15 Output: 0 -My notes: clearly defined, seems straightforward. - -Have to define the Input format: CSV rows on command line so example 3 is: -./ch-2.pl 0,1,0,1 1,0,1,0 0,1,0,0 1,0,0,1 +Example 3: -Initially, I found all RECTANGLES inside the matrix, then rejected those that -weren't SQUARES - before ever checking whether the corner elements were all 1. -Find that in ch-2-firstattempt.pl +Input: 125 +Output: 1 as 125 = 5 ** 3 -But then I improved that, by directly finding all SQUARES inside the -matrix, then checked whether their corner elements were all 1. -This reduces 4 nested loops to 3.. +My notes: clearly defined, not quite sure how to do it so.. let's brute force diff --git a/challenge-085/duncan-c-white/perl/ch-1.pl b/challenge-085/duncan-c-white/perl/ch-1.pl new file mode 100755 index 0000000000..8407c6b6a4 --- /dev/null +++ b/challenge-085/duncan-c-white/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl +# +# Task 1: "Triplet Sum +# +# You are given an array of real numbers greater than zero. +# +# Write a script to find if there exists a triplet (a,b,c) such that 1 < +# a+b+c < 2. Print 1 if you succeed otherwise 0. +# +# Example 1: +# +# Input: @R = (1.2, 0.4, 0.1, 2.5) +# Output: 1 as 1 < 1.2 + 0.4 + 0.1 < 2 +# +# Example 2: +# +# Input: @R = (0.2, 1.5, 0.9, 1.1) +# Output: 0 +# +# Example 3: +# +# Input: @R = (0.5, 1.1, 0.3, 0.7) +# Output: 1 as 1 < 0.5 + 1.1 + 0.3 < 2 +# +# My notes: simple, I think it means "pick any 3 elements a, b and c from the +# array and determine if any a,b,c triple sums to between 1 and 2", although +# both successful examples show 3 ADJACENT elements a, b and c... +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Data::Dumper; + +die "Usage: triplet-sum array\n" unless @ARGV; +my @x = @ARGV; + +# +# my $istriplet = findtriplet( @x ); +# Find whether the sum of any triplet of elements from @x +# has a sum between 1 and 2. Return 1 iff one does; return 0 otherwise. +# +fun findtriplet( @x ) +{ + my $n = @x; + foreach my $apos (0..$n-3) + { + my $a = $x[$apos]; + foreach my $bpos ($apos+1..$n-2) + { + my $b = $x[$bpos]; + foreach my $cpos ($bpos+1..$n-1) + { + my $c = $x[$cpos]; + my $sum = $a+$b+$c; + return 1 if $sum > 1 && $sum < 2; + } + } + } + return 0; +} + +my $istriplet = findtriplet( @x ); +say $istriplet; diff --git a/challenge-085/duncan-c-white/perl/ch-2.pl b/challenge-085/duncan-c-white/perl/ch-2.pl new file mode 100755 index 0000000000..5794389ed5 --- /dev/null +++ b/challenge-085/duncan-c-white/perl/ch-2.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +# +# Task 2: "Power of Two Integers +# +# You are given a positive integer $N. +# +# Write a script to find if it can be expressed as a ** b where a > 0 and +# b > 1. Print 1 if you succeed otherwise 0. +# +# Example 1: +# +# Input: 8 +# Output: 1 as 8 = 2 ** 3 +# +# Example 2: +# +# Input: 15 +# Output: 0 +# +# Example 3: +# +# Input: 125 +# Output: 1 as 125 = 5 ** 3 +# +# My notes: clearly defined, not quite sure how to do it so.. let's brute force +# + +use strict; +use warnings; +use feature 'say'; +use Function::Parameters; +use Data::Dumper; + +die "Usage: power N\n" unless @ARGV==1; +my $n = shift; + +# +# my $isatob = atob( $n ); +# Return 1 iff $n if a**b where a>0 && b>1 +# +fun atob( $n ) +{ + return 1 if $n==1; + my $limit = int(sqrt($n)); + foreach my $a (2..$limit) + { + next unless $n % $a == 0; + my $x = $n/$a; + while( $x>1 && $x % $a == 0 ) + { + $x /= $a; + } + return 1 if $x==1; + } + return 0; +} + + +my $result = atob( $n ); +say $result; + |
