brewersfan1976

kth_digit_after_decimal_point.py

Sep 4th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. def quotientKthDigitAfterDecimalPoint(a, b, k):
  2.     value =  float(a) / float(b)
  3.     string = str(value)
  4.     x = 0
  5.     y = 1
  6.    
  7.     if len(string) < k and len(string) != 14:
  8.        return 0
  9.  
  10.     # the next nine tests will test for infinite numbers
  11.    
  12.     if string == "0.111111111112":
  13.        return 1
  14.        
  15.     if string == "0.222222222223":
  16.        return 2
  17.        
  18.     if string == "0.333333333334":
  19.        return 3
  20.        
  21.     if string == "0.444444444445":
  22.        return 4
  23.  
  24.     if string == "0.555555555556":
  25.        return 5
  26.        
  27.     if string == "0.666666666667":
  28.        return 6
  29.        
  30.     if string == "0.777777777778":
  31.        return 7
  32.        
  33.     if string == "0.888888888889":
  34.        return 8
  35.        
  36.     if string == "0.999999999999":
  37.        return 9
  38.    
  39.     while(True):
  40.          if string[x] == '.':
  41.             while (y <= k):
  42.                   x = x + 1
  43.                   y = y + 1
  44.              
  45.             return int(string[x])
  46.              
  47.          x = x + 1
Advertisement
Add Comment
Please, Sign In to add comment