I need to print all the numbers in the list that do not have same digits ex: 22,33.
x,y=list(map(int,input().split()))
l=[]
m=[]
for i in range(x,y+1):
l.append(i)
for j in range(0,len(l)):
unit = l[j] % 10
while (l[j] != 0):
curr = l[j] %10
l[j]=l[j]//10
if(curr != unit):
m.append(l[j])
print(m)
Sample input: 10,20
Sample output: 10,12,13,14,15,16,17,18,19,20
(11 is removed)
my output: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
According to your sample of input and output, you want to remove the numbers which have the same digits.
So here is the solution:
x, y = list(map(int,input().split()))
ans = []
for i in range(x, y + 1):
list_num = list(str(i))
if list_num.count(str(i % 10)) != len(str(i)):
ans.append(i)
print(*ans)
range()
returns an iterator. You dont care about previous or future numbers, so you dont need to store any lists other than your outputs
Focus on isolating your logic. What does it mean for a number to have all the same digit? For instance, if you uniquely count all digits to be 1, then you have the same digit. Make it a function
from collections import defaultdict
def all_same(x):
vals = defaultdict(int)
while x != 0:
x, tens = divmod(x, 10)
vals[tens] += 1
return len(vals) == 1
x, y = list(map(int,input().split()))
print(','.join( str(v) for v in range(x, y + 1) if not all_same(v) ))
x, y = list(map(int, input().split()))
result = []
for i in range(x, y+1):
counts = [0] * 10
n = i
while n > 0:
n, remainder = divmod(n, 10)
counts[remainder] += 1
if max(counts) == 1:
result.append(i)
If you are allowed to convert your number to string you can do
result = [n for n in range(x, y + 1) if len(str(n)) == len(set(str(n)))]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.