diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-06 13:34:53 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-06 13:34:53 +0000 |
| commit | 82ce969a300c84b61d1a6ee09fbfc0727ffe0e42 (patch) | |
| tree | b98c1e0817d5da1a4573d29efed7f4ec03664da8 | |
| parent | 3c4e6204c9ec1d680983855f00ea3c7e0fd11f0e (diff) | |
| parent | 4573b40d38909f12fb344418b2e8c767d32a77a0 (diff) | |
| download | perlweeklychallenge-club-82ce969a300c84b61d1a6ee09fbfc0727ffe0e42.tar.gz perlweeklychallenge-club-82ce969a300c84b61d1a6ee09fbfc0727ffe0e42.tar.bz2 perlweeklychallenge-club-82ce969a300c84b61d1a6ee09fbfc0727ffe0e42.zip | |
Merge pull request #7214 from jacoby/master
Challege 194 Done Earlier Than Normal!
| -rw-r--r-- | challenge-194/dave-jacoby/perl/ch-1.pl | 35 | ||||
| -rw-r--r-- | challenge-194/dave-jacoby/perl/ch-2.pl | 42 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-194/dave-jacoby/perl/ch-1.pl b/challenge-194/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..366233e0a8 --- /dev/null +++ b/challenge-194/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @times = qw{ + ?5:00 + ?3:00 + 1?:00 + 2?:00 + 12:?5 + 12:5? +}; + +for my $time (@times) { + my $output = digital_clock($time); + say <<"END"; + Input: \$time = '$time' + Output: $output +END +} + +sub digital_clock ( $input ) { + my $valid = ''; + for my $i ( 0 .. 9 ) { + my $j = $input; + $j =~ s/\?/$i/mx; + my ( $h, $m ) = split m{:}, $j; + return $valid if $h > 23; + return $valid if $m > 59; + $valid = $i; + } + return $valid; +} diff --git a/challenge-194/dave-jacoby/perl/ch-2.pl b/challenge-194/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..5e59084c61 --- /dev/null +++ b/challenge-194/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = qw{ abbc xyzyyxz xzxz }; +for my $e (@examples) { + my $o = analyze($e); + say <<"END"; + Input: \$s = '$e' + Output: $o +END +} + +sub analyze( $string ) { + my $count = count_chars($string); + my @values = values $count->%*; + + for my $i ( 0 .. -1 + scalar @values ) { + my $j = $values[$i]; + my @array = @values; + $array[$i] = undef; + @array = grep { defined } @array; + my $c = 0; + my $d = scalar @array; + for my $v ( @array ) { + $c++ if $v + 1 == $j; + } + return 1 if $c == $d; + } + + return 0; +} + +sub count_chars ( $string ) { + my $output; + for my $i ( 0 .. -1 + length $string ) { + $output->{ substr $string, $i, 1 }++; + } + return $output; +} |
