Cartoon attribution
A little after writing the program (perm_facts.py) in my recent post:
Permutation facts
(which showed that the number of permutations of n things was the same as n factorial), I saw that I could replace the program's for loops with list comprehensions. So I did that, in the functions num_perms_upto and factorials_upto.
Then I also changed the recursive factorial function to the iterative version shown in the code below. (Although people sometimes use recursive algorithms instead of iterative ones, because they can be simpler to implement, often by using elegant divide and conquer methods), in this case, the recursive version is not really simpler than the iterative one.)
However, I then realized that I could replace the iterative version by a one-line functional version using reduce and operator.mul. So here is the program, perm_fact2.py, with all the changes mentioned above (the iterative version is left in, as comments, but the for loops from the original perm_fact.py are gone, replaced by shorter list comprehensions).
A small benefit of the changes is that the number of lines comes down by a few (if you don't count the commented lines for the iterative version, which I left in there, since it was not in the original), but that was not the goal of the exercise - I was just exploring different ways of doing it.
And whether the code is clearer or not after the changes, may be a matter of personal opinion. For those used to the procedural / imperative style, the changes may make it a bit harder to read, but I think the functional part (reduce with operator.mul, here) and list comprehensions seem to have a certain elegance, and they take slightly less LOC.
Excessive use of reduce, map, etc., just for the sake of it, isn't good, though. And up to 3 or so nested list comprehensions is easy to understand, IME. The thing to remember is that the nested for's (in a nested list comp) run in the same order (leftmost or outermost first) as in a nested for loop. (This is mentioned in the Python docs, and I think they did it that way to be intuitive.)
I ran the program the same way as the previous one, and it gave the same output (verified by DOS's "fc /l outfile1 outfile2", so I will not show the output again here.
#-------------------------------------------------------------- # perm_fact2.py # Modified version of perm_fact.py, with: # - two for loops replaced by list comprehensions; # - recursive factorial() replaced by iterative function; # - iterative factorial() in turn replaced by # functional version, using reduce(). # Author: Vasudev Ram # Copyright 2016 Vasudev Ram # Web site: https://vasudevram.github.io # Blog: http://jugad2.blotspot.com # Product store: https://gumroad.com/vasudevram #-------------------------------------------------------------- import sys from itertools import permutations from operator import mul def num_perms_upto(n): return [ len(list(permutations(range(i)))) for i in range(1, n + 1) ] def factorial(n): if n < 0: print "Argument n should be >= 0" sys.exit(0) #------------------------------------------------------- #Iterative computation of factorial. #f = 1 #for i in range(1, n + 1): # f *= i #return f #------------------------------------------------------- # Functional computation of factorial, using reduce(). return reduce(mul, range(1, n + 1), 1) def factorials_upto(n): return [ factorial(i) for i in range(1, n + 1) ] print "Number of permutations of 1 .. n, where n in 1 .. 10:" print num_perms_upto(10) print "Factorial of n, where n in 1 .. 10:" print factorials_upto(10)I also timed the runs of both perm_fact.py and perm_fact2.py (and perm_fact2.py separately for both the iterative and functional versions of factorial), a few times each, but did not notice any significant difference. Sometimes one was a bit faster, sometimes the other was, sometimes nearly the same.
(One reason why I thought of timing it in the first place, was because I had read somewhere recently that using map on a sequence can be faster than processing its items in a for loop; haven't tested that, so don't know whether is right or not, but it could be - IIRC, the reason given was that the iteration then happens inside the C implementation of map, and if true, that probably holds for reduce too.)
Also, due to the small amount of code in each, and only a few iterations happening in the code, versus the size of the Python interpreter, it is likely that a good (relative) chunk of the total run time was taken by the loading and initialization of the interpreter, and maybe by parsing the .py files. Other random OS processes running on my machine could also account for the variations. Anyway, the goal here was not really to see if the changes made it faster.
- Vasudev Ram - Online Python training and consulting FlyWheel - Managed WordPress Hosting Get updates on my software products / ebooks / courses. Jump to posts: Python DLang xtopdf Subscribe to my blog by email My ActiveState recipes
No comments:
Post a Comment