/
ch-1.raku
60 lines (45 loc) · 1.07 KB
/
ch-1.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
=begin comment
Perl Weekly Challenge
https://perlweeklychallenge.org/blog/perl-weekly-challenge-095/
095-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
=end comment
class Palindrome {
has Int $.num;
method is_palindrome() {
return 0 unless $.num.chars %% 2;
return 0 if $.num < 0;
my @digits = $!num.Str.split('');
while @digits.elems {
return 0 if @digits.shift ne @digits.pop;
}
return 1;
}
}
multi sub MAIN( Int $num ) {
say "\nInput: " ~ $num;
say 'Output: ' ~ Palindrome.new( num => $num ).is_palindrome;
}
multi sub MAIN( 'challenge' ) {
<1221 -101 90>.map: { MAIN( .Int ) };
}
multi sub MAIN( 'test' ) {
use Test;
my %test = 1221 => 1, -101 => 0, 90 => 0, 123321 => 1, 10100101 => 1 ;
for %test.keys {
is(
Palindrome.new( num => $_.Int ).is_palindrome,
%test{$_}
);
}
}