diff options
| author | David Ferrone <zapwai@gmail.com> | 2022-12-12 13:12:35 -0500 |
|---|---|---|
| committer | David Ferrone <zapwai@gmail.com> | 2022-12-12 13:12:35 -0500 |
| commit | 5f5c979bb278b989fbff1473ed44aefd2861cba9 (patch) | |
| tree | b282e0994081cf231c7f82fe56c2734dac31fb45 | |
| parent | 9801381b85125dbcb7a501a85337af1d9eee7f03 (diff) | |
| download | perlweeklychallenge-club-5f5c979bb278b989fbff1473ed44aefd2861cba9.tar.gz perlweeklychallenge-club-5f5c979bb278b989fbff1473ed44aefd2861cba9.tar.bz2 perlweeklychallenge-club-5f5c979bb278b989fbff1473ed44aefd2861cba9.zip | |
week 195
| -rwxr-xr-x | challenge-195/zapwai/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-195/zapwai/perl/ch-2.pl | 27 |
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"; |
