I gave you a Python 🐍 challenge πŸ†...

You delivered πŸ’ͺ!

This thread 🧡 will review some of your `argmax` implementations.

I'll also tell you what's good βœ… and bad ❌ about them.

I'll also tell you which one I think is the best, most Pythonic ✨ one.

Let's go πŸš€
The `argmax` function returns the index of the maximum element of the argument.

So, why don't we write this solution?

Compute the maximum, and then use `.index` to get its index! πŸ‘‡

But there are two issues here...

Can you see them? πŸ‘€
πŸ‘‰ first issue is that it traverses the argument twice;
(once to get the max, one more time to find its index)

πŸ‘‰ second issue is that it assumes the argument has a method `.index`, which might not be the case.

However, let's try and fix the first problem first...
If we only want to go over the argument once, then we can use a `for` loop to keep track of the maximum and its index!

Here's how you could write it πŸ‘‡

This goes over the argument only once βœ…

But if you've been paying attention to my tweets, you can improve it right away:
Instead of using `range(len(...))` which is almost always an anti-pattern ❌...

We can use `enumerate` πŸ‘‡

This cleans up the code a little bit βœ…

And it has another interesting effect...
By using `enumerate`, we no longer need to index into the argument to fetch the elements βœ…

Why is this good?

Because it means we can use MORE types of iterables as input.

But for this to be true, we need to get rid of the initial indexing in the first line πŸ‘‡
By using `float("-inf")`, we initialise the `mx` variable to something that will always be smaller than whatever is in the argument.

This means that the first iteration of the loop will *always* update the `mx` and `idx` variables.
Now, there's another fundamental difference between this solution and the previous ones.

The previous ones threw an error when the argument was empty.

This one returns -1 πŸ‘‡

Empty sequences do NOT have an argmax, so we want our function to raise an error when that happens.
How can we do this..?

Remember when we initialised `mx` with `lst[0]`?

We can do a β€œsimilar” thing, but that works on all types of iterables!

We just need to use `next` πŸ‘‡
By using `next` on the iterator over the argument, we get a `StopIteration` error if the argument is empty.

Now, callers of the `argmax` function can know if it failed or not πŸ‘‡
We are at a pretty good point, by now.

We have a function that works well, for a variety of input types.

Can we make it better..?

I say we can!

Let's talk about dictionary ordering 😁

Not `dict`.

Dictionary, as in, the book with the words!
Tuples are ordered like dictionaries; did you know that?

If you have a pair `(fst, snd)`, then the tuple is ordered by the value in `fst`.

If it matches the first element of the other pair, then the second element is used as a tie-breaker πŸ‘‡
Now, this is a technique I have seen often:

Let's put both things together:

πŸ‘‰ we take the values that define the ordering (the values of the argument);

πŸ‘‰ we take the values that we want to return (the indices);

and we zip them together.

Then, we call `max` 😊 πŸ‘‡
Again, the problem here is that we are using `range(len(...))`, which is not ideal ❌

Why?

Because not all iterables have a length.

Sadly, `enumerate` builds the tuples in the wrong order (first indices, then elements)...

But we could flip them! πŸ‘‡
But now...

We have the *initial problem* again ❌

We are going over the argument twice!

Once to build the enumeration with the correct ordering.

And another time to find the maximum!

Thankfully, we can fix this 😊

How?
Easy!

The `max` function accepts an optional argument called `key`.

We can use the `key` argument to change what/how the elements are compared within max!

For example, if `key` is the `neg` operator (the operator that negates numbers) we can use `max` to compute the `min` πŸ‘‡
What we have to do is the following:

Use the regular enumeration we get.

But use `key` to tell `max` to look at the values (the second element of the tuple) instead of the indices!

πŸ‘‡
We can do this with a little `lambda` πŸ‘‡

Beautiful πŸ”₯

But...

Let's make it just a little bit more pretty!

The `operator` module has an operator that renders our lambda useless!

Also, if we are not using `mx`, no need to assign to it, we can just use a sink variable.
In the end, this is what we get πŸ‘‡

What a beautiful solution:

βœ… errors on empty sequences
βœ… traverses argument only once
βœ… makes good use of built-ins
βœ… leverages the `key` argument from `max`
βœ… accepts a wide range of argument types

Do you like this solution?

I do 😁
Thank you to everyone who participated!

For reference, here is the original challenge, where you will find people who proposed really great solutions πŸ‘‡

Do you enjoy these challenges?

Because I do πŸ˜‚

Follow @mathsppblog for more content like this!

β€’ β€’ β€’

Missing some Tweet in this thread? You can try to force a refresh
γ€€

Keep Current with Rodrigo πŸπŸ“

Rodrigo πŸπŸ“ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @mathsppblog

6 Nov
The Python 🐍 Standard Library is one of the reasons I love πŸ’™ Python.

πŸ“‚πŸ” dealing with your filesystem is super simple.

All you have to do is use the `pathlib` module.

This short thread is a mini `pathlib` cookbook 🍳, showing some example usages of `pathlib`.

Ready πŸš€? Image
πŸ“‚ Creating a `Path` object

`Path` objects are the bread and butter of `pathlib`.

Here, I just create a path with no arguments, which defaults to the path `.`

Notice how I used `Path` but I get a `WindowsPath` back.

`pathlib` automatically detects your OS πŸ˜‰ Image
πŸ“‚ Getting the parent

The `parent` attribute returns the logical parent of the path you have at hands πŸ‘‡ Image
Read 11 tweets
5 Nov
πŸ—“οΈ the weekend is coming πŸ₯³

If you want, use it to rest! You deserve it!

Or, make this a memorable weekend:

Let this be the weekend you implement your first neural network 🧠 from scratch, in Python 🐍

Here's the roadmap πŸ›£οΈ

πŸ‘‡πŸ§΅
By the time you are done, you'll have

πŸ‘‰ a minimal neural network framework
πŸ‘‰ solved a ML problem with accuracy > 90%

And all this with a surprisingly short amount of code!
First, we start with an appetizer.

It will spark your interest, and acquaint you with some terminology.

Your appetizer is this amazing video by @3blue1brown πŸ‘‡

Read 9 tweets
5 Nov
Python 🐍 objects have one important characteristic about them:

Some objects are mutable, which means their contents can be changed.

This ⚑🧡 will explain what (im)mutability means in Python 🐍.

Let's go πŸš€
The (im)mutability of an object depends on its type.

In other words, (im)mutability is a characteristic of types, not of specific objects!

For example,

πŸ‘‰ ALL integers are immutable.
πŸ‘‰ ALL lists are mutable.

With me so far?
But what exactly does it mean for an object to be mutable?

Or for an object to be immutable?

Recall that an object is characterised by

πŸ‘‰ its identity
πŸ‘‰ its type
πŸ‘‰ its contents.

I wrote about that in this thread πŸ‘‡

Read 13 tweets
1 Nov
Here is a Python 🐍 function that tells you if the input number is even or odd...

But how does it work? πŸ‘‡

It looks a bit weird, doesn't it?

Someone DM'd me this function from a @realpython article, and now this ⚑🧡 will break it down for you.

Here we go πŸš€
If you have heard of Boolean short-circuiting, that's the short answer:

Boolean short-circuiting is what's responsible for the behaviour of `f`.

Let me explain...

First, we need to understand the precedence of the `and` and `or`.

Let me add parentheses πŸ‘‡
`and` has higher precedence than `or`.

In other words, `and` β€œbinds tighter” to its operands than `or`.

Now, usually we use `and` and `or` with Boolean operands, to return a Boolean value.

However, `and`/`or` work with arbitrary objects as operands.

We call it,
Read 14 tweets
31 Oct
Would you like me to teach you how to write Python 🐍 code that is more elegant, more effective, and more expressive than what you have been writing until now... for free?

I am looking to teach Python to people who want to master Python to the best of their abilities.
Do you? If you do, then I know I can help you improve your skills.

How do I know that?

My book has over 10,000 readers, and all of them have one thing in common: they are humans.
Python has dozens of built-ins, and all of them have multiple usage patterns. No human knows all of them by heart, but my research bundled them up nicely in a book for you to reference and learn from.
Read 12 tweets
30 Oct
πŸπŸ”¦ Python under the hood.

πŸ‘‡πŸ§΅ Here's a thread on the dunder method `__init__`.

You can't say you **know** Python if you don't know this, so...

In this thread we will cover the dunder method `__init__` and its role in Python OOP.

Here we go πŸš€
β€œinit” is short for β€œinitialise” or β€œinitialisation”.

That, right there, tells you what `__init__` is for!

When you create a new instance of a class, you use the method `__init__` to initialise it:

In other words, you use `__init__` to customise the instance.
Here's an example for you.

Say you want to create a class that represents people.

People have names, right? And whenever you create a new person, you want to give them their name.

It will be the __init__'s job, then, to take that name and associate it with the person object.
Read 15 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!

:(