Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use Python to solve it. Thanks! A common kind of puzzle question is \"how

ID: 3675976 • Letter: P

Question

Please use Python to solve it. Thanks!

A common kind of puzzle question is "how many numbers between 1 and 1000 contain the digit 11". Write a function countNumsfithDigit(upperNumber, digit) that returns the number of numbers from 1 through upperNumber that contain the given digit. For example countNumsfithDigit(10, 9) should return 1, countNumsfithDigit(10, 1) should return 2, and countNumsfithDigit(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 count(unum,digit):

     result = 0;

     for i in range (1,unum):

         result += has(x,digit)? 1 : 0;   

     return result

def has(x,digit):

    while (x != 0)

        if (x%10 == digit)

           return true;

        x   = x /10;

    return false;

def main():

   unum = input('Choose a upper number')

   digit = input('Choose a digit')

   print "Count of numbers from 1 to " + unum + " that have 4 as a a digit is "+ count(unum,digit);

   return 0;