Question: FOR PYTHON: A common kind of puzzle question is how many numbers between 1 and 10000 contain the digit 9?. Write a function howManyNumsWithDigit(maxNumber, digit)

FOR PYTHON: A common kind of puzzle question is "how many numbers between 1 and 10000 contain the digit 9?". Write a function howManyNumsWithDigit(maxNumber, digit) that returns the number of numbers from 1 through maxNumber that contain the given digit.

For example howManyNumsWithDigit(10, 5) should return 1, howManyNumsWithDigit(10,1) should return 2, and howManyNumsWithDigit(40, 2) should return 13. Your function must use a list comprehension (but remember, it returns a number not a list!) and have only one line of code (in addition to the def line).

Note: of course, there are ways to solve this problem that don't use list comprehensions. But, for this assignment, you must use a list comprehension. Use a list comprehension to create a list of all the numbers that contain the digit, and then simply return the length of that list.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!