diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-18 22:31:44 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-18 22:31:44 +0000 |
| commit | 0bfb7c2aca9e2767aa710f4d826ac01df3934889 (patch) | |
| tree | 020c7d4ea75992145138cc88116020378e7a0766 /challenge-241 | |
| parent | 36b392f45e2309a6ce511fc0e76b17c14032f9b5 (diff) | |
| parent | 60eaa303bfff74985a2e69485167841e71540152 (diff) | |
| download | perlweeklychallenge-club-0bfb7c2aca9e2767aa710f4d826ac01df3934889.tar.gz perlweeklychallenge-club-0bfb7c2aca9e2767aa710f4d826ac01df3934889.tar.bz2 perlweeklychallenge-club-0bfb7c2aca9e2767aa710f4d826ac01df3934889.zip | |
Merge pull request #9086 from ntovar/branch-243
Branch 243 By Nelo Tovar
Diffstat (limited to 'challenge-241')
| -rw-r--r-- | challenge-241/nelo-tovar/README.md | 1 | ||||
| -rw-r--r-- | challenge-241/nelo-tovar/perl/ch-1.pl | 59 | ||||
| -rw-r--r-- | challenge-241/nelo-tovar/perl/ch-2.pl | 28 |
3 files changed, 88 insertions, 0 deletions
diff --git a/challenge-241/nelo-tovar/README.md b/challenge-241/nelo-tovar/README.md new file mode 100644 index 0000000000..845b45e758 --- /dev/null +++ b/challenge-241/nelo-tovar/README.md @@ -0,0 +1 @@ +Solution by Nelo Tovar diff --git a/challenge-241/nelo-tovar/perl/ch-1.pl b/challenge-241/nelo-tovar/perl/ch-1.pl new file mode 100644 index 0000000000..fe64a21acd --- /dev/null +++ b/challenge-241/nelo-tovar/perl/ch-1.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +#Task 1: Arithmetic Triplets +# +#You are given an array (3 or more members) of integers in increasing order and a positive integer. +# +#Write a script to find out the number of unique Arithmetic Triplets satisfying the following rules: +# +#a) i < j < k +#b) nums[j] - nums[i] == diff +#c) nums[k] - nums[j] == diff +# +#Example 1 +# +#Input: @nums = (0, 1, 4, 6, 7, 10) +# $diff = 3 +#Output: 2 +# +#Index (1, 2, 4) is an arithmetic triplet because both 7 - 4 == 3 and 4 - 1 == 3. +#Index (2, 4, 5) is an arithmetic triplet because both 10 - 7 == 3 and 7 - 4 == 3. + +use strict; +use warnings; +use v5.28; + + +my @examples = ( + + { nums => [ 0, 1, 4, 6, 7, 10 ], diff => 3, }, + { nums => [ 4, 5, 6, 7, 8, 9 ], diff => 2, } +); + +sub aritmetic_triplet { + my $diff = shift; + my @nums = @_; + my $len_nums = scalar @nums; + my $count = 0; + + for (my $i = 0; $i < $len_nums - 2; $i++) { + for (my $j = $i + 1; $j < $len_nums - 1; $j++) { + next unless ( ($nums[$j] - $nums[$i]) == $diff ); + + for (my $k = $j + 1; $k < $len_nums; $k++) { + next unless ( ($nums[$k] - $nums[$j]) == $diff ); + $count++; + } + } + } + + return $count +} + +foreach my $element (@examples) { + my $at = aritmetic_triplet($element->{diff}, $element->{nums}->@*); + say 'Input : @nums = (', join(',', $element->{nums}->@*), ')'; + say ' $diff = ', $element->{diff}; + say "Output : $at\n" + +} diff --git a/challenge-241/nelo-tovar/perl/ch-2.pl b/challenge-241/nelo-tovar/perl/ch-2.pl new file mode 100644 index 0000000000..4ce36119b7 --- /dev/null +++ b/challenge-241/nelo-tovar/perl/ch-2.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl + +# Task 2: Prime Order +# +# You are given an array of unique positive integers greater than 2. +# +# Write a script to sort them in ascending order of the count of their prime factors, tie-breaking by ascending value. +# Example 1 +# +# Input: @int = (11, 8, 27, 4) +# Output: (11, 4, 8, 27)) +# +# Prime factors of 11 => 11 +# Prime factors of 4 => 2, 2 +# Prime factors of 8 => 2, 2, 2 +# Prime factors of 27 => 3, 3, 3 + +use strict; +use warnings; +use v5.28; + +my $x=shift; +for(my $y=2; $y<=$x; $y++) { + next if $x%$y; + $x/=$y; + say $y; + redo +} |
