Home > Mobile >  Where can c(digit) be found in n(number)
Where can c(digit) be found in n(number)

Time:12-12

n = int(input("Unesite n: "))
c = int(input("Unesite c: "))

def where(n, c):
    where = []
    for i in range(1, n   1):
        dig = i
        while dig > 0:
            if dig % 10 == c:
                where.append(i)
                break
            dig //= 10
    print(where)

The answer to this question is probably stupid but... :/ For input n=22 and c=1 output is [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21]. I am curious how to remove [] from the output, f string, or something? I want the output to look something like "From number 1 to {n}, digit {c} is shown in numbers: {where(n, c)}.

CodePudding user response:

def where(n, c):
    where = []
    for i in range(1, n   1):
        dig = i
        while dig > 0:
            if dig % 10 == c:
                where.append(i)
                break
            dig //= 10
    for e in where:
        print(e, end= " ")

then the output will be

1 10 11 12 13 14 15 16 17 18 19 21

CodePudding user response:

here is another solution:

def where(n, c):
 where = []
 for i in range(1, n   1):
     dig = i
     while dig > 0:
         if dig % 10 == c:
             where.append(i)
             break
         dig //= 10
 print(str(where)[1:-1])


n = 22
c = 1
where(n, c)
  • Related