the code:
def repeatingDigits(digits): pattern = set(digits.lstrip("0")) print(pattern)
if len(pattern) > 1:
return(False)
if len(pattern) == 1:
return(True)
repeatingDigits("0111") ''TRUE'' repeatingDigits("0112") ''FALSE''
Use the regex: ^0*([1-9])\1*$
Explanation:
^
: begin searching at start of string0*
: search for any repeated 0's([1-9])
: match digits other than 0 and remember it\1*
: match one or more instances of the previously matched digit$
: end of stringThe anchor tokens ^ and $ allow weeding out multiple occurrence of recurring digits. Python code:
import re
def repeatingDigits(digits):
pattern = r"^0*([1-9])\1*$"
return re.search(pattern, digits)