@@ -0,0 +1,1046 @@
+%0A%0Adef count_ones(input_num):%0A    '''%0A    Counting # of digit one shown up. NOTE: this is a math problem :(%0A    '''%0A    if input_num %3C= 0:%0A        return 0%0A    if input_num %3C 10:%0A        return 1%0A    res = 0%0A    val = input_num%0A    div = 1%0A    # Begin to calculate ones digit by digit. Starting from LSB%0A    while val:%0A        current_digit = val %25 10%0A        val /= 10%0A        if current_digit == 0:%0A            # Special case - ones are with MSB from 1 - (MSB - 1)%0A            res += (val) * div%0A        elif current_digit == 1:%0A            # Special case - ones are with MSB from 0 - (MSB - 1)%0A            # and modulo of division%0A            res += (val) * div%0A            res += (input_num %25 div) + 1%0A        else:%0A            # Normal case - ones are with MSB from 0 - MSB%0A            res += (val + 1) * div%0A        div *= 10%0A    return res%0A%0A%0Adef test(in_val):%0A    print %22Input: %25d%22 %25 (in_val)%0A    print %22My answer: %25d%22 %25 (count_ones(in_val))%0A%0A%0Atest(-1)%0Atest(0)%0Atest(19)%0Atest(99)%0Atest(109)%0Atest(119)%0Atest(209)%0Atest(319)%0Atest(999)%0Atest(5746)%0A

Create count_ones.py