diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-12-11 23:19:24 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-12-11 23:19:24 +0000 |
| commit | 717c133073f8fdc40f4aa05aea0e69778b179a10 (patch) | |
| tree | fe71d695af951d50ca13a675de6a74fe435e72cf | |
| parent | f7ff588a8219fc08d62087b4153bfcbe7cc678da (diff) | |
| parent | 89c181552cf72670b39db3278f068d2b28e7b445 (diff) | |
| download | perlweeklychallenge-club-717c133073f8fdc40f4aa05aea0e69778b179a10.tar.gz perlweeklychallenge-club-717c133073f8fdc40f4aa05aea0e69778b179a10.tar.bz2 perlweeklychallenge-club-717c133073f8fdc40f4aa05aea0e69778b179a10.zip | |
Merge pull request #7238 from Solathian/branch-for-challenge-194
Adding files for challenge 194
| -rw-r--r-- | challenge-194/solathian/perl/ch-1.pl | 64 | ||||
| -rw-r--r-- | challenge-194/solathian/perl/ch-2.pl | 108 |
2 files changed, 172 insertions, 0 deletions
diff --git a/challenge-194/solathian/perl/ch-1.pl b/challenge-194/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..6453dfb00d --- /dev/null +++ b/challenge-194/solathian/perl/ch-1.pl @@ -0,0 +1,64 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; + +# Challange 194 - 1 - Digital Clock +# 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. + +#Assumptions: +# input format can be extended to a correct format +# input only contains one '?' + +sub digitalClock($string) +{ + my ($first, $second, $third, $fourth) = $string =~ m"^(\?|\d)(\?|\d):(\?|\d)(\?|\d)$"; + my $returnVal; + + #if the first one is missing + if($first eq '?') + { + if($second > 3) + { + $returnVal = 1; + } + else + { + $returnVal = 2; + } + } + elsif($second eq '?') + { + if($first eq '2') + { + $returnVal = 3; + } + else + { + $returnVal = 9; + } + } + elsif($third eq '?') + { + $returnVal = 5; + } + elsif($fourth eq '?') + { + $returnVal = 9; + } + + # $string =~ s"\?"$returnVal"; + say $returnVal; + +} + +# digitalClock('?5:00'); # Output: 1 +# digitalClock('?3:00'); # Output: 2 +# digitalClock('1?:00'); # Output: 9 +# digitalClock('2?:00'); # Output: 3 +# digitalClock('12:?5'); # Output: 5 +# digitalClock('12:5?'); # Output: 9 +# digitalClock('?0:59'); # Output: 2
\ No newline at end of file diff --git a/challenge-194/solathian/perl/ch-2.pl b/challenge-194/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..443515a9b2 --- /dev/null +++ b/challenge-194/solathian/perl/ch-2.pl @@ -0,0 +1,108 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; + + +# Challange 194 - 2 - Frequency Equalizer +# 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. + +# freqEq('abbc'); +# Output: 1 since removing one alphabet 'b' will give us 'abc' where each alphabet frequency is the same. + +# freqEq('xyzyyxz'); +# Output: 1 since removing 'y' will give us 'xzyyxz'. + +# freqEq('xzxz'); +# Output: 0 since removing any one alphabet would not give us string with same frequency alphabet. + +# freqEq('xzxza'); +# Output: 1 since removing 'a' will give us 'xzyyxz'. + + +sub freqEq($string) +{ + my @chracters = split('', $string); + my @freqArr = (0) x 26; + + map{ $freqArr[ord($_) - ord('a')]++ } @chracters; + + say checkForRemove(\@freqArr); +} + +sub checkForRemove($freqArray) +{ + my $lastFreq; + my $lastIndex; + my $returnVal = 0; + + for(my $i = 0; $i < @$freqArray; $i++) + { + my $freq = $freqArray->[$i]; + + next if($freq == 0); + + if(not defined $lastFreq) + { + $lastFreq = $freq; + $lastIndex = $i; + } + + # do not bother with items of the same frequency + next if($freq == $lastFreq); + + # so we found a different frequency, but it cannot be solved by one character removal + return 0 if($lastFreq > ($freq + 1) || $lastFreq < ($freq -1)); + + # it is a difference of one so it might be able to be solved + if($lastFreq > $freq) + { + $freqArray->[$lastIndex]--; + $returnVal = checkForSameFreq($freqArray); + $freqArray->[$lastIndex]++; + } + elsif($lastFreq < $freq) + { + $freqArray->[$i]--; + $returnVal = checkForSameFreq($freqArray); + $freqArray->[$i]++; + } + + # if the previos steps did not succeed, sometimes it is a viable option to remove the single element + if(($returnVal == 0) && ($freqArray->[$lastIndex] == 1) ) + { + $freqArray->[$lastIndex]--; + $returnVal = checkForSameFreq($freqArray); + $freqArray->[$lastIndex]++; + } + + last; + } + + return $returnVal; +} + +sub checkForSameFreq ($freqArray) +{ + my $lastFreq; + + for(my $i = 0; $i < @$freqArray; $i++) + { + my $freq = $freqArray->[$i]; + + next if($freq == 0); # disregeard 0 elements + + if(not defined $lastFreq) + { + $lastFreq = $freq; + } + return 0 if($lastFreq != $freq) # check if the previous element has a different freq + } + + # no diffs found return true + return 1; +} + |
