From 85d81a12be37eba47cbad2cb4f8218d951436bec Mon Sep 17 00:00:00 2001 From: E7-87-83 Date: Fri, 13 Aug 2021 22:40:21 +0800 Subject: week 125 --- challenge-125/cheok-yin-fung/perl/ch-1.pl | 57 +++++++++++++++++++++++++++++++ challenge-125/cheok-yin-fung/perl/ch-2.pl | 5 ++- 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 challenge-125/cheok-yin-fung/perl/ch-1.pl diff --git a/challenge-125/cheok-yin-fung/perl/ch-1.pl b/challenge-125/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..10fabf76ef --- /dev/null +++ b/challenge-125/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,57 @@ +#!/usr/bin/perl +# The Weekly Challenge 125 +# Task 1: Pythagorean Triples +# Usage: ch-1.pl $n +use strict; +use warnings; +use v5.10.0; +use experimental 'signatures'; +use List::Util qw/max/; +use Test::More tests => 7; + +my $num = $ARGV[0] || 5; +my @arr = pyth($num)->@*; + +if (scalar @arr > 0) { + say "(",join(", ", $_->@*),")" foreach(@arr); +} +else { + say "-1"; +} + + + +sub pyth ($n) { + my @ans; + for my $a (1..int $n/sqrt(2) ) { + my $is_sq = $n*$n-$a*$a; + if (sqrt($is_sq) == int sqrt($is_sq)) { + push @ans, [$a, sqrt($is_sq), $n]; + } + } + + for my $a0 (1..$n-1) { + my $is_sq = $a0*$a0 + $n*$n; + if (sqrt($is_sq) == int sqrt($is_sq)) { + push @ans, [$a0, $n, sqrt($is_sq)]; + } + } + + for my $b0 ($n+1..$n*$n) { + my $is_sq = $b0*$b0 + $n*$n; + if (sqrt($is_sq) == int sqrt($is_sq)) { + push @ans, [$n , $b0 ,sqrt($is_sq)]; + } + } + + return \@ans; +} + + +ok scalar @{pyth(1)} == 0, "Number 1"; +ok scalar @{pyth(2)} == 0, "Number 2"; +ok scalar @{pyth(3)} == 1, "Number 3"; +ok scalar @{pyth(4)} == 1, "Number 4"; +ok scalar @{pyth(5)} == 2, "Number 5"; +ok scalar @{pyth(8)} == 2, "Number 8"; +ok scalar @{pyth(13)} == 2, "Number 13"; diff --git a/challenge-125/cheok-yin-fung/perl/ch-2.pl b/challenge-125/cheok-yin-fung/perl/ch-2.pl index 83851812d8..91ca281ea0 100644 --- a/challenge-125/cheok-yin-fung/perl/ch-2.pl +++ b/challenge-125/cheok-yin-fung/perl/ch-2.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl -# The Weekly Challenge 094 +# The Weekly Challenge 125 # Task 2: Binary Tree Diameter +# Usage: ch-2.pl [binary tree in array format, 'x' for null nodes] use strict; use warnings; use v5.10.0; @@ -42,6 +43,8 @@ sub diameter { return $max_dist; } +# above: I am tired this week therefore I don't optimize. -- CY + sub collect_leaves_by_id { my @tree = $_[0]->@*; my @leaves; -- cgit