WHOOSH! Bullseye!

Hey there, 👋

How is your Python going?

In this Mathspp Insider 🐍🚀 email I’ll tell you what axe throwing 🪓 has to do with itertools.accumulate.

WHOOSH

The axe hits the target and George yells “Bullseye!” 🎯 

I nod in his direction and I mark 5 points on the iPad next to us.

His score shows 26.

George hands me the axe for my 10th and final throw.
My score is 23.
If I hit the centre of the target for 5 points, I win.

I throw the axe.

WHOOSH

And I miss the mark.
I only got 2 points.
George marks the 2 points on the iPad.
He’s smiling.

After George marks my 2 points the screen shows a message “George wins”.

I was a bit upset and I called him.

“Hey George…”

”Yeah?”

“I won’t let this happen again!”

George raised his eyebrow:
“What do you mean?”

I confessed why I was upset:
“I was winning all game and I let you take the victory from me on the last round.”

“Really?
I was so focused during the game I didn’t even notice.
But all I see here are the points of each throw.”

The iPad showed George’s throws:

0, 0, 0, 0, 1, 5, 5, 5, 5, 5

It also showed mine:

3, 3, 2, 5, 2, 2, 2, 2, 2, 2

George raised his eyebrow again.
(He’s a very expressive guy.)

“I told you I suck at these calculations.
How can you show me that you were winning the whole game?”

I wasn’t prepared for George’s question.

But I know Python.
And I also know the standard library.
The Python standard library is like the Swiss army knife of programming.
It helps you out of any jam.

“Here’s the deal…”
I started explaining while I took my phone from my pocket and opened a basic Python REPL.
“I’ll start by listing all of our scores:”

>>> rodrigo = [3, 3, 2, 5, 2, 2, 2, 2, 2, 2]
>>> george  = [0, 0, 0, 0, 1, 5, 5, 5, 5, 5]

“We can easily see you won by summing all of our points.
Your sum is higher than mine:”

>>> sum(rodrigo)
25
>>> sum(george)
26

I proceeded with my explanation.

“The sum only shows the final result.
I want to compare all of the intermediate results.
I need itertools.accumulate for that”:

>>> from itertools import accumulate
>>> list(accumulate(rodrigo))
[3, 6, 8, 13, 15, 17, 19, 21, 23, 25]
>>> list(accumulate(george))
[0, 0, 0, 0, 1, 6, 11, 16, 21, 26]

“You can see that I was winning the whole game if you look closely.”

George scratched his head and complained “I can’t read and compare the numbers there.”

“You’re right.
Let me make it easier for you to see.“

I thought for a second and had a second idea.

“I’ll ask Python to count in how many rounds I was ahead of you”:

>>> import operator
>>> sum(
...     map(
...         operator.gt,
...         accumulate(rodrigo),
...         accumulate(george),
...     )
... )
...
9

“This means I was ahead of you 9 rounds.
The game lasted 10 rounds.
This means you won at the very last round.”

George looked at me and decided to mock me a bit:
”That just makes victory taste sweeter!”

We both laughed.

Despite the banter George was impressed:

“You have to teach me some of this Python stuff.
And the Swiss army knife thing too.”

“No worries my friend.
I’ll just send you a link to my bootcamp.
We learn a lot about the Swiss army knife there.”

🐍🚀 How was this email?

Login or Subscribe to participate in polls.