Tuesday, November 7, 2023

TWC242

Challenge Link

Task1

We convert each array to a set, then take their difference:

#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use Set::Scalar;

sub missing_members{
  my $s1 = Set::Scalar->new(@{$_[0]});
  my $s2 = Set::Scalar->new(@{$_[1]});
  [($s1-$s2)->members],[($s2-$s1)->members]
}

print show missing_members([1,2,3],[2,4,6]);
print show missing_members([1,2,3,3],[1,1,2,2]);

Task2

We reverse each sub-array of the matrix and xor each bit with 1 to flip it:


#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub flip_matrix{
  map{[map {$_^1} reverse @$_]}@{$_[0]};
}

print show flip_matrix([[1,1,0],[1,0,1],[0,0,0]]);
print show flip_matrix([[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]);

No comments:

Post a Comment