I saw in my mind fairy lights through the mist: Python Part 9

I saw in my mind fairy lights through the mist: Python Part 9

If you've been reading for a while you'll know focusing is my biggest aim and challenge.

Once I get started I can't stop. But it is the hardest thing in the world for me to get started


a string of fairy lights at dusk, with a sky in the background and a few little huts with fairy lights


Anyway, at least I have refined my Python strategy:

Shamelessly copy pasted from my LinkedIn cos why write it twice:

1. Solve Codewars problems
2. Write about them in my blog and what I got stuck on about them and what I learned from them
3. Solve the problem as best I can; try and make it more elegant if I can but just get it done; I can usually solve the ones I am working on very quickly
4. Stick to levels where I can solve it without too much pain for now; the focus for now is the Python; the higher levels will come later
5. After I have done the problem solving thingy, look at one or two or so other solutions and learn from them
6. Look up any Python I don't know
7. Write step by step what the code is doing

Python Problem: Happy "g"

Awwww! That's so cute. Well, now we're talking! I liked happy things.

Okay so far I have learned that:

  • True and False have to be capitalised in Python (whoops)
  • the .rfind() method is really cool as in 
if my_string.rfind("g") == -1:
        return false
  • Basically, it checks if something exists in a string
  • It gives you the index of a last occurence
  • If not present in the string, it returns -1

My solution:

Here is my absolutely embarrassing and horrific code.
aarwen
def happy_g(my_string: str) -> bool:
    print(my_string)

    my_list = list(my_string)
    
    for index in range(len(my_list)):
        if my_list[index] == "g":
            counter = 0
            if index > 0 and my_list[index - 1] == "g":
                counter += 1
            if index < len(my_list) - 1 and my_list[index + 1] == "g":
                counter += 1
            
            if counter == 0:
                print("I am returning False")
                return False
    
    print("I am returning True")
    return True

I am so embarrassed. I couldn't even quite crack it myself this time round. I got 99% of the way but had to ask Chat GPT to help me at two points of it - luckily there was an error that even chat GPT didn't spot but I did by looking at the tests.

I need to get better at debugging.

Printing things.

Checking things.

Making sure that the variable that I think is doing something, is actually doing that thing.

Else, how will I ever improve? But I am improving every day so that is good. I am making GREAT progress.

Notes on the solution:

I need to remember to get better at taking "prints" out of the final code.

Here's what the code does step by step:
  • It turns the string into a list
  • It loops through indexes equivalent to the list
  • IF IT LANDS ON A LETTER G
    • IT CHECKS, IF THE G IS NOT FIRST CHARACTER, IF THE VALUE OF THE CHARACTER WITH THE PREVIOUS INDEX IS  "G"
    • IT CHECKS, IF THE G IS NOT THE LAST CHARACTER, IF THE VALUE OF THE CHARACTER WITH THE INDEX PLUS ONE IS  "G"
    • If either of the above two statements are true then it increments the counter by one
    • If the counter is less than one we return False
  • Otherwise, we just return True
  • We don't even check anything else, we just return true

Every plan must change

My plan was to go through some more solutions but this is not a popular problem.

The third top solution is similar to mine.

The first is regex and so I will do this course https://www.codecademy.com/learn/introduction-to-regular-expressions

The second is... REGEX TOO.

THAT'S IT I GIVE UP I'M DOING SOME REGEX

FROM MY NEXT STUDY SESSIONS... WATCH THIS SPACE 🪐🚀

So the next aim is set! I am doing some regex courses

My greatest motivation song

I have two.

It's "So Long, London" by Taylor Swift but it's also "Who's Afraid of Little Old Me?". They are both from her album, The Tortured Poet's Department. "So Long, London" screams of quiet determination and enduring passion and makes me think of becoming a Staff Engineer. "Who's Afraid of Little Old Me?" screams (quite literally on two of the lines which is AMAZING) of strength and determination.

I remember the day I got laid off. After going to the pub after work and not drinking (I still had laptop access for 2 weeks and was writing CVs, LinkedIn recommendations for everyone who asked, and was doing just everything I could do find a job), I went to my friend's house.

She had also just been laid off. We had some tea and I met her cat. I left her house and wrote a hugely successful LinkedIn post - write there, on the steps of her house. 

The city was awash with lights.

I phoned my mum. My mum was suitably not too disappointed. We both knew there'd be other opportunities.

As I walked across Brighton in the dark, I thought to myself: "As many opportunities as I can see lights from the top of this hill." I ended up getting into my first choice company and turning down one of Europe's top fintech companies.

Later that night I had a pizza, some fries (bad idea, never have both), some wine and some limoncello. 

I thanked the pizza people because they gave me half of it for free. 

They ended up sending me loads more vouchers which got me through my 6-8 weeks of 12 hours a day of learning Python, studying and applying.

I am so happy I got laid off. I love my new colleagues. I love my new job. I love my new company. I love Python. 

I love working in water tech.

My ultimate motivational and inspirational song

It's got to be this one:

Everything I do, I do it for the Pylons


Everything I do, I do it for the Pylons

Everything I do, I do it for the wind farms

... and that's that...

Thank you.

Comments

Popular posts from this blog

Hello World

Yosemite

Where To Hide A Star