DEV Community

Discussion on: Daily Challenge #18 - Triple Trouble

yzhernand profile image
Yozen Hernandez • Edited

Here's my Perl solution. Works even if the first triple is not a double in the second number. Also uses regex backreferences, but only uses a regex for the first number. The much faster index is used to find the exact string in the second number.


use v5.24;
use strict;
use warnings;
use feature qw(signatures);
no warnings "experimental::signatures";
use List::Util qw(any);

sub tripledouble ($num1, $num2) {
    any { index($num2, "$_$_") > 0 } $num1 =~ /(\d)\1\1/g;

use Test::More tests => 3;
ok(tripledouble(451999277, 41177722899), "(451999277, 41177722899) has triple/double");
ok(tripledouble(4519992777, 4117772289), "(4519992777, 4117772289) has triple/double");
ok(!tripledouble(45199277, 411777228999), "(4519992777, 4117772289) does not have triple/double");

any from List::Util lets us search all "triples" from the global regex, and as long as one of them matches as a double in $num2, this will return a true value.