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?
PYTHON: Using a counter initialized to 0, count how many even numbers are multiples of 3...
PYTHON: Using a counter initialized to 0, count how many even numbers are multiples of 3 in a range of numbers 1 to 300. If the counter reaches 20, break out of the loop and stop counting.
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)
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.
QUESTION 1 How many sounds are in the name "James"? (Count each occurrence of a given...
QUESTION 1 How many sounds are in the name "James"? (Count each occurrence of a given sound separately, e.g., in "bluebird" [blubɹ̩d], there would be 6 sounds -- you would count [b] twice). 3 4 5 6 0.125 points    QUESTION 2 How many sounds are in the word "axis"? (Count each occurrence of a given sound separately, e.g., in "bluebird" [blubɹ̩d], there would be 6 sounds -- you would count [b] twice). 3 4 5 6 0.125 points   ...
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,...
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?
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)
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,
Write a Python program that reads an integer and prints how many digits the number has,...
Write a Python program that reads an integer and prints how many digits the number has, by checking whether the number is ≥10,≥100,≥1000, and so on (up to 1,000,000). Your program should also identify if a number is negative.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT