diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-12 18:28:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-12 18:28:03 +0100 |
| commit | bc1cdc209ebeed94f0c6542e0619d8c5b0ae3ba3 (patch) | |
| tree | 5d9e71dcb6996b64fb668862ebf28897ff4038e2 | |
| parent | b3876cd1b0d464c68581e7794f67393e7a9f8ee1 (diff) | |
| parent | 0e72507ebaf420c2269dbaa9e43af4dc54d22f4c (diff) | |
| download | perlweeklychallenge-club-bc1cdc209ebeed94f0c6542e0619d8c5b0ae3ba3.tar.gz perlweeklychallenge-club-bc1cdc209ebeed94f0c6542e0619d8c5b0ae3ba3.tar.bz2 perlweeklychallenge-club-bc1cdc209ebeed94f0c6542e0619d8c5b0ae3ba3.zip | |
Merge pull request #1556 from aliciabielsa/branch-for-challenge-055
challenge 055 perl & raku
| -rw-r--r-- | challenge-055/alicia-bielsa/perl/ch-1.pl | 65 | ||||
| -rw-r--r-- | challenge-055/alicia-bielsa/perl/ch-2.pl | 82 | ||||
| -rwxr-xr-x | challenge-055/alicia-bielsa/raku/ch-1.p6 | 61 |
3 files changed, 208 insertions, 0 deletions
diff --git a/challenge-055/alicia-bielsa/perl/ch-1.pl b/challenge-055/alicia-bielsa/perl/ch-1.pl new file mode 100644 index 0000000000..c4ae0a5e24 --- /dev/null +++ b/challenge-055/alicia-bielsa/perl/ch-1.pl @@ -0,0 +1,65 @@ +use strict; +use warnings; +use Data::Dumper; +#Flip Binary + +#You are given a binary number B, consisting of N binary digits 0 or 1: s0, s1, …, s(N-1). + +#Choose two indices L and R such that 0 ≤ L ≤ R < N and flip the digits s(L), s(L+1), …, s(R). +#By flipping, we mean change 0 to 1 and vice-versa. + +#For example, given the binary number 010, the possible flip pair results are listed below: + +# L=0, R=0 the result binary: 110 +# L=0, R=1 the result binary: 100 +# L=0, R=2 the result binary: 101 +# L=1, R=1 the result binary: 000 +# L=1, R=2 the result binary: 001 +# L=2, R=2 the result binary: 011 + +#Write a script to find the indices (L,R) that results in a binary number with maximum number of 1s. +#If you find more than one maximal pair L,R then print all of them. + +#Continuing our example, note that we had three pairs (L=0, R=0), (L=0, R=2), and (L=2, R=2) +#that resulted in a binary number with two 1s, which was the maximum. So we would print all three pairs. + +my $binaryNumber = $ARGV[0]; + + +unless (defined($binaryNumber) && $binaryNumber =~ /^[01]+$/){ + die "ERROR: Script accepts a binary number\n"; +} +print "Binary Number :$binaryNumber\n"; +my @aResults = (); +my $n = length($binaryNumber); + +foreach my $leftIndex (0..$n-1){ + foreach my $rightIndex ($leftIndex..$n-1){ + my $resultBinary = flipBetween( $leftIndex , $rightIndex) ; + my $totalOnes = () = $resultBinary =~ /1/g; + push (@aResults,{'L' => $leftIndex, 'R' => $rightIndex , 'B' => $resultBinary ,'1' => $totalOnes} ); + } +} + +my $maxNumberOnes ; +foreach my $result ( sort { $b->{'1'} <=> $a->{'1'} } @aResults){ + if (defined($maxNumberOnes) && $maxNumberOnes > $result->{'1'} ){ + last; + } + $maxNumberOnes = $result->{'1'}; + print "L=".$result->{'L'}.", R=".$result->{'R'}.", binary: ".$result->{'B'}.", ".$result->{'1'}." 1s\n"; +} + + +sub flipBetween { + my $left = shift; + my $right = shift; + my @aDigits = split ( '', $binaryNumber); + for my $i (0..$#aDigits){ + if ( $i >= $left && $i <= $right ){ + $aDigits[$i] = $aDigits[$i] ? 0 : 1; + } + } + return join ('', @aDigits); +} + diff --git a/challenge-055/alicia-bielsa/perl/ch-2.pl b/challenge-055/alicia-bielsa/perl/ch-2.pl new file mode 100644 index 0000000000..5cade6c951 --- /dev/null +++ b/challenge-055/alicia-bielsa/perl/ch-2.pl @@ -0,0 +1,82 @@ +use strict; +use warnings; +use Data::Dumper; + +#Wave Array + +#Any array N of non-unique, unsorted integers can be arranged into a wave-like array such that n1 ≥ n2 ≤ n3 ≥ n4 ≤ n5 and so on. + +#For example, given the array [1, 2, 3, 4], possible wave arrays +#include [2, 1, 4, 3] or [4, 1, 3, 2], since 2 ≥ 1 ≤ 4 ≥ 3 and 4 ≥ 1 ≤ 3 ≥ 2. This is not a complete list. + +#Write a script to print all possible wave arrays for an integer array N of arbitrary length. +#Notes: + +#When considering N of any length, note that the first element is always greater +#than or equal to the second, and then the ≤, ≥, ≤, … sequence alternates until the end of the array. + + +my @aArray = (1,2,3,4,5,6); +print "Given Array: [".join( ', ', @aArray)."]\n"; +my @aWaveArrays = (); +my @aTmp = (); +searchWaveArrays( \@aTmp , @aArray ); + + +foreach my $waveArray (@aWaveArrays){ + print "\n\n[ ".join( ' , ', @{$waveArray})."]"; + my @aLines = (); + my $count = 0; + my $waveFlag = 1; + while ( $waveFlag ){ + #[4, 1, 5, 2, 3] + my $countWaveMarkers = 0; + foreach my $number (@{$waveArray}){ + if ($count == 0 ){ + $aLines[$count] .= "~~~~~"; + } elsif ($count == $number-1){ + $aLines[$count] .= " ~ "; + } elsif ($count == $number-2){ + $aLines[$count] .= " ~~~ "; + } elsif ($count < $number){ + $aLines[$count] .= "~~~~~"; + } else { + $aLines[$count] .= " "; + next; + } + $countWaveMarkers++; + } + if ( $countWaveMarkers == 0){ + $waveFlag = 0; + } + + $count++; + } + print join("\n", reverse @aLines ); +} + +sub searchWaveArrays { + my $refWaveArray = shift; + my @aItemsLeft = @_; + if (scalar( @aItemsLeft ) == 0 ){ + push (@aWaveArrays, [@{$refWaveArray}]); + return; + } + my $flagUp = scalar(@{$refWaveArray}) % 2 ? 0 : 1; + foreach my $i (0..$#aItemsLeft){ + #check the last item of the wave, if defined + if (defined($$refWaveArray[$#{$refWaveArray}])){ + #If the wave is going up and the current item is lower there is no wave + if ( $flagUp && $aItemsLeft[$i] < $$refWaveArray[$#{$refWaveArray}] ){ + next; + } + #if the wave is going down and the current item is higher there is no wave + if ( !$flagUp && $aItemsLeft[$i] > $$refWaveArray[$#{$refWaveArray}] ){ + next; + } + } + my @aTmp = (@{$refWaveArray}, $aItemsLeft[$i]); + searchWaveArrays( \@aTmp, @aItemsLeft[0..$i-1], @aItemsLeft[$i+1..$#aItemsLeft]); + } +} + diff --git a/challenge-055/alicia-bielsa/raku/ch-1.p6 b/challenge-055/alicia-bielsa/raku/ch-1.p6 new file mode 100755 index 0000000000..fa1c6e143b --- /dev/null +++ b/challenge-055/alicia-bielsa/raku/ch-1.p6 @@ -0,0 +1,61 @@ +use v6; + + +#Flip Binary + +#You are given a binary number B, consisting of N binary digits 0 or 1: s0, s1, …, s(N-1). + +#Choose two indices L and R such that 0 ≤ L ≤ R < N and flip the digits s(L), s(L+1), …, s(R). +# By flipping, we mean change 0 to 1 and vice-versa. + +#For example, given the binary number 010, the possible flip pair results are listed below: + +#L=0, R=0 the result binary: 110 +#L=0, R=1 the result binary: 100 +#L=0, R=2 the result binary: 101 +#L=1, R=1 the result binary: 000 +#L=1, R=2 the result binary: 001 +#L=2, R=2 the result binary: 011 + +#Write a script to find the indices (L,R) that results in a binary number with maximum number of 1s. +#If you find more than one maximal pair L,R then print all of them. + +#Continuing our example, note that we had three pairs (L=0, R=0), (L=0, R=2), and (L=2, R=2) +#that resulted in a binary number with two 1s, which was the maximum. So we would print all three pairs. + + +sub MAIN (Int:D $binaryNumber where { $binaryNumber ~~ m/^<[01]>+$/ } ) { + say "Binary Number: $binaryNumber"; + + my @aResults = (); + my $n = $binaryNumber.chars; + for (0..$n-1) -> $leftIndex { + for ($leftIndex..$n-1) -> $rightIndex { + my @aDigits = $binaryNumber.split( '' ); + @aDigits.pop(); + @aDigits.shift(); + @aDigits = flipBetween( @aDigits ,$leftIndex , $rightIndex) ; + @aResults.push: {'L' => $leftIndex, 'R' => $rightIndex , 'B' => @aDigits.join('') ,'1' => @aDigits.grep({$_ eq 1}).elems} ; + } + } + + my $maxNumberOnes ; + for @aResults.sort({$^b.{1} <=> $^a.{1}} ) -> $result { + if ($maxNumberOnes.defined && $maxNumberOnes > $result.{'1'} ) { + last; + } + $maxNumberOnes = $result.{'1'}; + say "L="~$result.{'L'}~", R="~$result.{'R'}~", binary: "~$result.{'B'}~", "~$result.{'1'}~" 1s"; + } +} + +sub flipBetween (@aDigits, Int:D $left, Int:D $right ) { + my @aFlippedDigits = @aDigits; + for 0..@aFlippedDigits.end { + if $_ >= $left && $_ <= $right { + @aFlippedDigits[$_] = @aFlippedDigits[$_] == 1 ?? 0 !! 1; + } + } + return @aFlippedDigits; +} + |
