In: Computer Science
PYTHON PROBLEM
Given two numbers a and b, count how many times each of the digits 0 to 9 occur in all numbers between a and b, inclusive.
In order to make your code simpler, implement first the function intToList(n) that takes as input one integer n, and returns a list with the digits of n in the order they appear. For example, intToList(1964) should return [1,9,6,4].
Using the function intToList, implement the function digitsCount(a, b) that returns a list with 10 numbers, where position i holds the number of times digit i occurred in all numbers between a and b, inclusive. For example, digitsCount(1, 9) should return the list [0,1,1,1,1,1,1,1,1,1].
CODE:
def intToList(n):
l=[]
while(n>0):
k=n%10
l.append(k)#in this while loop we will be adding each digit of a
number
n=n//10
return l#returning the digits array
def digitsCount(a,b):
count=[0,0,0,0,0,0,0,0,0,0]
for i in range(a,b+1):
l=intToList(i)#calling the intToList function to count the number
of times a digit is repeated in range a to b
for j in l:
count[j]+=1#incrementing the element in resultant array when each
digit is found
return count #returning resultant list
a=int(input())#asking the user to enter a
b=int(input())#asking the user to enter b
result = digitsCount(a,b)#calling digitsCount function with a and b
as parameters
print(result)#printing the resultant array
NOTE: IF YOU HAVE ANY DOUBTS IN INDENTATION I'M KEEPING A PICTURE OF THE ABOVE CODE WHICH WAS EXECUTED BY ME WITH OUTPUT
IF YOU STILL HAVE ANY DOUBTS, PLEASE DONT HESITATE TO ASK IN THE COMMENT SECTION, THANK YOU!