Monday, November 17, 2025

TWC348

Challenge Link

Task1

We split the string in half and count the vowels of each half:
#!/usr/bin/env perl
use strict;
use warnings;

sub string_alike{
  my $h = length($_[0]) / 2;
  my $a = (substr $_[0],0,$h) =~ tr/aeiouAEIOU//;
  my $b = (substr $_[0],$h) =~ tr/aeiouAEIOU//;
  $a == $b && $a != 0
}

printf "%d\n",string_alike('textbook');
printf "%d\n",string_alike('book');
printf "%d\n",string_alike('AbCdEfGh');
printf "%d\n",string_alike('rhythmmyth');
printf "%d\n",string_alike('UmpireeAudio')

Task2

We calculate the result by applying the given operations:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub to_min{
  my @parts = split ':',$_[0];
  $parts[0] * 60 + $parts[1]
}

sub convert_time{
  my $diff = (to_min($_[1]) - to_min($_[0]) + 1440) % 1440;
  my $res = 0;
  foreach my $i(qw(60 15 5 1)) {
    $res += $diff / $i;
    $diff %= $i
  }
  $res
}

printf "%d\n",convert_time('02:30','02:45');
printf "%d\n",convert_time('11:55','12:15');
printf "%d\n",convert_time('09:00','13:00');
printf "%d\n",convert_time('23:45','00:30');
printf "%d\n",convert_time('14:20','15:25')

Tuesday, November 11, 2025

TWC347

Challenge Link

Task1

We format the date accordingly:
#!/usr/bin/env perl
use strict;
use warnings;

sub format_date{
  my @s = split ' ',$_[0];
  my $months = "JanFebMarAprMayJunJulAugSepOctNovDec";
  my $day = substr $s[0],0,length($s[0]) - 2;
  my $month = index($months,$s[1]) / 3 + 1;
  sprintf "%s-%02d-%02d",$s[2],$month,$day
}

printf "%s\n",format_date("1st Jan 2025");
printf "%s\n",format_date("22nd Feb 2025");
printf "%s\n",format_date("15th Apr 2025");
printf "%s\n",format_date("23rd Oct 2025");
printf "%s\n",format_date("31st Dec 2025");

Task2

We format the phone number accordingly:
#!/usr/bin/env perl
use strict;
use warnings;

sub format_phone_number{
  my ($phone) = @_;
  s/[- ]//g,
  s/(...)/$1-/g,
  s/-(.?)$/$1/,
  s/(^|-)(\d{2})(\d{2})$/$1$2-$3/ for $phone;
  $phone
}

printf "%s\n",format_phone_number('1-23-45-6');
printf "%s\n",format_phone_number('1234');
printf "%s\n",format_phone_number('12 345-6789');
printf "%s\n",format_phone_number('123 4567');
printf "%s\n",format_phone_number('123 456-78')

Monday, November 3, 2025

TWC346

Challenge Link

Task1

We use a stack to find the maximum number of valid parentheses:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub longest_parenthesis{
  my @s = split '',$_[0];
  my ($max,@stk) = (0,-1);
  foreach my $i(0..$#s) {
    if($s[$i] eq '(') {push @stk,$i}
    else {
      pop @stk;
      if(@stk == 0) {push @stk,$i}
      else {
	$max = max($max,$i - $stk[$#stk])
      }
    }
  }
  $max
}

printf "%d\n",longest_parenthesis('(()())');
printf "%d\n",longest_parenthesis(')()())');
printf "%d\n",longest_parenthesis('((()))()(((()');
printf "%d\n",longest_parenthesis('))))((()(');
printf "%d\n",longest_parenthesis('()(()');

Task2

We use Depth First Search to search for all the possible solutions:
#!/usr/bin/env perl
use strict;
use warnings;
use Algorithm::Combinatorics qw(variations_with_repetition);
use List::Util qw(mesh);
use Data::Show;

sub magic_expression{
  my ($str,$target) = @_;
  my @ds = split '',$str;
  my $it = variations_with_repetition(['+','-',,'*',''],$#ds);
  my @res;
  while(my $ops = $it->next) {
    my $expr = join '',mesh(\@ds,[@$ops,'']);
    next if $expr =~ /\b0[0-9]/;
    my $eval = $expr =~ s/-/+-/gr;
    $eval =~ s/(\d+)\*(\d+)/$1*$2/ge while $eval =~ /\*/;
    $eval =~ s/(-?\d+)\+(-?\d+)/$1+$2/ge while $eval =~ /\+/;
    push @res,$expr if $eval == $target
  }
  @res
}

show magic_expression("123",6);
show magic_expression("105",5);
show magic_expression("232",8);
show magic_expression("1324",10);
show magic_expression("1001",2)