diff options
| author | E7-87-83 <fungcheokyin@gmail.com> | 2021-06-09 16:22:51 +0800 |
|---|---|---|
| committer | E7-87-83 <fungcheokyin@gmail.com> | 2021-06-09 16:22:51 +0800 |
| commit | db0a146c35cdd246bc5317bd6f30c84339a00b3f (patch) | |
| tree | 9988f7833493bd5646ed68059d9febad4147b4c7 | |
| parent | c0e6f7597cc064e00bc42794780a55b424cfdba9 (diff) | |
| download | perlweeklychallenge-club-db0a146c35cdd246bc5317bd6f30c84339a00b3f.tar.gz perlweeklychallenge-club-db0a146c35cdd246bc5317bd6f30c84339a00b3f.tar.bz2 perlweeklychallenge-club-db0a146c35cdd246bc5317bd6f30c84339a00b3f.zip | |
Perl scripts for wk 116
| -rw-r--r-- | challenge-116/cheok-yin-fung/perl/ch-1.pl | 61 | ||||
| -rw-r--r-- | challenge-116/cheok-yin-fung/perl/ch-2.pl | 34 |
2 files changed, 95 insertions, 0 deletions
diff --git a/challenge-116/cheok-yin-fung/perl/ch-1.pl b/challenge-116/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..3ca3cd58f6 --- /dev/null +++ b/challenge-116/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!/usr/bin/perl +# The Weekly Challenge 116 +# Task 1 Number Sequence +# Usage: ch-1.pl $N +use strict; +use warnings; +# use Test::Simple tests => 7; + +my $N = $ARGV[0] || 12345; + +die "Please input an integer larger or equal to 10.\n" + unless $N =~ /^\d\d+$/; + +print "Impossible to split.\n" + unless (determine($N)); + + +sub determine { + my $num = $_[0]; + $num =~ s/^0+//; + my $yN; + my $div_len = int((1+(length $num))/2); + for my $k (1..$div_len) { + $yN = $num; + my $bool_next = 1; + my $bool_next_i = undef; + my $bool_next_d = undef; + my $f = substr($yN , 0, $k); + my $f_i; + my $f_d; + my @arr; + while ($bool_next && $yN ne "") { + $yN =~ s/^$f//; + push @arr, $f; + $f_i = $f+1; + $f_d = $f-1; + $bool_next_i = $yN =~ /^$f_i/; + $bool_next_d = $yN =~ /^$f_d/; + $bool_next = $bool_next_i || $bool_next_d; + $f = $f_i if $bool_next_i; + $f = $f_d if $bool_next_d; + } + if ($yN eq "") { + print join ",", @arr; + print "\n"; + return 1; + } + } + return 0; +} + + +=pod +ok determine(10) == 1, "N = 10"; +ok determine(1234) == 1, "example 1"; +ok determine(91011) == 1, "example 2"; +ok determine(10203) == 0, "example 3"; +ok determine(104103) == 1, "N = 104103"; +ok determine(12123456789101110) == 1, "a large N"; +ok determine(5405) == 0, "N = 5405"; +=cut diff --git a/challenge-116/cheok-yin-fung/perl/ch-2.pl b/challenge-116/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..d13dd3937e --- /dev/null +++ b/challenge-116/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl +# The Weekly Challenge 116 +# Task 2 Sum of Squares +# Usage: ch-2.pl $N +use strict; +use warnings; + +my $N = $ARGV[0]; + +die "Please input an integer larger or equal to 10.\n" + unless $N =~ /^\d\d+$/; + +print is_a_square_num(sum_of_sq($N)); +print "\n"; + + + +sub sum_of_sq { + my $num = $_[0]; + my $sum = 0; + for (split "", $num) { + $sum += $_*$_; + } + return $sum; +} + + +sub is_a_square_num { + my $a = sqrt $_[0]; + return ($a !~ /\./ ? 1 : 0) ; +} + + +# print ("$_: ", is_a_square_num(sum_of_sq($_)),"\n") for (10..99); #testing line |
