From 98edabfa843b06bc8aa9246ee16de7f8f75e853e Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Tue, 6 Dec 2022 01:40:29 -0500 Subject: Week 194 --- challenge-194/zapwai/perl/ch-1.pl | 27 ++++++++++++++++++++++ challenge-194/zapwai/perl/ch-2.pl | 48 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100755 challenge-194/zapwai/perl/ch-1.pl create mode 100755 challenge-194/zapwai/perl/ch-2.pl diff --git a/challenge-194/zapwai/perl/ch-1.pl b/challenge-194/zapwai/perl/ch-1.pl new file mode 100755 index 0000000000..e1dea11103 --- /dev/null +++ b/challenge-194/zapwai/perl/ch-1.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +use feature 'say'; +my $time = $ARGV[0] || '?5:00'; +my @A = split ':', $time; +my @hh = split '', $A[0]; +my @mm = split '', $A[1]; +my $ans; +if ($hh[0] eq '?') { + if ($hh[1] < 4) { + $ans = 2; + } + else { + $ans = 1; + } +} elsif ($hh[1] eq '?') { + if ($hh[0] eq '2') { + $ans = 3; + } else { + $ans = 9; + } +} elsif ($mm[0] eq '?') { + $ans = 5; +} else { + $ans = 9; +} +say "Input: ".$time; +say "Output: ".$ans; diff --git a/challenge-194/zapwai/perl/ch-2.pl b/challenge-194/zapwai/perl/ch-2.pl new file mode 100755 index 0000000000..68ad934138 --- /dev/null +++ b/challenge-194/zapwai/perl/ch-2.pl @@ -0,0 +1,48 @@ +#!/usr/bin/env perl +use feature 'say'; +my $s = $ARGV[0] || 'abac'; +my @word = split //, $s; +my @alph = split //, "abcdefghijklmnopqrstuvwxyz"; +my %hash = map { $_ => 0 } @alph; +foreach my $letter (@word) { + $hash{$letter}++; +} +my @freq; +my @letters; +my $letter; +foreach ( keys %hash ) { + if ($hash{$_} != 0) { + push @freq, $hash{$_}; + push @letters, $_; + } +} +say "Input: \$s = \'$s\'"; +my $ans; +$ans = "possible... remove $letter\n" if possible(); +print "Output: ".$ans; +if (!$ans) { + say "(Not possible.)" +} +sub possible { + my ($min, $max) = ($freq[0],$freq[0]); + foreach (1 .. $#freq) { + if ($freq[$_] < $min) { + $min = $freq[$_]; + } + if ($freq[$_] > $max) { + $max = $freq[$_]; + } + } + return 0 if (($max - $min > 1) || ($max - $min == 0)) ; + my $index; + my $index_counter = 0; + foreach (0 .. $#freq) { + if ($freq[$_] == $max) { + $index = $_; + $index_counter++; + } + return 0 if $index_counter > 1; + } + $letter = $letters[$index]; + return 1; +} -- cgit