Monday, May 5, 2025

TWC320

Challenge Link

Task1

We find the max of count of negative and positive numbers:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub maximum_count{
  my ($n,$p) = (0,0);
  map{if($_ != 0) {$_ < 0 ? $n++ : $p++}} @{$_[0]};
  max($n,$p)
}

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

Task2

We find the absolute value of sum of array and sum of each element's digits in the array:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub sum_diffrence{
  abs(sum0(@{$_[0]}) - sum0(split '',join '',@{$_[0]}))
}

printf "%d\n",sum_diffrence([1,23,4,5]);
printf "%d\n",sum_diffrence([1,2,3,4,5]);
printf "%d\n",sum_diffrence([1,2,34]);

No comments:

Post a Comment