Algorithms, Blockchain and Cloud

Teaching Kids Programming – Implement the Pairwise Function in Python using the Iterator/Yield


Teaching Kids Programming: Videos on Data Structures and Algorithms

Implement the Pairwise Function in Python using the Iterator/Yield

The pairwise is from itertools where we can iterate over a iterable (list/array, tuple, set, dictionary, string and etc) in Python, and then returns an iterator to the tuple of the adjacent elements. For example:

from itertools import pairwise
s = "1234567"
for a, b in pairwise(s):
    print(a, b)

This prints the following:

1, 2
2, 3
3, 4
4, 5
5, 6
6, 7

This is a handy syntax sugar to avoid dealing with the index for example, you can do this using the following old-school style:

s = "1234567"
n = len(s)
for i in range(n - 1):
    print(s[i], s[i + 1])

Not all the data structures support the indexing operations, for example, set, dictionary do not support [] operator, however you can convert them to list.

In python, for the iterables, we can convert to iterator via the iter function, and then we can implement the pairwise using the following concise way.

def pairwise(iterable):
    x = iter(iterable)
    a = next(x, None)
    for b in x:
        yield a, b
        a = b

The yield keyword gives an item at a time.

What is yield keyword?

Imagine you have a magical storybook. This book is special because it doesn’t tell you the whole story all at once. Instead, every time you open the book, it gives you one page of the story, just enough to keep you excited and wanting more.

Now, think of the “yield” keyword in a computer program like this magical book. When a program uses “yield,” it’s like the book saying, “Here’s the next part of the story. I’ll give you more when you come back!”

Let’s break it down with a little story about how it works:

The Tale of the Magical Book
Once upon a time, in the land of Codeville, there was a programmer named Pat. Pat wanted to create a story-telling program. But instead of giving the whole story at once, Pat wanted to share it one piece at a time. So, Pat used a magical spell called “yield.”

Here is how Pat’s spell worked:

  • Start the Story: Pat would start telling the story and reach a point where they wanted to pause and let the listener think about what happened so far.
  • Use the Spell “yield”: Pat would say “yield” at the point where the story should pause.
  • Wait for Next Time: The listener could come back later, and Pat would pick up the story right where they left off, without starting from the beginning.

How It Works in a Program

When Pat wrote the program, it looked something like this:

def magical_story():
    yield "Once upon a time, in a faraway land..."
    yield "There was a brave knight named Aria."
    yield "Aria had a special mission to save the kingdom."
  • First Time: When the listener (the program) asks for the story, it gets “Once upon a time, in a faraway land…”
  • Second Time: The next time it asks, it gets “There was a brave knight named Aria.”
  • Third Time: And then, “Aria had a special mission to save the kingdom.”

The “yield” keyword helps Pat’s program tell the story one part at a time, making it exciting and easier to follow.

Why It’s Cool
Using “yield” is super cool because:

  • It Saves Memory: Just like the magical book doesn’t need to show the whole story at once, the program doesn’t need to remember everything all at once. This makes it easier for the computer to handle big tasks.
  • It’s Interactive: It makes the program interactive and able to give you more information as you ask for it.

So, the “yield” keyword is like a magical spell that helps programs share their stories one exciting piece at a time!

–EOF (The Ultimate Computing & Technology Blog) —

799 words
Last Post: Teaching Kids Programming - Greedy Algorithm to Redistribute Items to Boxes (Knapsack Problem)
Next Post: The Major Security Flaws of Wirex (WirexApp) Exchange

The Permanent URL is: Teaching Kids Programming – Implement the Pairwise Function in Python using the Iterator/Yield (AMP Version)

Exit mobile version