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.

Here's the thread πŸ‘‡

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

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!


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

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.
Read 13 tweets
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 πŸ‘‡
Read 10 tweets
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 πŸš€?
πŸ“‚ 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 πŸ‘‡
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
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...
Read 20 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

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

Thank you for your support!

Follow Us on Twitter!