Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Python integer partitioning

$
0
0
Hello, can someone please explain me this piece of code line by line?
def gen(n):
    # tuple version
    if n == 0:
        yield ()
        return

    for p in gen(n-1):
        yield (1, ) + p
        if p and (len(p)<2 or p[1] > p[0]):
            yield (p[0] + 1, ) + p[1:]


I'm mostly troubled with the statement "yield", and while I've googled for examples, none of them seem to be helping me understand this code as none of them use it in this way.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>