Question

In: Computer Science

PYTHON PROBLEM Given two numbers a and b, count how many times each of the digits...

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].

Solutions

Expert Solution

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!


Related Solutions

How many base 10 numbers have five digits? How many five digit numbers have no two...
How many base 10 numbers have five digits? How many five digit numbers have no two consecutive digits equal? How many have at least one pair of consecutive digits equal?
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal...
Given n. Write a program in PYTHON to Generate all numbers with number of digits equal to n, such that the digit to the right is greater than the left digit (ai+1 > ai). E.g. if n=3 (123,124,125,……129,234,…..789)
In python, read the file credit_cards.txt into a dictionary with the count of how many cards...
In python, read the file credit_cards.txt into a dictionary with the count of how many cards of each type of card are in the file. credit_cards.txt contains the following data: John Smith, Discover Helen Jones, Visa Jerry Jones, Master Card Julio Jones, Diners Club Fred Jones, Diners Club Anthony Rendon, Platinum Visa Juan Soto, Platinum Visa George Jones, American Express Brandon Allen, Visa Henry Beureguard, Visa Allen Jackson, Master Card Faith Hill, Platinum Visa David Smith, Master Card Samual Jackson,...
1. A) How many three-digit numbers are there for which the sum of the digits is...
1. A) How many three-digit numbers are there for which the sum of the digits is at least 25? B) How many three-digit numbers can be formed if only odd numbers are allowed to be re-used Please combinatorics principles where applicable.
in PYTHON given a specific text file containing a list of numbers on each line (numbers...
in PYTHON given a specific text file containing a list of numbers on each line (numbers on each line are different) write results to a new file after the following tasks are performed: Get rid of each line of numbers that is not 8 characters long Get rid of lines that don't begin with (478, 932, 188, 642, 093)
how many five digit numbers include the digits 4 or 6 or both?
how many five digit numbers include the digits 4 or 6 or both?
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You...
Develop a python program to convert two decimal numbers (A and B) to binary numbers. You should generate B complement signal (flip all the bits of Input B,
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a',...
Python...Count the number of times a element of list1 occurs in in list2 list1 = ['a', 'b', 'c','e','j']; list2 = ['a', 'c', 'd', 'b','e','z']; {'a': 1, 'c': 1, 'b': 1, 'e': 1, 'j': 0} How do I get to this to work without the Collections counter?
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with...
a) How many 3-digit numbers are there? b) How many 3-digit numbers can you make with all three digits different? c) How many of the numbers is part b) are odd?
find how many different numbers can be made by rearranging all 5 digits of the number...
find how many different numbers can be made by rearranging all 5 digits of the number 12345 if a) there is no restrictions. b) the number made is an even number. c) sandra wishes to buy some applications for her smart phone but she only has enough money fot 5 apps in total. there are 3 train apps, 6 social network apps and 14 game apps available. sandra wants to have at least 1 of each type of app. find...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT