# 解 Perl Weekly Challenge 095 -- 回文數與堆疊

Perl Weekly Challenge 095 兩題目算是基本題吧。回文數 (Palindrome Number) 這題也有出現在 leetcode: leetcode/palindrome-number。對於 Perl 或 Raku 這類字串與整數都自動無縫轉換的語言來說，是個很省事的題目。

## 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.

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
``````

## 解 #1 > Palindrome Number

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

``````(defn is-palindrome
"Tell if the given string is a palindrome"
[s]
(= s (string/reverse s)))

(defn is-palindrome-number
"Tell if the given number is palindromic"
[n]
(is-palindrome (string n)))``````

## 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

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
``````

## 解 #2 > Demo Stack

``````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;
}
}``````

``````    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``````