I have a problem where I need to find the number of unique numbers. No digits in the number would have been repeated.
Number like 11, 1123, 41124 wont qualify.
Numbers should be 1234, 12, or 987 like these would qualify.
My code works fine. But when the range go beyond 10^6. The code takes lots of time to execute. How can I optimize the same.
for i in xrange(10000000):# pow(10, n) 10, 100, 10000,...
j = str(i)
if j != j[::-1]:
for k in range(len(j)):
if j.count(j[k]) == 1:
if k == len(j)-1:
count += 1
#print j
else:
break
if len(j) == 1:
count += 1
print count
i,j,k-- it's very hard to keep up with what is what. (2.) Stop usingrange(len(j))and such. It's just going to cause off-by-one errors and make things more difficult to read later (j.count(j[k]). (3.) Usecollections.Counterfor tasks like this.contains_repeated_digits = lambda n: any(v > 1 for v in Counter(str(n)).values())Don't use lambda. It's just an example.