Two basic tasks from Perl Weekly Challenge 095. Palindrome number is also on leetcode: leetcode/palindrome-number. For languages that coerce values to Int / Str automatically, these two tasks does not require too much code.

## TASK #1 › Palindrome Number

Submitted by: Mohammad S Anwar

You are given a number $N.

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

Example 1:

```
Input: 1221
Output: 1
```

Example 2:

```
Input: -101
Output: 0, since -101 and 101- are not the same.
```

Example 3:

```
Input: 90
Output: 0
```

## Solution #1 > Palindrome Number

It is pretty straightforward: simply convert the input number to string, then check whether that string is palindromic. Based on the example, all negative numbers are non-palindromic. I wonder numbers with decimal points are considered.

Let's say we are only dealing with integers. Here's the implementation in Raku.

```
sub is-palindrome-number (Int $n --> Bool) {
return "$n" eq "$n".flip;
}
```

The operation of flipping a string corresponds to the `flip`

subroutine from `Str`

in Raku. To convert a number `$n`

to string, either `$n.Str`

or `"$n"`

would do. The part where it says `(Int $n --> Bool)`

is the signature of this subroutine. It means that the subroutine takes one `Int`

argument, and returns a `Bool`

(Boolean values. `True`

or `False`

.)

## TASK #2 › Demo Stack

Submitted by: Mohammad S Anwar

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

Example:

```
my $stack = Stack->new;
$stack->push(2);
$stack->push(-1);
$stack->push(0);
$stack->pop; # removes 0
print $stack->top; # prints -1
$stack->push(0);
print $stack->min; # prints -1
```

## Solution #2 > Demo Stack

If we are just defining a class with the abovementioned methods, they can all be delegated to one subroutine in Raku. Let's use this `IntStack`

as an example, which is stack that can hold a bunch of integers.

```
class IntStack {
has Int @!store;
method push(Int $n) {
@!store.push($n);
}
method pop(--> Int) {
@!store.pop;
}
method top(--> Int) {
@!store.tail;
}
method min(--> Int) {
@!store.min;
}
}
```

Which can be used like this:

```
my $stack = IntStack.new;
$stack.push(2);
$stack.push(-1);
$stack.push(0);
$stack.pop;
say $stack.top; #=> -1
$stack.push(0);
say $stack.min; #=> -1
```

## Discussion (0)