diff options
Diffstat (limited to 'challenge-238/luca-ferrari/python/ch-2.py')
| -rw-r--r-- | challenge-238/luca-ferrari/python/ch-2.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/challenge-238/luca-ferrari/python/ch-2.py b/challenge-238/luca-ferrari/python/ch-2.py new file mode 100644 index 0000000000..183d67e65d --- /dev/null +++ b/challenge-238/luca-ferrari/python/ch-2.py @@ -0,0 +1,42 @@ +#!python + +import sys +import collections + +def reduce( n ): + if n <= 9: + return n + + multiplication = 1 + for digit in map( int, str( n ) ): + multiplication *= digit + + return multiplication + + +def main( argv ): + steps = {} + for n in map( int, argv ): + current_step = 0 + if n > 9: + # need reduction + current_value = n + while current_value > 9: + current_value = reduce( current_value ) + current_step += 1 + + # if the key is not here, create a list + if not str( current_step ) in steps: + steps[ str( current_step ) ] = [] + + steps[ str(current_step) ].append( n ) + + # now traverse the dictionary and sort the array + # and print it + for k, v in collections.OrderedDict(sorted(steps.items())).items(): + print( ", ".join( map( str, v ) ) ) + + + +if __name__ == '__main__': + main( sys.argv[ 1: ] ) |
