-3

I am playing around with Python and I would like to print each number form 0000 to 9999 but only if the numbers do not repeat itself.

I started like this just to print every number:

for i in range(0000, 10000):
   print(f"Checking: {1:04}")

that prints every number up to 9999 and i would like to print each number that does not have a repeated number in them. this was my attempt:

for i in range(0000, 10000):
   for n in i:
      if i[n] != i[n + 1]
         print(f"checking: {i:04}")

and the error that prints is:

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    for n in i:
TypeError: 'int' object is not iterable

thanks, any tip is appreciated!

AMC
  • 2,642
  • 7
  • 13
  • 35
diego
  • 409
  • 4
  • 12
  • using integer division and modulo we can verify the uniqueness of a number; – Aditya Jun 26 '20 at 19:06
  • The error means that you cannot iterate over an integer type; – Aditya Jun 26 '20 at 19:06
  • This fails because int objects aren't iterable, as in they can't be looped through like a string. One, somewhat hacky, way to get around this would be to convert the integer to a string and then loop through that. Additionally, you would need to prepend the proper number of zeros, because ints ignore superfluous digits. –  Jun 26 '20 at 19:08
  • You can work with `str(i)`, which is iterable. However, the condition that you tried to use, if fixed, would print those numbers that are not of the form aaaa. Is that what you wanted? Or do you also want to skip numbers where a digit is repeated but not in all four positions, like abcb? – NotDijkstra Jun 26 '20 at 19:09
  • If I understand the question correctly, nothing under 123 is going to work mostly because of two zeros. – Mark Ransom Jun 26 '20 at 19:20
  • @MarkRansom On the other hand, in their attempt, they tried to print as soon as they found a digit different from the one following it. So, I am not sure if they want to skip 120 or not. – NotDijkstra Jun 26 '20 at 19:24
  • @NotDijkstra yes that is what i wanted. but how would I have to change for the code to print also the abca? – diego Jun 26 '20 at 21:06
  • @MarkRansom really nice, helpful and deep analysis of the code ;) – diego Jun 26 '20 at 21:07
  • So does `0101` pass or get rejected by the rules you're using? – Mark Ransom Jun 26 '20 at 21:12
  • What do you understand from that error message? – AMC Jun 26 '20 at 21:14
  • Does this answer your question? [Python - TypeError: 'int' object is not iterable](https://stackoverflow.com/questions/19523563/python-typeerror-int-object-is-not-iterable) – AMC Jun 26 '20 at 21:15

1 Answers1

1

You can first convert each number into a str, then a set().

If the amount of elements in to set is the same amount of elements in the original string,
that means there are no repeated digits in the number.

for i in range(0000, 10000):
    if len(set(str(i))) == len(str(i)):
        print(i)

Output:

0
1
2
3
4
5
6
7
8
9
10
12
13
14
15
16
17
18
19
20
21
23
24
25
26
27
28
...
9846
9847
9850
9851
9852
9853
9854
9856
9857
9860
9861
9862
9863
9864
9865
9867
9870
9871
9872
9873
9874
9875
9876
Red
  • 26,798
  • 7
  • 36
  • 58