DEV Community

Simon Green
Simon Green

Posted on

Weekly Challenge 095

Challenge 095

TASK #1 › Palindrome Number

You are given a number $N.

Write a script to figure out if the given number is Palindrome. Print 1 if true otherwise 0.

My solution

This seems pretty straight forward. Check the input is a number, then say 1 if $N and reverse($N) are the same, otherwise 0.

There are probably some edge cases should be thought about if this was the real world. For example, would you consider '121.0' palindromic, because 121 is?

Examples

» ./ch-1.pl 1221
1

» ./ch-1.pl -101
0

» ./ch-1.pl 90
0

» ./ch-1.pl 12.21
1
Enter fullscreen mode Exit fullscreen mode

TASK #2 › Demo Stack

Write a script to demonstrate Stack operations like below:

  • push($n) - add $n to the stack
  • pop() - remove the top element
  • top() - get the top element
  • min() - return the minimum element

My solution

As someone who has written a fair amount of object orientated Perl, this task was also pretty straight forward. I have a stock standard class where new blesses the object and defines the $self->{stack} variable to hold the stack.

I then have methods for the four functions (push, pop, top and min) that do what you would expect them to do.

Example

» ./ch-2.pl
-1
-1
Enter fullscreen mode Exit fullscreen mode

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

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

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay