Python language basics 66: a practical example of a yield generator in Python

Introduction

In the previous post we continued our investigation of yield generators in Python. In particular we looked at how a yield function could remember the state of its internal variables. We saw how code execution jumps in and out of a yield function as it’s being enumerated.

At this point you may think that yield functions are interesting but what can they be used for? You’ll find a lot of examples on the internet. In this post we’ll look at the following scenario:

Say you’d like to divide an integer, e.g. 20, into equal parts of 3 and distribute any remainder equally across the groups. The result of such an operation would be the following 3 integers:

7,7,6

20 can be divided into 3 equal parts of 6 and we have a remainder of 20 – 6 * 3 = 2. 2 is then added as 1 and 1 to the first two groups of 6. The result is a more or less equal distribution of the start integer.

The following function will perform just that:

import math

def distribute_integer(total, divider):
    if divider == 0:
        yield 0
    else:
        rest = total % divider
        result = total / divider

        for i in range(0, divider):
            if rest > 0:
                rest -= 1
                yield math.ceil(result)
            else:
                yield math.floor(result)

groups = distribute_integer(20, 3)
for g in groups:
    print(g)

This will print 7,7,6 as expected.

Read the next part here.

Read all Python-related posts on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Python language basics 66: a practical example of a yield generator in Python

  1. Pingback: Python language basics 67: iterating over a list with list comprehension | Dinesh Ram Kali.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: