diff options
| -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"; |
