data = "172930-683082".split("-") min_, max_ = int(data[0]), int(data[1]) def validate(num): repeats = False repeats_twice = False repeatlen = 0 lastc = None nums = str(num) for i, c in enumerate(nums): if lastc: if c < lastc: return False # not rising if c == lastc: repeats = True if repeatlen == 0: repeatlen = 2 else: repeatlen += 1 else: if repeatlen == 2: repeats_twice = True repeatlen = 0 lastc = c return repeats and (repeats_twice or repeatlen == 2) matches = 0 for i in range(min_, max_): if validate(i): matches += 1 print(matches) # print(validate(123466))