[Solved] Count of non-repeating digits


The question is ambiguous, since there’s two reasonable ways of interpreting “repeated digit”.

  • First, a number has a “repeated digit” if it has the same digit twice or more in succession. For example 12334 has a repeated digit (3).

  • Second, a number has a repeated digit if the same digit appears twice. So 1231 would not have a repeated digit in the first sense, but would in the this sense (1 repeats).

We can find which meaning is correct by checking the test cases they give.

def nonrep1(n):
    return sum(all(c != d for c, d in zip(str(i), str(i)[1:])) for i in xrange(1, n+1))

def nonrep2(n):
    return sum(all(str(i).count(d) < 2 for d in '0123456789') for i in xrange(1, n+1))

assert nonrep1(7) == 7
assert nonrep1(3456) == 2562

assert nonrep2(7) == 7
assert nonrep2(3456) == 2562

The final assertion fails, so the first meaning of “non repeated” is the one intended by the problem-setter.

I have written slightly silly terse versions of the programs so you can still write your own code now that you understand what the problem is asking.

2

solved Count of non-repeating digits