Robbie Hatley’s Solutions, in Perl, for The Weekly Challenge #361 (“Zeckendorf Representation” and “Find Celebrity”)

For those not familiar with "The Weekly Challenge", it is a weekly programming puzzle with two parts, with a new pair of tasks each Monday. You can find it here: The Weekly Challenge

The Weekly Challenge for the week of 2026-02-16 through 2026-02-22 is #361. The tasks for challenge #361 are as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 361-1: Zeckendorf Representation
Submitted by: Mohammad Sajid Anwar
You are given a positive integer (<= 100). Write a script to
return Zeckendorf Representation of the given integer.
Every positive integer can be uniquely represented as sum of
non-consecutive Fibonacci numbers.

Example 1
Input: $int = 4
Output: 3,1
4 => 3 + 1 (non-consecutive Fibonacci numbers)

Example 2
Input: $int = 12
Output: 8,3,1
12 => 8 + 3 + 1

Example 3
Input: $int = 20
Output: 13,5,2
20 => 13 + 5 + 2

Example 4
Input: $int = 96
Output: 89,5,2
96 => 89 + 5 + 2

Example 5
Input: $int = 100
Output: 89,8,3
100 => 89 + 8 + 3

To solve this problem, I'll greedily gobble Fibonacci numbers from $int until it becomes zero, then return the numbers gobbled.

   $out = '*'x$lpw . $str . '*'x$rpw

Robbie Hatley's Perl Solution to The Weekly Challenge 361-1

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Task 361-2: Find Celebrity
Submitted by: Mohammad Sajid Anwar
You are given a binary matrix (m x n). Write a script to find
the celebrity, return -1 when none found. A celebrity is
someone, everyone knows and knows nobody.

(
   # Example 1 input:
   [
      [0, 0, 0, 0, 1, 0],  # 0 knows 4
      [0, 0, 0, 0, 1, 0],  # 1 knows 4
      [0, 0, 0, 0, 1, 0],  # 2 knows 4
      [0, 0, 0, 0, 1, 0],  # 3 knows 4
      [0, 0, 0, 0, 0, 0],  # 4 knows NOBODY
      [0, 0, 0, 0, 1, 0],  # 5 knows 4
   ],
   # Expected output: 4

   # Example 2 input:
   [
      [0, 1, 0, 0],        # 0 knows 1
      [0, 0, 1, 0],        # 1 knows 2
      [0, 0, 0, 1],        # 2 knows 3
      [1, 0, 0, 0],        # 3 knows 0
   ],
   # Expected output: -1

   # Example 3 input:
   [
      [0, 0, 0, 0, 0],     # 0 knows NOBODY
      [1, 0, 0, 0, 0],     # 1 knows 0
      [1, 0, 0, 0, 0],     # 2 knows 0
      [1, 0, 0, 0, 0],     # 3 knows 0
      [1, 0, 0, 0, 0],     # 4 knows 0
   ],
   # Expected output: 0

   # Example 4 input:
   [
      [0, 1, 0, 1, 0, 1],  # 0 knows 1, 3, 5
      [1, 0, 1, 1, 0, 0],  # 1 knows 0, 2, 3
      [0, 0, 0, 1, 1, 0],  # 2 knows 3, 4
      [0, 0, 0, 0, 0, 0],  # 3 knows NOBODY
      [0, 1, 0, 1, 0, 0],  # 4 knows 1, 3
      [1, 0, 1, 1, 0, 0],  # 5 knows 0, 2, 3
   ],
   # Expected output: 3

   # Example 5 input:
   [
      [0, 1, 1, 0],        # 0 knows 1 and 2
      [1, 0, 1, 0],        # 1 knows 0 and 2
      [0, 0, 0, 0],        # 2 knows NOBODY
      [0, 0, 0, 0],        # 3 knows NOBODY
   ],
   # Expected output: -1

   # Example 6 input:
   [
      [0, 0, 1, 1],        # 0 knows 2 and 3
      [1, 0, 0, 0],        # 1 knows 0
      [1, 1, 0, 1],        # 2 knows 0, 1 and 3
      [1, 1, 0, 0],        # 3 knows 0 and 1
   ],
   # Expected output: -1
);

To solve this problem, I simply look for a row of 0s at a vertical index $i such that all other rows have "1" at vertical index $i. That's easily determined by applying sum0 to vertical slice $i; a celebrity will always have a sum of ($i-1). (Celebrities can never be self-aware, because that would involve knowing someone. And there can never be more than one celebrity, because that would violate "everyone knows them".)

Robbie Hatley's Perl Solution to The Weekly Challenge 361-2

That's it for challenge 361; see you on challenge 362!

Comments

Popular posts from this blog

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #334 (“Range Sum” and “Nearest Valid Point”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #336 (“Equal Group” and “Final Score”)

Robbie Hatley's Solutions, in Perl, for The Weekly Challenge #326 (“Day of Year” and “Decompressed List”)