Hi Reader,
Thanks for sticking with me through all of those Black Friday emails! π
Seriously, though, I'm glad you're still here! π
Now it's time to get back to the tips! π
βVisual Anagramsβ
This is a collection of "multi-view" optical illusions generated by diffusion models (similar to DALL-E and Stable Diffusion). I can't get over how good these are, and I'd encourage you to spend a few minutes checking them out!
There's also a research paper explaining how these were constructed, as well as a Colab notebook you can use to create your own!
On Friday, I was working on a problem from the Python Problem-Solving Bootcamp that I talked about last week.
The problem boils down to this:
You have a text file containing 2000 numbers (one per line). If you walk through them in order, how many times is the current number greater than the previous number?
For example, if the first 8 numbers looked like this...
...the answer would be 5, since the value increases 5 times (and decreases 2 times) when walking through the numbers from left to right.
How would YOU solve this problem with code? Give it some thought, and then keep scrolling...
The first step is to read in the data. I used a context manager (the with block) to ensure that the file would automatically be closed.
Within the block, I used a list comprehension to read in the file line-by-line and convert each number (which was originally a string) into an integer:
We can slice the resulting list to view the first 8 elements, and also confirm the length of the list:
Now we're ready to solve the problem!
Here's the solution that I came up with:
How does this work?
The resulting count is 1553, which turns out to be correct.
I think this is a reasonable and readable solution. However, the solution notebook (provided by the bootcamp) reminded me of three great Python tricks I could have used to write a more elegant solution!
I'll teach you those tricks below... π
This solution uses zip to avoid manually assigning current to previous at the end of each for loop iteration:
If you're not familiar with zip, it aggregates the elements from multiple iterables and returns an iterator of tuples.
In simpler language, zip pairs together elements from different objects so that you can work with them at the same time.
In this case, we're pairing together the nums list (starting with element 0) and the nums list (starting with element 1), and then unpacking each pair of elements into previous and current during each iteration of the for loop.
This solution removes the conditional statement from the for loop:
How does this work?
Finally, we can rewrite the solution even more concisely:
This converts the previous solution into a generator expression, which is like a list comprehension that is lazily evaluated.
Then, we pass the generator object to the sum function, which adds up all of the True values (by converting them to ones).
My Python course includes 9 sets of exercises and a 7-part project. It's great for beginners and intermediate users alike.
Feel free to get in touch if you're wondering if it's right for you!
If you enjoyed this weekβs tip, please forward it to a friend! Takes only a few seconds, and it really helps me reach more people! π
See you next Tuesday!
- Kevin
P.S. I'll give you 20 seconds to guess what this video is about (YouTube)
Did someone awesome forward you this email? Sign up here to receive Data Science tips every week!
Join 25,000+ intelligent readers and receive AI tips every Tuesday!
Hi Reader, Until 8 PM ET tonight, you can get the All-Access Pass for $99: Here's everything you need to know: Access all existing courses for one year ($700+ value) Includes new courses launched during your subscription Includes e-book version of Master Machine Learning (coming soon) Additional discounts available Lock in this price forever 30-day refund policy Get the Pass for $99 Questions? Please let me know! - Kevin
Hi Reader, I wanted to share with you three limited-time resources for improving your Python skills... 1οΈβ£ Algorithm Mastery Bootcamp π₯Ύ Are you looking for an intense, 12-day Python bootcamp? My friend Rodrigo GirΓ£o SerrΓ£o is running a new Algorithm Mastery Bootcamp, and it starts in just 5 days! In the bootcamp, you'll solve 24 real programming challenges and participate in daily live sessions to discuss and compare solutions. It's a great way to strengthen your problem-solving muscles πͺ I...
Hi Reader, Last week, I launched the All-Access Pass, which gives you access to ALL of Data School's courses for one year. Through Black Friday, you can buy the pass for $99, after which the price will increase. Here are the included courses: Build an AI chatbot with Python ($9) Create your first AI app in 60 minutes using LangChain & LangGraph! β‘ Build AI agents with Python ($99) Develop the skills to create AI apps that can think and act independently π€ Conda Essentials for Data Scientists...