121426
<- Here, 1 is an alternating repetitive digit.
523563
<- Here, NO digit is an alternating repetitive digit.
552523
<- Here, both 2 and 5 are alternating repetitive digits.
333567
<- Here, 3 is an alternating repetitive digit.
I found re.findall(r'(?=(\d)\d\1)',P)
as the solution in editorial but not able to understand it.
Edit - Not allowed to use if
conditions.
You may use this regex using lookaheads:
(\d)(?=\d\1)
RegEx Demo
Explanation:
(\d)
: Match and capture a digit in group #1 (?=
: Start lookahead
\d
: Match any digit\1
: Back-reference to captured group #1)
: End lookaheadYou could do this without a regex using zip()
in a list comprehension:
>>> s = '552523'
>>> [a for a, b in zip(s, s[2:]) if a == b]
['5', '2']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With