#!/usr/bin/env python
"""Calculate the Kaprekar depth of supplied numbers
  refer http://plus.maths.org/issue38/features/nishiyama/index.html

  Copyright (C) 2007, Michael Davies <michael@the-davies.net>
  Should be fine for anything later than Python 2.1

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License as
  published by the Free Software Foundation; either version 2 of the
  License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  02111-1307, USA.
"""
__authors__ = [ "Michael Davies <michael@the-davies.net" ]
__license__ = "GPL"

import sys

# Python 2.2 crack
try:
   True, False
except NameError:
   True, False = 1, 0

# verbose flag
verbosity = False

def listOp(numStr, ascending):
   """ Return a list of single digit numbers in either ascending or
   descending order from the supplied numStr """
   list = []
   for ch in numStr:
       list += ch
   list.sort()
   if not ascending:
       list.reverse()
   return list

def getBiggestNumber(numStr):
   return int("".join(listOp(numStr, False)))

def getSmallestNumber(numStr):
   return int("".join(listOp(numStr, True)))

def calcKaprekarDepth(arg):
   global verbosity
   last = -1
   diff = 0
   number = arg
   i = 0
   while (diff != last):
       i=i+1
       last = diff
       biggest = getBiggestNumber(number)
       smallest = getSmallestNumber(number)
       diff = biggest - smallest
       if verbosity:
           print "Step %s Biggest=%s Smallest=%s Difference=%s" % \
               (i, biggest, smallest, diff)
       number = str(diff)
   return i

def kaprekarDepth(args):
   global verbosity
   for arg in args:
       argLength = len(arg)
       if arg == "-v": verbosity = True
       elif argLength != 3 and argLength != 4:
           print >>sys.stderr, """
Kaprekar numbers exist for numbers of length 3, 4, 6, 8, 9, 10 - but only
numbers of length 3 and 4 are computationally acceptible :-)"""
           sys.exit(1)
       else:
           result = calcKaprekarDepth(arg)
           if verbosity:
               print "The Kaprekar depth of %s is %s" % \
                   (arg, calcKaprekarDepth(arg))
           else:
               print result

if __name__ == "__main__":
   kaprekarDepth(sys.argv[1:])
# This file has been zigned!  See http://michaeldavies.org/zign-tools
# zign-version: 0.9.4
# zign-hashes: MD5, SHA-1
# -----BEGIN PGP SIGNED MESSAGE-----
# Hash: SHA1
# 
# da84b98e461e2a372a2edd4d3978440fabff33b101bee280a2de6df908276cb3b6dfdbad
# -----BEGIN PGP SIGNATURE-----
# Version: GnuPG v1.4.7 (Darwin)
# 
# iD8DBQFHmzko7kkvCQqp1vwRAlPqAKCI9cDzxuVK4a3G353Q91tLi4P9bACeN5TL
# 0p5h7XA60chfkMnNv6pa7LI=
# =sVfL
# -----END PGP SIGNATURE-----
# zign: Protecting you since 2007 :-)
# 