Last active
December 16, 2015 21:50
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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