One for the speed merchants today, as all we have to do is count the six-digit numbers in a given range where (a) no digit is ever smaller than its predecessor and (b) there is an adjacent pair of the same digit.
def digits_ascend(n): s = str(n) for i in range(0, len(s)-1): if s[i] > s[i+1]: return False return True def adjacent_digit_pair(n): s = str(n) for i in range(0, 10): if 2 * str(i) in s: return True return False print(len([n for n in range(156218, 652528) if digits_ascend(n) and adjacent_digit_pair(n)]))
The second star throws in the minor wrinkle that a pair of digits is valid if and only if it is strictly a pair, i.e. not part of a threesome, foursome or moresome. Not much of a change required to continue the above approach:
def adjacent_digit_pair_strict(n): s = str(n) for i in range(0, 10): if 2 * str(i) in s and 3 * str(i) not in s: return True return False