DEV Community

Simon Green
Simon Green

Posted on

2 2

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

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay