Small change, big difference
Sometimes the small things in life can make the biggest difference. This time it is a small adventure in Python.
Sometimes the small things in life can make the biggest difference. This time it is a small adventure in Python.
I was working on getting some work ported over to Strawberry from Graphene-Django, and I suddenly hit a snag. Once I found out what happened to the Strawberry Fields, I was just glad I could solve it at that point.
I was put in charge to write some extra tests in our framework covering our Docker registry endpoints. We created a framework around Locust.
This idea comes from JavaScript when there were no classes yet as there are now in ES6. You would write something like this:
So this post touches on some Python concepts in async/await territory. I will not cover event loops nor how to interact with them, but something I uncovered/unearthed in a quest to make something work that was synchronous only. I will use the word async which itself is a shortening of the word asynchronous.
So we all know planes are not a good idea. Before you know it you have Samuel L. Jackson shouting in your ear.
So maybe we need a few new ideas.
So for the smart people out there they might have already figured out that the result for 153 is the same as for 135,315,351,513 and 531. This means that we can calculate the result for all of those once and just check if the result of that calculation is in that list. Which is the case for the number 153.
The next mini optimization we can do is to bail out earlier because when summing the number and we overshoot the original then we can stop immediately. For example 9 ** 6 is a big number and so with bigger numbers it makes sense to bail out earlier.
The next thing we can optimize is the fact that the result of the powers calculation does not change during the same length of the numbers. In order words, 3 ** 3 does not change for the numbers 123, 345, 543 and any other number containing a 3 in the range of 100 – 999.
A small mini optimization is to move the call to len out as the length of the number does not change.