Never One To Hold Back: Learning Python Part 5
Never One To Hold Back: Learning Python Part 5
It's 5 pm and I cannot start new intellectual work on the codebase at this time anyway - so I'm just doing some more Python.
I need to be able to think properly to read the tickets, read the links, read the notes, look at the codebase, and really think about what it is that I am doing.
A New Python Problem
Because I just wanted to get some python in, I am going for an easier problem, a level 7 kyu (they very and sometimes I can solve 6 kyu in 5 minutes and sometimes it takes all day and help from my mentor. 7 kyu can also be a 5-10 minute job but I come across harder ones too sometimes).
Okay, so the problem I have decided to go for today is the Credit Card Mask problem, hoping I can solve it quickly, look at some refactors, maybe one or two or so.
I AM SOOOO HAPPPY! I solved this! It definitely took me (way) more than 5 minutes but I got there! I had to put my code into a real IDE to spot some errors and also I had to put one error into chat GPT partly cos I couldn't read it off the Codewars UI but I got it!!! I got there! Here is my code:
def maskify(cc: str) -> str:
if len(cc) < 4:
return cc
else:
last_four_digits: list = list(cc[len(cc)-4:])
all_other_digits: list = list(cc[:len(cc)-4])
for index in range(len(all_other_digits)):
all_other_digits[index] = "#"
new_string = "".join(all_other_digits + last_four_digits)
return new_string
Yayyyy! I'm literally so happy and so so so delighted. However, as ever, my solution is a bit hacky, and a bit n00by, so let's go and have a look at end see what the others/pros have done:
ef maskify(cc):
return "#"*(len(cc)-4) + cc[-4:]
Nice! Here's one that I don't understand, which is always so awesome, so let's go.
OKAY, I DO get it. I actually do get it straight away! They are slicing like I did. They slice at the end too and they return that. And in fact they use -4 instead of len(cc) - 4 because I forget that you can count backwards like that in Python.
But they return it in a much cleverer way! For the first part of the equation, they literally just do this:
- They get the LENGTH of the string minus the last four digits.
- The multiply the "#" character as a string BY THIS LENGTH.
- They concatenate this string of # characters with the last four digits that they have sliced off. Brilliant.
Oh no gross
What is this one please???!
I was doing so well!
def maskify(cc):
return '{message:#>{fill}}'.format(message=cc[-4:], fill=len(cc))
I understand nothing.
But as my mentor has taught me and as I am writing about on my other Software Engineer Princess blog - I need to figure out as much as possible from the INTERFACE! Okay this is sooo gross though.
message is apparently just the last four digits of the string, so fine.
Okay apparently this is some Python string formatting stuff that I have never seen before. Never ever ever. Like, WOW!!!
From Chat GPT:
Everything I do, I do it for the wind farms, please (as always)
Everything I do, I do it for the wind farms.
Everything I do, I do it for the pylons.
Everything I do I do it for the wind farms, and for the love of what I love.
LinkedIn Inspo:
I wrote this on my linkedin and thought it was good. I write a lot of LI posts but they do not get stored. However I am storing it here just in case for one day though:
Here's to living your dream - because only you can do what you really do - and only you can do what you really love - and by finding your gift you will bring it out to the world. Because "you playing small doesn't serve the world." Find what you love, and love what you do, and bring out what you do into the world - because only you can.
Comments
Post a Comment