Sunday, February 9, 2025

TWC308

Challenge Link

Task1

We find the intersection of two arrays:
#!/usr/bin/env perl
use strict;
use warnings;

sub count_common{
  my %h;
  $h{$_}++ for @{$_[0]};
  scalar grep{exists $h{$_} && $h{$_}++ < 2} @{$_[1]}
}

printf "%d\n",count_common(['perl','weekly','challenge'],
			   ['raku','weekly','challenge']);
printf "%d\n",count_common(['perl','raku','python'],
			   ['python','java']);
printf "%d\n",count_common(['guest','contribution'],
			   ['fun','weekly','challenge']);

Task2

We find the resultant array by xoring the elements starting with the given first:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub decode_xor{
  my @res = ($_[1]);
  push @res,$res[-1] ^ $_ foreach @{$_[0]};
  @res
}

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

No comments:

Post a Comment