I hope that I come back one day to tell you that I really changed: Super Learning Python Part 4

I hope that I come back one day to tell you that I really changed: Super Learning Python Part 4

A green leafy background. A black silhouette is meditating. And light is coming out of the heart centre of the heart chakra that is in there. The light is bright green.

So I would like to dedicate this blog's title to my boss.

"I hope that I come back one day to tell you that I really changed..." my Python.

I am working so so hard to improve it and I am really working to learn and grow. The actual quote is from a Lana del Rey song but it's not one I want to quote explicitly here

Still obsessed with the heart chakra, still obsessed with love and what it means to pour love into everything I do.

And I love Python and I love and am obsessed with this journey of learning and growth.

I am learning Python with all my heart

I wanted to say that I finally understood this solution:

def likes(names):
n = len(names)
return {
0: 'no one likes this',
1: '{} likes this',
2: '{} and {} like this',
3: '{}, {} and {} like this',
4: '{}, {} and {others} others like this'
}[min(4, n)].format(*names[:3], others=n-2)

So this solution is just a dictionary. It's not any fancy new technique - yes it's called dictionary mapping or whatever - but it's not any new solution or whatever - it's just a dictionary.

So this is how it works:
  • The function takes a parameter names which is a list of names
  • It creates one variable, n, which is the length of the list of names
  • It then quite literally just creates the dictionary - WOW - every key-value pair is a solution - every key-value pair is one of the potential solutions to the problem, one of the potential outputs that we want to return - WOW, I have never seen this before - WOW, I have never seen dictionaries used like this before, wow, wow, WOW.
  • By the way this is called a LITERAL. 
    • A LITERAL is when a value is expressed IN-PLACE.
    • So we just use a value - and we don't save it to anything - like .join() is called on a string literal
      • ", ".join(names)
        • btw, many other languages, such as JavaScript, don't use literals in this way
        • This usage is very specific to Python
  • And so then: we literally just use DICTIONARY ACCESSING LOGIC AND NOTATION JUST TO ACCESS THE VALUE AND THE SOLUTION OF THE DICTIONARY THAT WE WANT - WOW. 
    • WOW, this is so so so amazing - WOW, I have never seen this done like this before - WOW.
  • Because, inside of the dictionary accessing notation, we have a function and we use logic to change the response of the output depending on the input. WOW!!!!
  • Basically, so, we want the function to return option 4 if the length of the input is 4 or ANYTHING HIGHER, because the output never changes after option 4 as the response will be the same anyway.
  • So whatever is lower, n or 4, gets taken and used to access the desired response.
  • AND THEN we use .format()
  • And this basically has a combination of arguments and keyword arguments
    • Arguments get accessed in order
    • This order is the list of names, sliced at 3
    • The splat operator (*) basically just means we don't have to write out every name on the list of names individually
    • Others is a keyword argument that the .format() argument will default to when it meets it.
  • And thus we return the formatted sentence exactly as we like it!
I am so massively happy to have learned and understood this solution.

I solved a mega problem in Python

I solved this problem right here on Codewars.

The Hall of the Dead. Or rather: Setting Places for the Dead

It was really really hard! Here is my solution:

def set_table(the_dead: list[str]) -> list[str]:
seat_list: list[str] = ["_____"] * 12

for name in the_dead:
if name[0] in "QUTHCRDMZ":
preferred_seat_index: int = 0
elif name[0] in "WEVOXING":
preferred_seat_index: int = 3
elif name[0] in "JFABKPLY":
preferred_seat_index: int = 6
else:
preferred_seat_index: int = 9

if seat_list[preferred_seat_index] == "_____":
seat_list[preferred_seat_index] = name
else:
for number in range(1,7):
for offset in (-number, number):
if seat_list[(preferred_seat_index + offset) % 12]
                    == "_____":
seat_list[(preferred_seat_index + offset) % 12] = name
break
else: continue
break

return seat_list

I want to write some more on this but maybe this is something for a later post?

I have just been given quite a bit of work to do for my team so while I might do some more python later outside of hours I need to make my work focus my team's work again (as opposed to just Python development, which I do when I am blocked, stuck, and/or waiting for someone (or just need a quick little break). 

Anyway, would like to write more about this later - thanks.

I had a look at some of the other solutions but they are all not very verbose and very annoying - when are people going to realise that verbosity is a good thing in coding! These people might be good coders but in the world of software engineering, try naming your variables ik and j! see how your colleagues will love it (sarcasm!).

Nevertheless, what did I learn doing this? 

A LOT. One thing was to be careful about variables.

For example, in the triple-nested (!!!) for loop, I was adding number instead of offset to preferred_seat_index and as such I was only adding positive values and all of the negative values, which were the preferable ones, as the preference is for the seat to be counter-clockwise if the preferred one is not available, were ignored. Therefore, it is very important to go back over your code and to make sure that it is doing what you wanted it to do correctly.

It is important to check that everything you think it's doing, it does - and that it adds up.

P.S.

I just looked over all of my merged pull requests, and I am so happy! I am SO happy! I am SO, SO, SO HAPPY! I am so happy to be finally contributing to the codebase and to be doing it properly.

Everything I do, I do it for the wind farms.

Everything I do, I do it for the wind farms

To know me is to know that I love the wind farms. I love the pylons, and I love renewable energy.

I really really really love offshore wind farms deeply, and from the bottom of my heart. I love land wind turbines too, and they make my heart sing.

In this lifetime, we've all got to find what we love, and follow it. Following what you love will open your heart to the rest of the world. You've always got to start with what you love! And I love electricity.

I love renewable energy.

I love the way that I feel when I look at a wind turbine.

My heart sings when I look at a wind turbine.

The village that I grew up in is surrounded by wind turbines

I was born into a tiny small village in Luxembourg. 

As you drove in to my village from school and town and so on, and the centre (i.e. Luxembourg city, where most of our activity was based), you had to drive past a substation.

I loved that station so much with all of my heart and it is still there.

I still love it to this day.

That village is surrounded by turbines now

I don't know when it happened but it did (I moved to another town when I was 7 and still go back but I don't live there anymore and so I missed the actual transition).

Everywhere you look in absolutely every direction you can see wind turbines - everywhere.

And even from my favourite viewing spot - which is next to my favourite house ever, ever - or at least one of them - you have the BEST view of all of the wind turbines. Ever, EVER!

They are the same turbines that you can see from Luxembourg airport

So if you want to see them, you can see them from there... xx

Thank you.


 

Comments

Popular posts from this blog

Hello World

Yosemite

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