DEV Community

Simon Green
Simon Green

Posted on

3 2

Weekly Challenge 108

Tasks, My solutions

TASK #1 › Locate Memory

Task

Write a script to declare a variable or constant and print it’s[sic] location in the memory.

My solution

One good thing about Perl (and most modern languages) is that you don't really need to worry about memory management. Perl will automatically reclaim memory when a variable is no longer used.

For this task, I take the reference to the variable (for example SCALAR(0x559184ba5890) and use regular expression to display the location (the bit between the parenthesis).

Minified

In 22 characters, this can be minified to the below example.

» perl -E 'say\$a=~/(0x[0-9a-f]+)/'
0x555668d2a3f8
Enter fullscreen mode Exit fullscreen mode

Example

» ./ch-1.pl 
0x555668d2a3f8
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Bell Numbers

Task

Write a script to display top 10 Bell Numbers.

My solution

In tackling this task, the animated gif is very helpful. I use the @bell array to reproduce this. I start by seeding the table with a single value, and then use the copy and add functions to generate the subsequent values. Finally I use a foreach loop to display the last number in each row to display the list of bell numbers.

Example

» ./ch-2.pl
1
2
5
15
52
203
877
4140
21147
115975
Enter fullscreen mode Exit fullscreen mode

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

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