25 Nov, 21 tweets, 7 min read
I enjoy thinking about multiple possible solutions to the same Python π problems.

I do!

Call me crazy π€ͺ, I don't care!

In the past, I've tweeted simple challenges π, you tweeted solutions, and I wrote a thread analysing your solutions.

Here is a meta π§΅ of past challenges!
The first challenge I ever posted was to implement the Caesar cipher

This is the thread with the solutions π

The moral of the story was that very little people know about the `str.maketrans` and `str.translate` methods.

Then I wrote about implementing something like a `.find` method on lists.

Something that looks for the index of an element, but doesn't throw an error when the element is not there.

The next one I published had a more math-y vibe to it.

Or at least, it was implicit there.

I wanted you to compute the sum of the first positive integers:

The best solution made use of a mathematical insight, allowing us to write a closed formula!

Way better than the βobviousβ O(n) solution summing along a range, or similar π

Next up is the last.

No, no, not the last challenge I published!

A challenge to implement a βlastβ function.

(I'm so funny :P)

That was a great one because lots of people chipped in with amazing ideas!

You can read those ideas here π

After that one, I posted a challenge about checking if a string is a pangram.

I had been doing some research on Python sets and wanted to see if you'd put them to good use!

Of course you didn't disappoint, and many of you presented lovely alternatives making use of `set`:

After a challenge that dealt with strings, I wrote a challenge about numbers.

I asked you to implement the `sign` function.

It's such a simple function...

And yet, this was the challenge that generated more fuss, with people coming up with all sorts of crazy ideas!

I think I did a pretty good job of exploring them in the solution thread π

I stayed on the maths side of things, and came up with the idea to ask you to implement the `sum` function, that is a Python built-in:

The solution thread I wrote took some effort, but ended up looking nice π

Then, I actually enjoyed asking you to implement a built-in, so I did it again!

This time, I asked you to implement `enumerate`, to see how you'd handle the fact that you needed to return an iterator:

... and *once again*, you didn't disappoint!

Many awesome solutions were sent to me, so I had a lot of material to work with for the thread I wrote comparing all the alternative approaches π

I can't help it, I have a mathematical background, and thus I asked you for a maths-inclined function again, which also played with iterables:

The `argmax` function π

The solution thread was a fair amount of work to put together, but we covered all sorts of interesting concepts...

Like the fact that `max` takes an optional `key` argument, or that indexing has an equivalent in the `operator` module:

Lastly, I published another string-based challenge, where I wanted you to swap the casing of the input string!

I am yet to write the solution thread for this challenge, but I have **not** forgotten π

These are the problems/solutions I've written on Twitter.

If you like this content of mine, then you'll love to know that I'll be launching a Problem-Solving Bootcamp tomorrow!

Part of it will have a similar feel to these threads!

I'll explain briefly:
You'll be solving some practice problems, and then I'll send you an in-depth analysis of multiple solutions for you to study!

It's going to be great!

You can find out more about it here:

β’ β’ β’

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

This Thread may be Removed Anytime!

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

# More from @mathsppblog

25 Nov
Want to boost your Python π problem-solving skills?

I am looking for highly-motivated individuals that want to stay ahead of the curve!

If that's you, keep on reading π...

Because tomorrow I'm launching the Python Problem-Solving Bootcamp!
This is a bootcamp focused on writing code.

Throughout the bootcamp you will solve 50 programming puzzles over the course of 25 days.

By going through the puzzles, you'll be practising your Python skills, and writing code is the best way to improve!

But there's more to it.
You will be getting 2 challenges/day.

Does that sound scary?

Good! If it does, then it's because it is an excellent growing opportunity.
21 Nov
I have been tweeting about Python π string formatting.

I have been preaching π that f-strings are the best string formatting alternative.

Then comes the string method `.format`.

And only then, %-formatting.

Here is a thread π§΅ comparing the 3 π
In its most basic form,

π %-formatting uses % and a letter inside the string
π `.format` replaces sequences of {} with the data
π f-strings use {} to insert the data _inside_ the string

Here is how it looks like π
An undervalued feature of string formatting is that you can easily determine whether your data should be formatted with its string (str) or with its representation (repr).

For debugging, `repr` is usually more useful.

Here is how this looks like π
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`.

π 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 π
π Getting the parent

The `parent` attribute returns the logical parent of the path you have at hands π
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 π

ππ§΅
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!

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

Your appetizer is this amazing video by @3blue1brown π

5 Nov
I gave you a Python π challenge π...

You delivered πͺ!

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...
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.