DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 107

Challenge, My solutions

TASK #1 › Self-descriptive Numbers

Task

Write a script to display the first three self-descriptive numbers.

My solution

say '1210, 2020, 21200'. On to the next task. Not so quickly :) While the above would produce the expected output, that's not the spirit of what the weekly challenge is about.

For this task I ran an incrementing loop that exited once three self-descriptive numbers were found. The main part of the logic appears in the _is_self_descriptive subroutine. I first convert the number into an array of occurrences. For example 12140 would be turned into 1,2,1,0,1,0,0,0,0,0. I then compare this with the digit in the position, and return false undef or true 1 if there is a match.

Example

» ./ch-1.pl 
1210, 2020, 21200
Enter fullscreen mode Exit fullscreen mode

TASK #2 › List Methods

Task

Write a script to list methods of a package/class.

My solution

For caveats first.

  • In the real world, I would simply use Class::Inspector's methods function. There is no need to re-invent a perfectly working wheel.
  • My solution won't list inherited methods. We could walk the @ISA tree, but this seems more complicated than required for the task.
  • Likewise it won't show autoloaded methods.
  • My code is loosely based on Class::Inspector

And with all of that, there isn't much else to say. I require the package, and then look up everything in its table, and exclude everything that it's a sub-routine (e.g variables).

If a package is not supplied, then Calc is assumed.

Example

» perl -I . ./ch-2.pl 
div
new
add
mul
Enter fullscreen mode Exit fullscreen mode

Top comments (0)