Skip to content

Instantly share code, notes, and snippets.

@imiric
Last active December 16, 2015 21:50
Show Gist options
  • Save imiric/5503028 to your computer and use it in GitHub Desktop.
Save imiric/5503028 to your computer and use it in GitHub Desktop.
Finds palindrome numbers within a list of ranges. Input file here: https://gist.github.com/hminaya/5435673
#!/usr/bin/env perl
use strict;
use List::Util qw(min max);
my (@palindromes, @ranges, @ranges_flat, $lo, $hi);
open FILE, "seed.txt" or die $!;
while (<FILE>) {
chomp;
push(@ranges, [split(' ', $_)]);
}
close(FILE);
@ranges_flat = map {@$_} @ranges;
$lo = min @ranges_flat;
$hi = max @ranges_flat;
while ($hi >= $lo) {
if ($lo == reverse $lo) {
push(@palindromes, $lo);
}
$lo++;
}
my $total = 0;
foreach (@ranges) {
my $pcount = 0;
my ($lo, $hi) = @{$_};
foreach (@palindromes) {
if ($_ >= $lo && $_ <= $hi) {
$pcount++;
}
}
$total += $pcount;
}
print "$total\n";
$ perl -v
This is perl 5, version 16, subversion 3 (v5.16.3) built for i686-linux-thread-multi
$ time perl capicua.pl
106814
perl capicua.pl 0.64s user 0.00s system 99% cpu 0.647 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment