ADDP-P5

No digit twice in a row

Practice 5 of 6Advanced DP13 tests

The problem

Count the whole numbers x with a ≤ x ≤ b in which no two neighbouring digits are equal, written in the usual way with no zeros in front. So 1213 counts, 1223 does not, and 100 does not (two zeros). The number 0 counts: it has one digit.

count_no_repeat(0, 100)    # 91
count_no_repeat(10, 20)    # 10: every number but 11

Limits

  • 0 ≤ a ≤ b ≤ 10¹⁸, so counting the numbers one by one is impossible.

Builds on ADDP-5 Digit DP.

Continue on a laptop

Practice runs Python in the browser and needs a keyboard and a wider screen. Read the problem and open hints here, then write your solution on a laptop. Progress is saved per browser for now, so hints opened here stay on this device.

Need a hint?0 of 3 opened

  1. Hint 1Nudge
  2. Hint 2Approach Open hint 1 first
  3. Hint 3Pseudocode Open hint 2 first
  4. Worked solutionlast resort Opens after all 3 hints
Python waits for a wider screensolution.py

Ctrl + Enter runs the tests. Tab indents; press Esc, then Tab, to leave the editor.

13 tests, not run yet
Test results
#TestResultReads · time
1One to nineNot run
2ZeroNot run
3Up to 100Not run
4From 10 to 20Not run
5A single bad numberNot run
6A single good numberNot run
7Leading zeros are not digitsNot run
8Counting from aNot run
9Up to 10¹⁸Not run
10A big rangeNot run
11A tight path that breaks earlyNot run
12From ten nines to 10¹¹Not run
13Many rangesNot run