diff options
| author | Thomas Köhler <jean-luc@picard.franken.de> | 2022-12-06 19:52:16 +0100 |
|---|---|---|
| committer | Thomas Köhler <jean-luc@picard.franken.de> | 2022-12-06 19:52:16 +0100 |
| commit | 0728b3d29b59e2d812375598e497e632df2d225f (patch) | |
| tree | 561a67ac9c0ec1f41441538cde5717f15ea3f8ae | |
| parent | 92e2c29a3c032377eff9c49dbdd4bded0ba5653e (diff) | |
| download | perlweeklychallenge-club-0728b3d29b59e2d812375598e497e632df2d225f.tar.gz perlweeklychallenge-club-0728b3d29b59e2d812375598e497e632df2d225f.tar.bz2 perlweeklychallenge-club-0728b3d29b59e2d812375598e497e632df2d225f.zip | |
Add solution for challenge 194
Signed-off-by: Thomas Köhler <jean-luc@picard.franken.de>
| -rwxr-xr-x | challenge-194/jeanluc2020/perl/ch-1.pl | 43 | ||||
| -rwxr-xr-x | challenge-194/jeanluc2020/perl/ch-2.pl | 43 |
2 files changed, 86 insertions, 0 deletions
diff --git a/challenge-194/jeanluc2020/perl/ch-1.pl b/challenge-194/jeanluc2020/perl/ch-1.pl new file mode 100755 index 0000000000..5e2ef47200 --- /dev/null +++ b/challenge-194/jeanluc2020/perl/ch-1.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +# ch-1.pl +# +# You are given time in the format hh:mm with one missing digit. +# Write a script to find the highest digit between 0-9 that makes it valid time. + +use warnings; +use strict; +use Data::Dumper; + +my @times = ( + '?5:00', + '?3:00', + '1?:00', + '2?:00', + '12:?5', + '12:5?' +); + +foreach my $time (@times) { + my ($output, $newtime) = calculate($time); + print "$time -> $output ($newtime)\n"; +} + +sub calculate { + my $time = shift; + if ($time =~ m/^\?([0-3]:\d\d)$/) { # 20:XX - 23:XX + return (2, "2$1"); + } elsif ($time =~ m/^\?(\d:\d\d)$/) { # 14:XX - 19:XX + return (1, "1$1"); + } elsif ($time =~ m/^2\?(:\d\d)$/) { # 23:XX + return (3, "23$1"); + } elsif ($time =~ m/^([01])\?(:\d\d)$/) { # 09:XX or 19:XX + return (9, "${1}9$2"); + } elsif ($time =~ m/^(\d\d:)\?(\d)$/) { # XX:5X + return (5, "${1}5$2"); + } elsif ($time =~ m/^(\d\d:\d)\?$/) { # XX:X9 + return (9, "${1}9"); + } else { + die "Invalid time $time!\n"; + } +} + diff --git a/challenge-194/jeanluc2020/perl/ch-2.pl b/challenge-194/jeanluc2020/perl/ch-2.pl new file mode 100755 index 0000000000..ce28ff9a45 --- /dev/null +++ b/challenge-194/jeanluc2020/perl/ch-2.pl @@ -0,0 +1,43 @@ +#!/usr/bin/perl +# ch-2.pl +# +# You are given a string made of alphabetic characters only, a-z. +# Write a script to determine whether removing only one character can make the frequency of the remaining characters the same. + +use warnings; +use strict; + +my @strings = ( + "abbc", + "xyzyyxz", + "xzxz" +); + +foreach my $string (@strings) { + my $output = calculate($string); + print "$string -> $output\n"; +} + +sub calculate { + my $string = shift; + my @parts = split //, $string; + my $map = {}; + my $reverse_map = {}; + # map will contain the frequency of each character + map { $map->{$_}++ } @parts; + # let's count all frequencies + foreach my $key (keys %$map) { + $reverse_map->{ $map->{$key} }//=0; + $reverse_map->{ $map->{$key} }++; + } + # if we have more or less than 2 frequencies then + # removing one character won't end up in the same + # frequency for all characters + if(scalar(keys(%$reverse_map)) != 2) { + return 0; + } + # let's sort the different frequencies + my ($first, $second) = sort {$a <=> $b} keys %$reverse_map; + return 1 if $first+1 == $second && $reverse_map->{$second} == 1; + return 0; +} |
