# RabbitFarm

### 2021-01-31

#### Perl Weekly Challenge 097

The examples used here are from the weekly challenge problem statement and demonstrate the working solution.

## Part 1

You are given string `\$S` containing alphabets A..Z only and a number `\$N`. Write a script to encrypt the given string `\$S` using Caesar Cipher with left shift of size `\$N`.

### Solution

``````
use strict;
use warnings;
sub caesar_cypher{
my(\$s, \$n) = @_;
my @cypher = map { unless(ord(\$_) == ord(' ')){
my \$x = ((ord(\$_) - \$n) < ord('A')?(ord(\$_) - \$n + 26):(ord(\$_) - \$n));
chr(\$x);
}
else{
\$_
}
} split(//, \$s);
return join("", @cypher);
}

MAIN:{
my(\$S, \$N);
\$S = "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG";
\$N = 3;
print "\$S\n";
print caesar_cypher(\$S, \$N) . "\n";
}
``````

### Sample Run

``````
\$ perl perl/ch-1.pl
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD
``````

The basic approach here is pretty much the straightforward one: use the ascii values for the characters and subtract `\$n`. In Perl we use the ord function to do this and the chr to go in the other direction, ascii value to character. The only thing we really need to be careful of is if subtracting `\$n` takes us outside the ascii range for upper case letters, then we need to add 26 to get back in range.

Certain style instructions have been burned into my brain over the years and I find them almost impossible to deviate from. The one that applies here is Whenever possible do not use numeric literals. They are often poorly documented and become “magic numbers”, and make code readability and future debugging unnecessarily difficult. So it is in that spirit that I write, for example, `ord(' ')` instead of just `32`.

## Part 2

You are given a binary string `\$B` and an integer `\$S`. Write a script to split the binary string `\$B` of size `\$S` and then find the minimum number of flips required to make it all the same.

### Solution

``````
use strict;
use warnings;

use feature "bitwise";

sub substrings{
my(\$d, \$s) = @_;
my @substrings;
for(my \$i = 0; \$i < length(\$d); \$i+=\$s){
push @substrings, substr(\$d, \$i, \$s);
}
return @substrings;
}

sub min_flips{
my(\$d, \$s) = @_;
my @flips;
my @substrings = substrings(\$d, \$s);
for my \$digits (@substrings){
my \$flip_count = 0;
map { \$flip_count += unpack("%32b*", \$digits ^. \$_) } @substrings;
push @flips, \$flip_count;
}
return [sort {\$a <=> \$b} @flips]->;
}

MAIN:{
my(\$B, \$S);
\$B = "101100101";
\$S = 3;
print min_flips(\$B, \$S) . " flips\n";
\$B = "10110111";
\$S = 4;
print min_flips(\$B, \$S) . " flips\n";
}
``````

### Sample Run

``````
\$ perl perl/ch-2.pl
1 flips
2 flips
``````

### Notes

The `substrings` function is just a convenient wrapper around the code necessary to break the string into the right sized chunks. The assumption is that the string is evenly divisible into chunks of size `\$s`. If we were not making this assumption we would need to add some zero padding for any unevenly sized substring.

Since `use feature "bitwise";` is present the `^.` is defined and the operands to `^.` are taken to be bit strings and the result is itself a bit string.`min_flips` does a bitwise xor operation, pairwise comparing each substring in a `map`. Since xor is 1 only when the bits are different the result is a bit string of set bits, the ones needed to be flipped. `unpack` is used to sum these, and the result added `\$flip_count` which is then pushed into an array. The minimum number of flips is determined by the smallest number in that array. The bitwise feature was introduced in Perl 5.22 and graduated from experimental status in Perl 5.28.

ASCII Table

xor

Perl’s xor

bitwise feature