I'll Sue You If You Step On My Lawn

I'll Sue You If You Step On My Lawn

This is a wonderful quote from a Taylor Swift song, "Who's Afraid of Little Old Me." I thought that I had gotten past the phase of naming my blog posts after beautiful lines from indie songs (or clearly not-so-indie) but evidently this is not the case.

About eight wind farms stand over a field, presumably somewhere in Luxembourg.

As always I am continuing my theme of posting pictures of the wind farms in Luxembourg on here.

I love them so so so so SO SO MUCH!

Number of People On The Bus

I love life.

I love the Universe, and I love life.

At 11.15 pm, on the last train from London to Worthing, with the whole universe packed onto this train as the Brighton one's not running, I loaded up codewars.

And I literally am not kidding - the problem that came up all by itself as if by magic was "number of people on the bus." Now that's poetry. 

That's magic. That's beautiful. Beautiful. Wow. Lol. 

So what was my solution? Hacky as well - here we are.
def number(bus_stops: list[list[int]]) -> int:
    number_of_people_on_the_bus = 0
    for pair in bus_stops:
        number_of_people_on_the_bus = (number_of_people_on_the_bus + pair[0]) - pair[1]
    return number_of_people_on_the_bus

The Top Solutions:

Because there's got to be something better - and there's got to be something else - someone else has OBVIOUSLY done this better than me in Python.

OH NO NO NOOOOOO

NOT ANOTHER GENERATOR EXPRESSION

MORE GENERATOR EXPRESSIONS

I KEEP ON FORGETTING THEM

I KEEP ON FORGETTING TO USE THEM

BUT WHYYYYYYYY AND THANK YOU AND THANK YOU AND WHY

MY NEMESIS!

Solution Number 1 Feat. Generator Expressions

So what does this one do?

def number(bus_stops):
    return sum([stop[0] - stop[1] for stop in bus_stops])
This one:
  • it returns the sum of something
  • but what does it return the sum of?
It:
  • takes every list of two sets of numbers from the bigger list
  • it loops from all of the sets in the array one by one
  • it subtracts the number of people who get on the bus from the number of people who get off the bus or something
  • it yields each one of those values
  • it then returns the sum of that

Solution Number 2 Also Feat. Generator Expressions

 So this is really interesting because it looks like the above; it uses a generator expression inside of a sum() method. So for every iteration of the loop the iterator yields something.
def number(stops):
return sum(i - o for i, o in stops)

but what on earth is this with the io! I really don't get it. Chat GPT I must call you now! MY MIND IS BLOWN! It seems that Python can infer from the number of values present in the inner list how to do things like this.

It can use this kind of destructuring as in the example after all, it seems.

OH NO ALL OF THE TOP 3 ALL FEATURE GENERATOR EXPRESSIONS AARRGH

Solution Number 3

Okay so solutions number 2 and 3 are basically the same but just with different variable names - I need to get better at spotting that sooner, sorry.
def number(bus_stops):
    return sum(on - off for on, off in bus_stops)
But anyway there is something to be learned from here, so let me go again.
It also destructures something based on the number of variables in the inner list ARRRRGH.
Fixed length. This works because of the fixed length of this inner lists.
It works because of the fixed length of all the inner lists.
Iterable unpacking. 

Comments

Popular posts from this blog

Hello World

Yosemite

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