diff options
| author | Niels van Dijke <perlboy@cpan.org> | 2021-06-21 13:50:37 +0000 |
|---|---|---|
| committer | Niels van Dijke <perlboy@cpan.org> | 2021-06-21 13:50:37 +0000 |
| commit | a23dcfe91d0ab3f070f4f1d96346e9ec4f687fbc (patch) | |
| tree | e88f60ca716524b454ba4db5f05da0e2c1a499c2 | |
| parent | 4ad5d7152a11a77634488a47aaa4be414426282d (diff) | |
| download | perlweeklychallenge-club-a23dcfe91d0ab3f070f4f1d96346e9ec4f687fbc.tar.gz perlweeklychallenge-club-a23dcfe91d0ab3f070f4f1d96346e9ec4f687fbc.tar.bz2 perlweeklychallenge-club-a23dcfe91d0ab3f070f4f1d96346e9ec4f687fbc.zip | |
Task 1 (attempt #2)
| -rwxr-xr-x | challenge-118/perlboy1967/perl/ch-1.pl | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/challenge-118/perlboy1967/perl/ch-1.pl b/challenge-118/perlboy1967/perl/ch-1.pl new file mode 100755 index 0000000000..ebef294054 --- /dev/null +++ b/challenge-118/perlboy1967/perl/ch-1.pl @@ -0,0 +1,50 @@ +#!/usr/bin/perl + +# Perl Weekly Challenge - 118 +# - https://perlweeklychallenge.org/blog/perl-weekly-challenge-118/#TASK1 +# +# Task 1 - Binary Palindrome +# +# Author: Niels 'PerlBoy' van Dijke + +use v5.16; +use strict; +use warnings; + +use Math::BigInt; + +use Test::More; + +# Prototype(s) +sub isBinaryPalindrome($); + +my %tests = ( + 5 => 1, + 4 => 0, + 9 => 1, + 92 => 0, + # Now some real challenges: + '87654321000012345678' => 0, + '77194726158210796949047323339125271902066562321453396538483931136131497022805' => 1, + '9096277536784585483240416959567103451191883478284251193514416195152412459618527660091914322009559535522570905762842795912065673925408126515314057188586933' => 1, + '390520815125593832371201697557683624491649534912901063643439413447139822863062906277079006273425577853445715844801588127146480334303438634964256432990176608224878768486258600947343601637695607728348109788264339635836155197549535941692743300329974746863355305611317558289413771864738304339390446884778588808422645889858617862446537780027060422416346663958099847404236107613580798319044445091549200574727166165228621771181084035836181195365834105668617904174505103477930561669709938616552640420092052483600909110756129217312104708327535891661407921594300013183081491389611184433598547514094574949978459326057992386614899828467492613606747654322873143331601686076385652190953089604400472524840507492345956054437362719341542264830374240351214439821838436994367564099748207975' => 1, + '30420870501830658244148948732224404217342708033527580850831453232482181974424130263498721816183762713186497654433463101910432392302669795264593430452308705577792439180820164559840712624581697015256080328595081563691884887966022352598882122446213882099281149316168476669542316213722373693620829396128713734980773976974780869098575147279674021211113701671985932165689256627405162912403495055711286767619699102675978272511992832575030990198128609741644635772467941438682368246680914255854766493352308874864233727671409360525102696547280664474358892673919573186256762944290413735188055534315001317678929934077126393950847593950707407597087760686781836671147599520769256723053565731764486148196331601402102994940008093436478378805531201716187295264427922839406420155164083625103641845966094705857884998581973686233845853892253944014152628584058328694791102891690448048438091485903100668725541560014886082050907359188291037811773977401573876011701005604613643633404889391818647294932964017966003081525288752382572500314089057171184548639686361899323807887770533254577435329677589223591692679559260249372091759449793917384279130460488987636758630393185893438906611972048204416758651037784396187329086767968670816979363153092743506130430708990514476677076076379988593166341491306383411909116482005346991092175488409796222756065611971763653644119376384994495350380858876963052860192571384451362017876989943341353274127149466801473037539931725912434119099922995276532144225615942035281332572715510022689749397955371341861066914160368835845080109187964555517587537085073194937627436374786038562561773391814382014839179311823851594182703360755902480395009633743331779731561371551623661080643607686065031005499752336844556402053611296484589931601829085673936805205532631574218322090693883342208418286039836484819416091603596640840198898945225614084025359917071719' => 1, +); + +foreach my $test (keys %tests) { + is(isBinaryPalindrome($test),$tests{$test},sprintf("test: %d digits",length($test))); +} + +done_testing; + +sub isBinaryPalindrome($){ + my ($i) = @_; + + # Only binary numbers with their LSB set can be a binary palindrome + return 0 if (substr($i,-1) eq '0'); + + my $b1 = substr(Math::BigInt->new($i)->as_bin(),2); + my $b2 = join '',reverse split //,$b1; + + return ($b1 eq $b2 ? 1 : 0); +} |
