Sunday, October 15, 2023

TWC239

Challenge Link

Task1

We join each array's elements and check for equality:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/usr/bin/env perl
use strict;
use warnings;

sub same_string{
  join('',@{$_[0]}) eq join('',@{$_[1]});
}

printf "%d\n",same_string(["ab","c"],["a","bc"]);
printf "%d\n",same_string(["ab","c"],["ac","b"]);
printf "%d\n",same_string(["ab","cd","e"],["abcde"]);

Task2

We check if each character in each string of array is in the allowed string:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(all);

sub consistent_strings{
  my ($arr,$allowed) = @_;
  scalar grep{all{index($allowed,$_) != -1} split '',$_}@$arr;
}

printf "%d\n",consistent_strings(['ad','bd','aaab','baa','badab'],
				 'ab');
printf "%d\n",consistent_strings(['a','b','c','ab','ac','bc','abc'],
				 'abc');
printf "%d\n",consistent_strings(['cc','acd','b','ba','bac','bad',
				  'ac','d'],'cad');

No comments:

Post a Comment