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

Top comments (0)