Monday, February 16, 2026

TWC361

Challenge Link

Task1

We calculate the zeckendorf representation of the given number:
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Show;

sub zeckendorf_representation{
  my ($n) = @_;
  my @fibs = (1,2);
  push @fibs,$fibs[-1] + $fibs[-2] while($fibs[-1] + $fibs[-2] <= $n);
  
  my ($i,$rem,@parts) = ($#fibs,$n);
  while($rem) {
    if($fibs[$i] <= $rem) {
      push @parts,$fibs[$i];
      $rem -= $fibs[$i];
      $i -= 2;
    } else {
      --$i
    }
  }
  @parts
}

show zeckendorf_representation(4);
show zeckendorf_representation(12);
show zeckendorf_representation(20);
show zeckendorf_representation(96);
show zeckendorf_representation(100);

Task2

We find person who doesn't know anybody else:
#!/usr/bin/env perl
use strict;
use warnings;

sub find_celebrity{
  my ($party) = @_;
  my $n = @$party;
  return -1 if $n == 0;

  my $cand = 0;
  foreach my $i(1..$n-1) {
    $cand = $i if($party->[$cand][$i]);
  }
  
  foreach my $i(0..$n-1) {
    next if $i == $cand;
    return -1 if($party->[$cand][$i] || !$party->[$i][$cand]);
  }
  $cand
}

printf "%d\n",find_celebrity([[0,0,0,0,1,0],
			      [0,0,0,0,1,0],
			      [0,0,0,0,1,0],
			      [0,0,0,0,1,0],
			      [0,0,0,0,0,0],
			      [0,0,0,0,1,0]]);
printf "%d\n",find_celebrity([[0,1,0,0],
			      [0,0,1,0],
			      [0,0,0,1],
			      [1,0,0,0]]);
printf "%d\n",find_celebrity([[0,0,0,0,0],
			      [1,0,0,0,0],
			      [1,0,0,0,0],
			      [1,0,0,0,0],
			      [1,0,0,0,0]]);
printf "%d\n",find_celebrity([[0,1,0,1,0,1],
			      [1,0,1,1,0,0],
			      [0,0,0,1,1,0],
			      [0,0,0,0,0,0],
			      [0,1,0,1,0,0],
			      [1,0,1,1,0,0]]);
printf "%d\n",find_celebrity([[0,1,1,0],
			      [1,0,1,0],
			      [0,0,0,0],
			      [0,0,0,0]]);
printf "%d\n",find_celebrity([[0,0,1,1],
			      [1,0,0,0],
			      [1,1,0,1],
			      [1,1,0,0]]);

Monday, February 9, 2026

TWC360

Challenge Link

Task1

We justify the text according to the given width:
#!/usr/bin/env perl
use strict;
use warnings;

sub text_justifier{
  my $diff = $_[1] - length($_[0]);
  die "Length too short!" if($diff < 0);
  my $l = int($diff / 2);
  my $r = $diff - $l;
  '*' x $l . $_[0] . '*' x $r
}

printf "%s\n",text_justifier('Hi',5);
printf "%s\n",text_justifier('Code',10);
printf "%s\n",text_justifier('Hello',9);
printf "%s\n",text_justifier('Perl',4);
printf "%s\n",text_justifier('A',7);
printf "%s\n",text_justifier('',5);

Task2

We sort the case-folded words and join them back into a string:
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(fc);

sub word_sorter{
  join ' ',sort {fc($a) cmp fc($b)} split /\s+/,$_[0];
}

printf "%s\n",word_sorter('The quick brown fox');
printf "%s\n",word_sorter('Hello    World!   How   are you?');
printf "%s\n",word_sorter('Hello');
printf "%s\n",word_sorter('Hello, World! How are you?');
printf "%s\n",word_sorter('I have 2 apples and 3 bananas!');

Sunday, February 8, 2026

TWC359

Challenge Link

Task1

We keep on summing the digits of the number until we reach a number with a single digit:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub digital_root {
  my ($n) = @_;
  my $c = 0;
  $c++, $n = sum0 split '',$n while $n > 9;
  printf "Persistence  = %d\nDigital Root = %d\n",$c,$n
}

digital_root(38);
digital_root(7);
digital_root(999);
digital_root(1999999999);
digital_root(101010);

Task2

We keep on removing duplicate characters until there's none:

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

sub string_reduction{
  my ($s) = @_;
  1 while $s =~ s/(\w)\1//g;
  $s
}

printf "%s\n",string_reduction('aabbccdd');
printf "%s\n",string_reduction('abccba');
printf "%s\n",string_reduction('abcdef');
printf "%s\n",string_reduction('aabbaeaccdd');
printf "%s\n",string_reduction('mississippi');

Sunday, December 28, 2025

TWC353

Challenge Link

Task1

We find the sentence with maximum words:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(max);

sub max_words{
  max map{scalar split} @{$_[0]}
}

printf "%d\n",max_words(['Hello world',
			 'This is a test','Perl is great']);
printf "%d\n",max_words(['Single']);
printf "%d\n",max_words(['Short',
			 'This sentence has six words in total',
			 'A B C',
			 'Just four words here']);
printf "%d\n",max_words(['One','Two parts','Three part phrase','']);
printf "%d\n",max_words(['The quick brown fox jumps over the lazy dog',
			 'A',
			 'She sells seashells by the seashore',
			 'To be or not to be that is the question']);

Task2

We simulate the solution and check for the given conditions:
#!/usr/bin/env perl
use strict;
use warnings;

sub validate_coupon{
  my %valid;
  @valid{qw(electronics grocery pharmacy restaurant)} = 1;
  map {$_[0]->[$_] =~ /^[_0-9a-zA-Z]+$/ &&
	 exists $valid{$_[1]->[$_]} &&
	 $_[2]->[$_] eq 'true' ? 1 : 0} 0..$#{$_[0]}
}

printf "(%s)\n", join ',',
  validate_coupon(['A123','B_456','C789','D@1','E123'],
		  ['electronics','restaurant','electronics',
		   'pharmacy','grocery'],
		  ['true','false','true','true','true']);
printf "(%s)\n", join ',',
  validate_coupon(['Z_9','AB_12','G01','X99','test'],
		  ['pharmacy','electronics','grocery',
		   'electronics','unknown'],
		  ['true','true','false','true','true']);
printf "(%s)\n", join ',',
  validate_coupon(['_123','123','','Coupon_A','Alpha'],
		  ['restaurant','electronics','electronics',
		   'pharmacy','grocery'],
		  ['true','true','false','true','true']);
printf "(%s)\n", join ',',
  validate_coupon(['ITEM_1','ITEM_2','ITEM_3','ITEM_4'],
		  ['electronics','electronics','grocery','grocery'],
		  ['true','true','true','true']);
printf "(%s)\n", join ',',
  validate_coupon(['CAFE_X','ELEC_100','FOOD_1','DRUG_A','ELEC_99'],
		  ['restaurant','electronics','grocery',
		   'pharmacy','electronics'],
		  ['true','true','true','true','false']);

Monday, December 15, 2025

TWC352

Challenge Link

Task1

We return every string that is a substring of amother:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(any);

sub match_string{
  my ($arr) = @_;
  my @res;
  foreach my $i(0..$#$arr) {
    my $x = $arr->[$i];
    foreach my $j(0..$#$arr) {
      my $y = $arr->[$j];
      if($i != $j && index($y,$x) != -1 && !any{$_ eq $x} @res) {
	push @res,$x;
	last
      }
    }
  }
  @res
}

printf "(%s)\n",join ', ',match_string(['cat','cats','dog','dogcat',
				       'dogcat','rat','ratcatdogcat']);
printf "(%s)\n",join ', ',
  match_string(['hello','hell','world','wor','ellow','elloworld']);
printf "(%s)\n",join ', ',
  match_string(['a', 'aa', 'aaa', 'aaaa']);
printf "(%s)\n",join ', ',
  match_string(['flower','flow','flight','fl','fli','ig','ght']);
printf "(%s)\n",join ', ',
  match_string(['car','carpet','carpenter','pet',
		'enter','pen','pent']);

Task2

We check if the accumulated binary number is divisible by 5 and accumulate the boolean results in an array:
#!/usr/bin/env perl
use strict;
use warnings;

sub binary_prefix{
  my @res;
  my $x = 0;
  foreach(@{$_[0]}){
    $x = ($x << 1 | $_) % 5;
    push @res,$x == 0 ? 1 : 0;
  }
  @res
}

printf "(%s)\n", join ', ', binary_prefix([0,1,1,0,0,1,0,1,1,1]);
printf "(%s)\n", join ', ', binary_prefix([1,0,1,0,1,0]);
printf "(%s)\n", join ', ', binary_prefix([0,0,1,0,1]);
printf "(%s)\n", join ', ', binary_prefix([1,1,1,1,1]);
printf "(%s)\n", join ', ', binary_prefix([1,0,1,1,0,1,0,0,1,1]);

Friday, December 12, 2025

TWC351

Challenge Link

Task1

We sort the array and remove the minimum and maximum elements then take the average:
#!/usr/bin/env perl
use strict;
use warnings;
use List::Util qw(sum0);

sub special_average{
  my ($arr) = @_;
  return 0 if @$arr < 2;
  @$arr = sort {$a <=> $b} @$arr;
  my @sub = splice @$arr,1,$#$arr-1;
  (sum0 @sub) / @sub
}

printf "%d\n",special_average([8000,5000,6000,2000,3000,7000]);
printf "%d\n",special_average([100_000,80_000,110_000,90_000]);
printf "%d\n",special_average([2500,2500,2500,2500]);
printf "%d\n",special_average([2000]);
printf "%d\n",special_average([1000,2000,3000,4000,5000,6000]);

Task2

We sort the array and then check if the distance of all elements is the same:
#!/usr/bin/env perl
use strict;
use warnings;

sub arithmetic_progression{
  my ($arr) = @_;
  @$arr = sort {$a <=> $b} @$arr;
  my $d = abs(shift(@$arr) - shift(@$arr));
  for(my $i = 0; $i < $#$arr-1; ++$i){
    return 0 if abs($arr->[$i] - $arr->[$i+1]) != $d
  }
  1
}

printf "%d\n",arithmetic_progression([1,3,5,7,9]);
printf "%d\n",arithmetic_progression([9,1,7,5,3]);
printf "%d\n",arithmetic_progression([1,2,4,8,16]);
printf "%d\n",arithmetic_progression([5,-1,3,1,-3]);
printf "%d\n",arithmetic_progression([1.5,3,0,4.5,6]);

Friday, December 5, 2025

TWC350

Challenge Link

Task1

We count all strings of length 3 with no repeating characters:
#!/usr/bin/env perl
use strict;
use warnings;

sub good_substrings{
  scalar grep {!/(.).*\1/} $_[0] =~ /(?=(...))/g
}

printf "%d\n",good_substrings('abcaefg');
printf "%d\n",good_substrings('xyzzabc');
printf "%d\n",good_substrings('aababc');
printf "%d\n",good_substrings('qwerty');
printf "%d\n",good_substrings('zzzaaa');

Task2

We find all shuffle pairs between the given ranges:
#!/usr/bin/env perl
use strict;
use warnings;

sub shuffle_pairs{
  my ($from,$to,$count) = @_;
  my $found = 0;
  foreach my $n($from..$to){
    my $c = 0;
    my $s = join '',sort split '',$n;
    foreach my $w(2..9){
      my $m = $n * $w;
      next if length $m > length $n;
      next if $s ne join '',sort split '',$m;
      ++$c
    }
    ++$found if $c >= $count
  }
  $found
}

printf "%d\n",shuffle_pairs(1,1000,1);
printf "%d\n",shuffle_pairs(1500,2500,1);
printf "%d\n",shuffle_pairs(1_000_000,1_500_000,5);
printf "%d\n",shuffle_pairs(13_427_000,14_100_000,2);
printf "%d\n",shuffle_pairs(1030,1130,1);