Monday, March 3, 2025

TWC311

Challenge Link

Task1

We swap the case of each letter in the string:

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

sub upper_lower{
  $_[0] =~ tr/A-Za-z/a-zA-Z/r
}

printf "%s\n",upper_lower('pERl');
printf "%s\n",upper_lower('rakU');
printf "%s\n",upper_lower('PyThOn');

Task2

We sum the digits of each group while the length of the string is greater than n:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub group_digit_sum{
  my ($str,$n) = @_;
  while(length $str > $n){
    my @gps = unpack("(A$n)*",$str);
    $str = join('',map{sum0 split '',$_} @gps)
  }
  $str
}

printf "%s\n",group_digit_sum('111122333',3);
printf "%s\n",group_digit_sum('1222312',2);
printf "%s\n",group_digit_sum('100012121001',4);