Monday, September 22, 2025

TWC340

Challenge Link

Task1

We repeatedly remove adjacent duplicate characters until there's none left:
#!/usr/bin/env perl
use strict;
use warnings;

sub duplicate_removals{
  my ($str) = @_;
  while(1) {
    my $c = 0;
    $str =~ s/(.)\1//g and $c = 1;
    last unless $c
  }
  $str
}

printf "%s\n",duplicate_removals('abbaca');
printf "%s\n",duplicate_removals('azxxzy');
printf "%s\n",duplicate_removals('aaaaaaaa');
printf "%s\n",duplicate_removals('aabccba');
printf "%s\n",duplicate_removals('abcddcba')

Task2

We extract the numbers into an array, and check if that array is ascending:

#!/usr/bin/env perl
use strict;
use warnings;

sub is_ascending{
  foreach my $i(1..$#{$_[0]}) {
    return 0 if $_[0]->[$i] <= $_[0]->[$i-1]
  }
  1
}

sub ascending_numbers{
  my @arr = grep {/\d+/} split /\s+/,$_[0];
  is_ascending(\@arr)
}

printf "%d\n",ascending_numbers('The cat has 3 kittens 7 toys 10 beds');
printf "%d\n",ascending_numbers('Alice bought 5 apples 2 oranges 9 bananas');
printf "%d\n",ascending_numbers('I ran 1 mile 2 days 3 weeks 4 months');
printf "%d\n",ascending_numbers('Bob has 10 cars 10 bikes');
printf "%d\n",ascending_numbers('Zero is 0 one is 1 two is 2');

Tuesday, September 16, 2025

TWC339

Challenge Link

Task1

We sort the array then find the difference of the two smallest and largest pairs:
#!/usr/bin/env perl
use strict;
use warnings;

sub max_diff{
  my ($arr) = @_;
  @$arr = sort{$a <=> $b} @$arr;
  $arr->[-1] * $arr->[-2] - $arr->[0] * $arr->[1]
}

printf "%d\n",max_diff([5,9,3,4,6]);
printf "%d\n",max_diff([1,-2,3,-4]);
printf "%d\n",max_diff([-3,-1,-2,-4]);
printf "%d\n",max_diff([10,2,0,5,1]);
printf "%d\n",max_diff([7,8,9,10,10]);

Task2

We take the maximum of the running sum of the given array:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub peak_point{
  my $s = 0;
  max 0,map {$s += $_} @{$_[0]}
}

printf "%d\n",peak_point([-5,1,5,-9,2]);
printf "%d\n",peak_point([10,10,10,-25]);
printf "%d\n",peak_point([3,-4,2,5,-6,1]);
printf "%d\n",peak_point([-1,-2,-3,-4]);
printf "%d\n",peak_point([-10,15,5]);

Monday, September 8, 2025

TWC338

Challenge Link

Task1

Sum each row and find their maximum:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0 max);

sub highest_row{
  max map{sum0 @$_} @{$_[0]}
}

printf "%d\n",highest_row([[4,4,4,4],
			   [10,0,0,0],
			   [2,2,2,9]]);
printf "%d\n",highest_row([[1,5],
			   [7,3],
			   [3,5]]);
printf "%d\n",highest_row([[1,2,3],
			   [3,2,1]]);
printf "%d\n",highest_row([[2,8,7],
			   [7,1,3],
			   [1,9,5]]);
printf "%d\n",highest_row([[10,20,30],
			   [5,5,5],
			   [0,100,0],
			   [25,25,25]]);

Task2

Find maximum distance between all elements of two lists:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub max_distance{
  my @pairs = map{
    my $x = $_;
    map {[$x,$_]} @{$_[1]}
  } @{$_[0]};
  max map {abs $_->[0] - $_->[1]} @pairs;
}

printf "%d\n",max_distance([4,5,7],[9,1,3,4]);
printf "%d\n",max_distance([2,3,5,4],[3,2,5,5,8,7]);
printf "%d\n",max_distance([2,1,11,3],[2,5,10,2]);
printf "%d\n",max_distance([1,2,3],[3,2,1]);
printf "%d\n",max_distance([1,0,2,3],[5,0]);

Wednesday, September 3, 2025

TWC337

Challenge Link

Task1

We count the numbers which are less than each element and store them at that element's index:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;
use List::Util qw(sum0);
use List::MoreUtils qw(frequency);

sub smaller_than_current{
  my %h = frequency(@{$_[0]});
  my @arr;
  foreach my $i(0..$#{$_[0]}) {
    my %h2 = %h{grep{$_ <= $_[0]->[$i]} keys %h};
    @arr[$i] = sum0(values %h2) - 1
  }
  @arr
}

print show smaller_than_current([6,5,4,8]);
print show smaller_than_current([7,7,7,7]);
print show smaller_than_current([5,4,3,2,1]);
print show smaller_than_current([-1,0,3,-2,1]);
print show smaller_than_current([0,1,1,2,0]);

Task2

We do the simulation on a row x col matrix and count the odd numbers:
#!/usr/bin/env perl
use strict;
use warnings;

sub odd_matrix{
  my ($row,$col,$m) = @_;
  my @g;
  push @g,[(0) x $col] foreach(0..$row-1);
  foreach my $p(@$m){
    foreach(0..$col-1) {
      ++$g[$p->[0]]->[$_]
    }
    foreach(0..$row-1){
      ++$g[$_]->[$p->[1]]
    }
  }
  my $res = 0;
  foreach my $i(0..$row-1){
    foreach my $j(0..$col-1) {
      $res++ if $g[$i][$j] % 2
    }
  }
  $res
}

printf "%d\n", odd_matrix(2,3,[[0,1],[1,1]]);
printf "%d\n", odd_matrix(2,2,[[1,1],[0,0]]);
printf "%d\n", odd_matrix(3,3,[[0,0],[1,2],[2,1]]);
printf "%d\n", odd_matrix(1,5,[[0,2],[0,4]]);
printf "%d\n", odd_matrix(4,2,[[1,0],[3,1],[2,0],[0,1]]);