diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-06 18:57:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-06 18:57:48 +0000 |
| commit | a80754d8cb3465e9d3fa3154ee5e49c7d202fac8 (patch) | |
| tree | 3aee02387a742364089205d9927139c45c20af06 | |
| parent | 9bb39ef1eae0a14cc14bea1e07645e5c41f68a46 (diff) | |
| parent | 0728b3d29b59e2d812375598e497e632df2d225f (diff) | |
| download | perlweeklychallenge-club-a80754d8cb3465e9d3fa3154ee5e49c7d202fac8.tar.gz perlweeklychallenge-club-a80754d8cb3465e9d3fa3154ee5e49c7d202fac8.tar.bz2 perlweeklychallenge-club-a80754d8cb3465e9d3fa3154ee5e49c7d202fac8.zip | |
Merge pull request #7221 from jeanluc2020/jeanluc-194
Add solution for challenge 194
| -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; +} |
