Hmmm.... reading other answers I might have misinterpreted what was required in this challenge. What is it exactly? For example, if num1 = 121 and num2 = 141, would they be triple troubled numbers? (because 7*3 = 21, and 7*2 =14) Or would it be num1 = 8777and num2 = 877 be triple troubled numbers? (because 7 repeats 3 times in the first one and 2 in the second one)
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.
#!/usr/bin/perlusev5.24;usestrict;usewarnings;usefeatureqw(signatures);nowarnings"experimental::signatures";useList::Utilqw(any);sub tripledouble($num1, $num2) {any{index($num2,"$_$_")>0}$num1=~/(\d)\1\1/g;}useTest::Moretests=>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.
Used a regex to find groups of 3 digits. Then checked if the digits were the same. If they were we check the second number for the digit twice, again using a regex!
JavaScript
A quick solution using JavaScript, I need to improve it later. Live demo on CodePen.
Hmmm.... reading other answers I might have misinterpreted what was required in this challenge. What is it exactly? For example, if num1 = 121 and num2 = 141, would they be triple troubled numbers? (because 7*3 = 21, and 7*2 =14) Or would it be num1 = 8777and num2 = 877 be triple troubled numbers? (because 7 repeats 3 times in the first one and 2 in the second one)
I'm getting caught up a few days behind, but I understood this is three digits in a row. Which I think is confirmed by the linked challenge examples.
So
num1 = 8777 and num2 = 877would return true for this!Starting on my version now
To achieve that, the only change would be in the
ifstatement. Instead of doingx*3andx*2, it would be''+x+x+xand''+x+xrespectively: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
indexis used to find the exact string in the second number.anyfrom 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.A python solution. Assumes numbers are strings.
Rust Solution!
Used a regex to find groups of 3 digits. Then checked if the digits were the same. If they were we check the second number for the digit twice, again using a regex!
Seems like a good problem for regex with backreferences. JavaScript:
A day late to the party, but here's my solution:
Gist: gist.github.com/kerrishotts/85214d...
Ruby solution: