aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-13 12:00:35 +0000
committerGitHub <noreply@github.com>2022-12-13 12:00:35 +0000
commitbc2c850b4b3ffd7d6d2f10f2622e51e439a3f6e6 (patch)
treeb7e08a3f02dd05ed74905341b43b37d2c4a7b056
parent2059b1abdbd1df1bd15f0df393f5e6f239a0c7cb (diff)
parent5f5c979bb278b989fbff1473ed44aefd2861cba9 (diff)
downloadperlweeklychallenge-club-bc2c850b4b3ffd7d6d2f10f2622e51e439a3f6e6.tar.gz
perlweeklychallenge-club-bc2c850b4b3ffd7d6d2f10f2622e51e439a3f6e6.tar.bz2
perlweeklychallenge-club-bc2c850b4b3ffd7d6d2f10f2622e51e439a3f6e6.zip
Merge pull request #7245 from zapwai/branch-for-challenge-195
week 195
-rwxr-xr-xchallenge-195/zapwai/perl/ch-1.pl28
-rwxr-xr-xchallenge-195/zapwai/perl/ch-2.pl27
2 files changed, 55 insertions, 0 deletions
diff --git a/challenge-195/zapwai/perl/ch-1.pl b/challenge-195/zapwai/perl/ch-1.pl
new file mode 100755
index 0000000000..606dbdd0d1
--- /dev/null
+++ b/challenge-195/zapwai/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#!/usr/bin/env perl
+use feature 'say';
+my $n = $ARGV[0] || 35;
+my @unspecial; # An integer is special when all of its digits are unique.
+my $count = 0;
+foreach (1 .. $n) {
+ if (is_special($_)) {
+ $count++;
+ } else {
+ push @unspecial, $_;
+ }
+}
+sub is_special() {
+ my $num = shift;
+ my @digits = split //, $num;
+ my @freq;
+ for (0 .. 9) {
+ foreach my $d (@digits) {
+ $freq[$_]++ if ($d == $_);
+ }
+ }
+ for (0 .. 9) {
+ return 0 if ($freq[$_] > 1);
+ }
+ return 1;
+}
+say "Input: \$n = $n";
+say "Output: $count as except for {@unspecial} all others are special.";
diff --git a/challenge-195/zapwai/perl/ch-2.pl b/challenge-195/zapwai/perl/ch-2.pl
new file mode 100755
index 0000000000..590dc00aa0
--- /dev/null
+++ b/challenge-195/zapwai/perl/ch-2.pl
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+my @list = (1,1,4,2,6);
+my @freq;
+my $ans;
+foreach my $num (@list) {
+ next if ($num % 2 == 1);
+ $freq[$num]++;
+}
+if (!@freq) {
+ $ans = -1;
+}
+my $max_freq;
+for (1 .. $#freq/2) {
+ my $i = 2*$_;
+ if ($max_freq < $freq[$i]) {
+ $max_freq = $freq[$i]
+ }
+}
+for my $num (1 .. $#freq/2) {
+ my $i = 2*$num;
+ if ( $freq[$i] == $max_freq ) {
+ $ans = $i;
+ last;
+ }
+}
+print "Input: \@list = @list\n";
+print "Output: $ans\n";