aboutsummaryrefslogtreecommitdiff
path: root/challenge-095/dave-jacoby/perl/ch-1.pl
blob: b6fe19801a0c3b2e6bf7f18e50bc1c9373b2ebb3 (plain)
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
#!/usr/bin/env perl

use strict;
use warnings;
use feature qw{ say signatures state };
no warnings qw{ experimental };

use Scalar::Util qw{looks_like_number};

my @numbers = ( 1221, -101, 90, 2112, 9, 90.09 );

for my $num (@numbers) {
    my $r = is_palindrome_number($num);
    say qq{Input:   $num};
    say qq{Output:  $r};
    say '';
}

# this is specifically about numbers, so we'll use 
# looks_like_number from Scalar::Util. Otherwise we'll
# assume base-10 and treat it like a decimal, which is
# how Perl likes to stringify numbers. 

# returns 0 if not a number
# returns 0 if not a palindrome
# what remains should only be palindromic numbers,
#   so returns 1

sub is_palindrome_number($num = 0) {
    return 0 unless looks_like_number($num);
    my $mun = join '', reverse split //, $num;
    return 0 unless $mun eq $num;
    return 1;
}