aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-10-31 16:42:44 +0000
committerGitHub <noreply@github.com>2023-10-31 16:42:44 +0000
commita7baa1966641d39b7a4daf06e96226cc56eb91b5 (patch)
treee499bf887c9321c53d82344d3dfad9b631f46665
parent16bf3983753261eee7c2e898a91be21dca8addc7 (diff)
parent4ee504e83ea5e87a2007472f8672ff9a1f9978bd (diff)
downloadperlweeklychallenge-club-a7baa1966641d39b7a4daf06e96226cc56eb91b5.tar.gz
perlweeklychallenge-club-a7baa1966641d39b7a4daf06e96226cc56eb91b5.tar.bz2
perlweeklychallenge-club-a7baa1966641d39b7a4daf06e96226cc56eb91b5.zip
Merge pull request #8976 from kjetillll/challenge-241-kjetillll
challenge-241
-rw-r--r--challenge-241/kjetillll/perl/ch-1.pl39
-rw-r--r--challenge-241/kjetillll/perl/ch-2.pl46
2 files changed, 85 insertions, 0 deletions
diff --git a/challenge-241/kjetillll/perl/ch-1.pl b/challenge-241/kjetillll/perl/ch-1.pl
new file mode 100644
index 0000000000..09fa327ecf
--- /dev/null
+++ b/challenge-241/kjetillll/perl/ch-1.pl
@@ -0,0 +1,39 @@
+use warnings; use strict;
+
+#use Algorithm::Combinatorics 'combinations'; #...or just use this one:
+
+sub comb{my($l,$k,$s)=(@_,0);$k?map{my$i=$_;map[$$l[$i],@$_],comb($l,$k-1,$i+1)}$s..@$l-$k:[]}
+
+sub triplets { comb(\@_,3) }
+
+sub number_of_arithmetic_triplets {
+ my($diff,@array) = @_;
+ scalar
+ grep {
+ my($ni, $nj, $nk) = @$_;
+ $nj-$ni == $diff and $nk-$nj == $diff
+ }
+ triplets(@array)
+}
+
+for my $test (
+ {
+ input_nums => [0, 1, 4, 6, 7, 10],
+ input_diff => 3,
+ output_expected => 2
+ },
+ {
+ input_nums => [4, 5, 6, 7, 8, 9],
+ input_diff => 2,
+ output_expected => 2
+ }
+){
+ my $output_got = number_of_arithmetic_triplets(
+ $$test{input_diff},
+ @{ $$test{input_nums} }
+ );
+ my $result = $output_got == $$test{output_expected}
+ ? 'ok'
+ : 'NOT OK';
+ print "$result expected: $$test{output_expected} got: $output_got\n";
+}
diff --git a/challenge-241/kjetillll/perl/ch-2.pl b/challenge-241/kjetillll/perl/ch-2.pl
new file mode 100644
index 0000000000..c81e8bacc9
--- /dev/null
+++ b/challenge-241/kjetillll/perl/ch-2.pl
@@ -0,0 +1,46 @@
+use warnings; use strict;
+
+#use Math::Prime::Util 'factor' #...or this simpler but suboptimal one:
+
+sub factor { #returns the list of an ints prime factors
+ my($n) = @_;
+ my $factor;
+ $n % $_ == 0 and $factor = $_ and last for 2 .. sqrt $n;
+ $factor ? ( $factor, factor($n/$factor) ) : $n
+}
+
+sub number_of_prime_factors { my @factor = factor(@_); scalar @factor }
+
+sub prime_order {
+ map $$_{value},
+ sort {
+ $$a{sort_by} <=> $$b{sort_by} or
+ $$a{value} <=> $$b{value}
+ }
+ map {value=>$_, sort_by=>number_of_prime_factors($_)},
+ @_
+}
+
+for my $test (
+ {
+ input => [11, 8, 27, 4],
+ output_expected => [11, 4, 8, 27]
+ },
+ {
+ input => [
+ 2 * 3 * 4 * 5, #120
+ 2 ** 8, #256
+ 7 * 7 * 7, #343
+ 3 * 7 * 11, #231
+ 2 * 7 * 13, #182
+ 19 * 23, #437
+ ],
+ output_expected => [437, 182,231,343,120,256]
+ }
+){
+ my @output_got = prime_order( @{ $$test{input} } );
+ print "@output_got" eq "@{ $$test{output_expected} }"
+ ? 'ok'
+ : 'NOT OK';
+ print " got: @output_got\n";
+}