Everything I Do, I Do It For The Pylons: Python Part 7

Everything I Do, I Do It For The Pylons: Python Part 7

Everything I do, I do it for the pylons

Everything I do, I do it for the pylons. 

I am sorry. 

I am sorry if this disappoints you or lets you down. 

I am sorry if this breaks the mystique.

There are no surprises with me

I just love pylons.

I just love wind farms.

I just love renewable energy and electricity.

But I also have a second love, and that is even greater

And that is for software engineering.

Just for pure software engineering and coding.

My only surprise is this

The only thing I will surprise you with is my ability to turn a difficult situation around. The only thing I will surprise you with is my ability to grow very, very quickly. 

The only thing I will surprise you with is how fast I am able to learn.

This is the journey I have been preparing for all my life.

This is the journey I have been preparing to go on all my life.

I am here, now.

The journey of a thousand miles begins at your feet.

The journey of a thousand miles begins at your feet

You've probably heard "the journey of a thousand miles begins with a single step", but I prefer the above translation.

As a fan of the Tao Te Ching for life, I have a few preferred translations.

But I really, really like the above.

A beautiful photo of a pylon by the canal in Leeds.
Another photo of the canal in Leeds, with the pylon, from my FAVOURITE FRIEND EVER IN THE WORLD 💙💚💛💜🩵💖 THE BIGGEST FRIEND AND THE BEST SUPPORTER EVER IN THE WORLD I LOVER HER SOOOO MUCH 💖
Can you guess who it is? 😍😍 It's my little sister! 😍

My journey begins now

My journey of being more efficient with my tickets, and working more efficiently, and working faster, begins now.

And it begins with building up some python.

And it begins with my mentor's strategy

My mentor's strategy is to solve level 7 kyu problems on Codewars until I can solve them elegantly.

So let's go! And I have a new HTML technique for posting code so let's see how it goes haha.

There are a lot of level 7 codewars problems to solve. 

So let's get cracking.

I've decided to go for a "really easy" problem, or basically return the length of the shortest word in a string.

Shortest Word

Oh God, I've done this before in JavaScript. You need a way to store the minimum and for every iteration of the loop you displace the lowest value.

Of course, there will be an elegant way to do this in python as well, but I don't know it. I can't remember the logic from my last times.

Okay, so here is my solution:
def find_short(my_string: str) -> int:
    words: list = my_string.split()
    current_min_length:int  = 1000
    
    for word in words:
        current_length: int = len(word)
        current_min_length: int = min(current_length, current_min_length)
    
    return current_min_length

I struggled with this one a lot because Python assigns variables very differently to JavaScript. I actually got the idea for the crazy high initialisation of current_min_length from Chat GPT; I wrote most of the code myself but when it wasn't working I ran it through it and it turns out you can't start your variables with 0 or 1 that you then run through min().

I did look on Stack Overflow and there was a neat solution but I didn't understand it and I want my initial solution to be something that I can understand - the refactoring can come later!!!

Solution 1

Have a look at the simplest solution:

def find_short(s):
    return min(len(x) for x in s.split())

I can't ahahaa. I am losing the will. How do I make sure I never miss something like this again??!

GENERATOR EXPRESSIONS - MY NEMESIS RETURNS!!!

Okay so not so easy after all. A generator expression within the min() function. 

And what does a generator expression do? - It YIELDS SOMETHING.

SO FOR EVERY ITERATION OF THE LOOP - IT WILL YIELD SOMETHING.

IN THIS CASE, THE LENGTH OF THE STRING.

AND THEN FROM ALL OF THOSE YIELD VALUES - IT WILL TAKE THE min() AND THEREFORE THE SHORTEST LENGTH.

And then we just return that number. So, in bullet points: solution 1, here we go.

Solution 1 in bullet points:

  • We pass the string into find_short()
  • We pass a generator expression into the min() function
  • We loop through a list in the generator expression
  • We create a list by calling string.split() on s
  • For every item on the list of strings (called "x"), we yield a length of this item!!!
  • Once we have a LIST of those yields (or a TUPLE - I don't know what it does under the hood and I DON'T NEED TO  - this is what my mentors say anyway).
    • We can use the min() function to find the lowest value
    • And this is what we return !!!

Solution 2


def find_short(s):
    return len(min(s.split(' '), key=len))
SO WHAT IS GOING ON HERE???! 

Alright, we:
  • Wrap all of our logic in a len() function so that will return the length of the final value
  • Wrap all of our logic inside a second min() function
  • Split the string into a list
  • min() knows to find the smallest item on the list
  • however, min also knows to find the smallest item on the list by the length of the item due to the optional key=len parameter 
    • If it were a list of numbers it wouldn't need that
    • But as this is a list of strings we might need this value
  • We then return the len() of that min() item.

Solution 3

def find_short(s):
    s = s.split() # splits the string into a list of individual words
    l = min(s, key = len) # finds the shortest string in the list
    return len(l) # returns shortest word length

The comments on the code are pretty self-explanatory.

  • We split the string into a list
  • We return the shortest item on the list, just as with the above;
  • So as with the above, we return the shortest item on the list
  • We use the key=len optional min() argument again
  • We return the length len() of that shortest item

"TAKE YOUR BROKEN WINGS, AND FLY".



"When your heart is broken down, down, down
And your head don't reach the sky
Take your broken wings and fly

When your head is hanging low, low, low
And the tears, they keep falling
Take your broken feet and run

With the world up on your shoulders
Nowhere left to hide
Keep your head up, carry on"

Comments

Popular posts from this blog

Hello World

Yosemite

Two Lines of Thought, Makes Me Wonder What I'm Missing