Advertisement
Guest User

feet and inches

a guest
Jul 2nd, 2011
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.10 KB | None | 0 0
  1. from math import floor
  2. from fractions import Fraction
  3.  
  4. def convert(n):    
  5.     feet, r = int(floor(n) / 12), n % 12
  6.     inches  = int(floor(r))
  7.     numer   = int(round(32 * (r - inches)))
  8.    
  9.     return feet, inches, Fraction(numer, 32)
  10.  
  11. def show_inches(inches, fractions):
  12.  
  13.     def pluralize_inches(i):
  14.         if i == 1:
  15.             return str(i) + " inch"
  16.         else:
  17.             return str(i) + " inches"
  18.    
  19.     if inches and fractions:
  20.         return str(inches) + ' and ' + str(fractions) + ' inches'
  21.     elif inches or fractions:
  22.         return pluralize_inches(inches or fractions)
  23.     else: return ''
  24.  
  25. def feet_and_inches(n):
  26.  
  27.     def pluralize_feet(ft):
  28.         if not ft:
  29.             return ''
  30.         elif ft == 1:
  31.             return str(ft) + ' foot'
  32.         else:
  33.             return str(ft) + ' feet'
  34.    
  35.     if not n:
  36.         return "0 feet 0 inches"
  37.     else:
  38.         feet, inches, fractions = convert(n)
  39.         ft = pluralize_feet(feet)
  40.         i  = show_inches(inches, fractions)
  41.         return ft + (ft and i and ' ') + i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement