aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-12-03 11:22:37 +0000
committerGitHub <noreply@github.com>2021-12-03 11:22:37 +0000
commit484b96336dd4f4dad3e8e055b4da527dbf4a6330 (patch)
tree3ec3220a67148b2433c074c49e1220139c918e0b
parent2379f0f5e6edef92837c02fa808216d9a46c3578 (diff)
parent5509f49043513a7ea5ee9d1efedf8452561624b3 (diff)
downloadperlweeklychallenge-club-484b96336dd4f4dad3e8e055b4da527dbf4a6330.tar.gz
perlweeklychallenge-club-484b96336dd4f4dad3e8e055b4da527dbf4a6330.tar.bz2
perlweeklychallenge-club-484b96336dd4f4dad3e8e055b4da527dbf4a6330.zip
Merge pull request #5320 from corvettes13/patch-13
Create ch-2.pl
-rw-r--r--challenge-141/paul-fajman/perl/ch-2.pl99
1 files changed, 99 insertions, 0 deletions
diff --git a/challenge-141/paul-fajman/perl/ch-2.pl b/challenge-141/paul-fajman/perl/ch-2.pl
new file mode 100644
index 0000000000..51224b48e5
--- /dev/null
+++ b/challenge-141/paul-fajman/perl/ch-2.pl
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+
+# Weekly Challenge 141 Task 2
+#
+# You are given positive integers, $m and $n.
+#
+# Write a script to find total count of integers created using the digits of
+# $m which is also divisible by $n.
+#
+# Repeating of digits are not allowed. Order/Sequence of digits can’t be altered.
+# You are only allowed to use (n-1) digits at the most. For example, 432 is not
+# acceptable integer created using the digits of 1234. Also for 1234, you can only
+# have integers having no more than three digits.
+
+use strict;
+use warnings;
+
+my ($m, $n, $nums);
+
+if ($#ARGV eq 1) {
+ $m = $ARGV[0];
+ $n = $ARGV[1];
+}
+else {
+ print "Using test data. Provide 2 input numbers to test your own numbers.\n";
+ $m = 1234;
+ $n = 2;
+}
+
+# Split the input integer into an array.
+# Will use it to construct numbers later.
+
+my @number = split(//,$m);
+my (@final, @all_nums, @dfinal);
+my (@arr, @sarr, my @uarr);
+
+my $i=1;
+my $j=length($m);
+
+# Create a string from an array from digit 1 up to the maximum digit.
+my $digits = join(",",(1..$j));
+
+# All nums will possess all length-1 combinations of the digits spot.
+until ($i eq $j) {
+ @arr = glob "{$digits}" x $i;
+ push @all_nums, @arr;
+ $i++;
+}
+
+# Loop through all of our possible digit place combinations.
+for my $z (@all_nums) {
+ @arr = split(//,$z);
+ @uarr = uniq(@arr);
+
+ # Check that there were no repeated digits.
+ if (@arr ne @uarr) {
+ next;
+ }
+
+ # Produce a sort array. Order/sequence matters
+ @sarr = sort(@arr);
+
+ # Use an arry match operator to compare original array vs the sorted.
+ # If it's not a match, then it's not in the correct order.
+ if (@arr ~~ @sarr) {
+ # Do nothing, it's a match. I don't know how to calculate the opposite of this.
+ }
+ else {
+ next;
+ }
+
+ # With a valid sequence of digits, the number can be "constructed"
+ # Because I used digit places, need to use that digit place and the
+ # original array that the number was dumped into (@number);
+ foreach(@arr) {
+ $nums.=$number[$_-1];
+ }
+ push @final, $nums;
+ undef($nums);
+}
+
+# Sort the possible integers created using the digits.
+@final = sort { $a <=> $b } @final;
+
+for my $y (@final) {
+ if (($y % $n) == 0) {
+ push @dfinal, $y
+ }
+}
+print "Possible integers created using the digits of $m are:\n@final.\n\n";
+print "There are ".@dfinal." integers divisible by $n such as:\n@dfinal.\n\n";
+
+# subroutine uniq
+# Subroutine is used to determine if elements in an array are unique
+sub uniq {
+ my %hash;
+ grep !$hash{$_}++, @_;
+}
+# End of subroutine uniq ############################################