diff options
| author | Solathian <horvath6@gmail.com> | 2022-11-27 19:41:34 +0100 |
|---|---|---|
| committer | Solathian <horvath6@gmail.com> | 2022-11-27 19:41:34 +0100 |
| commit | c6618c000f50f7d756bcc3b7e3f07d3347719e63 (patch) | |
| tree | a13dc95f10b5a70aafc7e36394ff8e3882fd3015 | |
| parent | e094f0679d0b4e8ee7a79c0045841209e964ff4c (diff) | |
| download | perlweeklychallenge-club-c6618c000f50f7d756bcc3b7e3f07d3347719e63.tar.gz perlweeklychallenge-club-c6618c000f50f7d756bcc3b7e3f07d3347719e63.tar.bz2 perlweeklychallenge-club-c6618c000f50f7d756bcc3b7e3f07d3347719e63.zip | |
Adding files for challange 192
| -rw-r--r-- | challenge-192/solathian/perl/ch-1.pl | 61 | ||||
| -rw-r--r-- | challenge-192/solathian/perl/ch-2.pl | 102 |
2 files changed, 163 insertions, 0 deletions
diff --git a/challenge-192/solathian/perl/ch-1.pl b/challenge-192/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..63e81edaba --- /dev/null +++ b/challenge-192/solathian/perl/ch-1.pl @@ -0,0 +1,61 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; + +# Challange 192 - 1 - Binary Flip +# You are given a positive integer, $n. +# Write a script to find the binary flip. + +# First find the binary equivalent of the given integer, 101. +# Then flip the binary digits 0 -> 1 and 1 -> 0 and we get 010. +# So Binary 010 => Decimal 2. + +# assumptions, the given value is certainly a number and it can be stored on 32bits +sub bFlip($number) +{ + say unpack("N", pack("B32", substr("0" x 32 . join('', map( $_= $_ == 0 ? 1 : 0, split('', sprintf('%b', $number)))), -32))); +} + + +# in a more cultured and readable manner +sub bFlip2($number) +{ + # converting it to binary + $number = sprintf('%b', $number); + + # splitting it into an array + my @array = split('', $number); + + # flip bits in the array with map and a tenary operator + @array = map( $_= $_ == 0 ? 1 : 0, @array); + + # re-create the string + $number = join('', @array); + + # pad to be a 32bit binary + $number = substr("0" x 32 . $number, -32); # "If OFFSET is negative, starts that far back from the end of the string." - So only 32 chracters will be kept + + # pack it as 32bit - B: A bit string (descending bit order inside each byte). + $number = pack("B32", $number); + + #unpack N - N An unsigned long (32-bit) in "network" (big-endian) order. + $number = unpack("N", $number); + + say $number; +} + +#tests +# my $inp0 = 5; # Output: 3 +# bFlip($inp0); +# bFlip2($inp0); + +# my $inp1 = 4; # Output: 3 +# bFlip($inp1); +# bFlip2($inp1); + +# my $inp2 = 6; # Output: 1 +# bFlip($inp2); +# bFlip2($inp2);
\ No newline at end of file diff --git a/challenge-192/solathian/perl/ch-2.pl b/challenge-192/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..41f56aadfb --- /dev/null +++ b/challenge-192/solathian/perl/ch-2.pl @@ -0,0 +1,102 @@ +#!usr/bin/perl +use v5.32; +use warnings; + +use feature 'signatures'; +no warnings 'experimental'; + +# Challange 192 - 2 - Equal Distribution + +# You are given a list of integers greater than or equal to zero, @list. +# Write a script to distribute the number so that each members are same. If you succeed then print the total moves otherwise print -1. +# Please follow the rules (as suggested by Neils van Dijke [2022-11-21 13:00] + + +# 1) You can only move a value of '1' per move +# 2) You are only allowed to move a value of '1' to a direct neighbor/adjacent cell + +use constant +{ + FALSE => 0, + TRUE => 1, +}; + +# my @test0 = (1, 0, 5); +# my @test1 = (0, 2, 0); +# my @test2 = (0, 3, 0); + +# maskCode(@test0); +# maskCode(@test1); +# maskCode(@test2); + +sub isItDone($target, @list) +{ + foreach my $elem (@list) + { + return FALSE if($elem != $target); + + } + + return TRUE; +} + + +sub maskCode(@list) +{ + my $sum; + map($sum += $_, @list); + + my $target = $sum/@list; + + # check if it is even + if(int($target) == $target) + { + my $steps; + my $i = 0; + while( isItDone($target, @list) == FALSE) + { + if(($i + 1) > $#list) + { + $i = 0; + @list = reverse @list; # reverse the list for the next round + } + + if(($list[$i] > $target)) # move items until we reach target + { + if($list[$i+1] != $target) # try to place to the right + { + $steps++; + $list[$i]--; + $list[$i+1]++; + } + elsif(($list[$i-1] != $target) && (($i-1) != 0)) + { + $steps++; + $list[$i]--; + $list[$i-1]++; + } + + else + { + $i++; + } + + + } + else + { + $i++; + } + + } + say $steps; + } + + # it is odd, it cannot be done + else + { + say -1; + return -1; + } + +}
\ No newline at end of file |
