A common kind of puzzle question is \"how many numbers between 1 and 1000 contai
ID: 3676939 • Letter: A
Question
A common kind of puzzle question is "how many numbers between 1 and 1000 contain the digit 7?". Write a function countNumsWithDigit(upperNumber, digit) that returns the number of numbers from 1 through upperNumber that contain the given digit. For example countNumsWithDigit(10, 9) should return 1, countNumsWithDigit(10,1) should return 2, and countNumsWithDigit(30, 2) should return 12. 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).Explanation / Answer
def countNumsWithDigit(upperNumber,digit): return len([x for x in range(upperNumber+1) if str(digit) in str(x)]
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.