My Dev's Life Story #1 (Part 1) | My introduction to code

tomatowizard

Antoine

Posted on October 31, 2021

My Dev's Life Story #1 (Part 1) | My introduction to code

Hi bonjour 👋

Last week I was introducing myself briefly and explaining the concept of my idea for this blog. Today is the first episode! (I'm so excited!). In this episode I'll tell you about my first lines of code and my long research for an apprenticeship. This episode will go over six months and will be divided into three chapters:

  • #1.1 | My introduction to code
  • #1.2 | The choice
  • #1.3 | Looking for a company

Every post will be released on sundays from now on. I hope y'all have a fun time reading 😉

My introduction to code

As you may have read in my Little introduction, code and devs were always around me even before I was born. My parents work in this field and as far as I remember my dad wanted everybody to run their PC on a linux dist (the one he was using back then was ArchLinux). But my first lines of code were written when I was 22 years old (at the time I'm writing this I'm 23 and going toward my 24th birthday). I remember my younger sister coming back from highschool with a coding exercise her teacher gave her to do on python. I was kinda jealous, I never had classes that introduced me to code. Though I remember coding on my TI 84 Plus in 2013, I never took that hobby seriously until I started stealing my sister's exercises sheets and tried solving every problem on python.

# Example of problems that were given on my sister's papers.

height = float(input("Enter the height of the rectange: "))
width = float(input("Enter the width of the rectangle: "))
surface = height*width

if surface == 100:
    print("The surface is equal to 100m²")

if surface > 100:
    print("The surface is bigger than 100m²")

if surface < 100:
    print("The surface is smaller than 100m²")
Enter fullscreen mode Exit fullscreen mode

These exercises piqued my interest and I went to look deeper into them. My step-dad then challenged me on the well-known Fizz-Buzz problem, telling me he used to give it during interviews just to test briefly the logic of the candidates.

Quick reminder of the Fizz-Buzz challenge and the solution I wrote:

Print integers 1 to N, but print “Fizz” if an integer is divisible by 3, “Buzz” if an integer is divisible by 5, and “FizzBuzz” if an integer is divisible by both 3 and 5.

for fizzbuzz in range(201):
    if not fizzbuzz % 3 and not fizzbuzz % 5 :
        print("fizzbuzz", end=', ')
    elif not fizzbuzz % 3 :
        print("fizz", end=', ')
    elif not fizzbuzz % 5 :
        print("buzz", end=', ')
    else :
        print(fizzbuzz, end=', ')
Enter fullscreen mode Exit fullscreen mode

Quick note: Nowadays we could use the new addition of case in python right ?

This leaded me to search for more and learn more while I was still studying philosophy and struggling knowing what I would do after graduating. But I needed a more challenging problem.

The first project

Something that got confirmed the more I started getting involved into coding was that building something that had a use also for other people was a really big motivation for me. Back then I was really involved in a card game community (mix of Yu-Gi-Oh! and Magic The Gathering). We had a community discord where players would come and join groups to enjoy a moment together and play games. That's when I wanted to try and build a bot with useful command related to card game. I didn't know yet how to host nor use a database so I wanted to do something simpler with still some interactions for the people using it. Giving that I already experienced python, I decided to use this language to build this project with the library discord.py.

In paralell of learning how to use this library and build the bot, I also was coding in advance the functions that I wanted to integrate to the project. Like a probability calculator. Here you might say 'That's a good idea, seems not that hard to do!', and you would be right if only I wasn't a literature and art student 🎥

Welcome to the world of STA-TIS-TICS! Before I start on this part, I don't want to be too long on this as I might say some wrong things, so it's more about the progression of my mindset and the approach I take toward the final function and the rendered output. The reason why I'm explaining this in detail is because it was the main function/attraction of the bot I was building, the rest was more gadget-like.

Firstly, let's eliminate the bleeding, here is the first version of my calculator:

def prob_draw():
    deck_size = int(input("Number of cards in the deck : "))
    n_draw = int(input("Number of card drawn (sample) : "))
    n_copy = int(input("Number of the target card (population) : "))
    prob_result = 1

    print(f'Deck size : {deck_size} cards')
    print(f'Number of card drawn : {n_draw}')
    print(f'{n_copy} target(s) in the deck')

    for draw in range(n_draw):
        print(f"{n_copy}/{deck_size}*{n_draw}*{prob_result}")
        prob_result = n_copy/deck_size*prob_result
        print(f"For {draw+1} copy(ies) in starting hand : {round(prob_result*n_draw*100, 2)}%")

        n_copy = n_copy-1
        deck_size = deck_size-1
        n_draw = n_draw-1
        if n_copy == 0: break
        if deck_size == 0: break

prob_draw()
Enter fullscreen mode Exit fullscreen mode

Well... it looks okay right? Sure Antoine, sure.

The problem of this solution is that already the first result is not completely accurate. AND! Bigger the population is, bigger the sample is and farther the result will be from accurate probability. So now I'm facing a problem, I don't know accurately the probablity of drawing my Sol ring in my commander deck (which is 7.07% chance in a 7 cards starting hand). I started to read about probability in card games (shoutout to all the crazy good guys that play poker and have to do this in their head all the time). So I discovered the existence of hypergeometric probability (I'm not really exactly sure it's really called that way but that site/or this one were using this term). Even if I could use these sites to do the work for me, where is the fun in not doing something I don't know how to do ?

Thanks to the scipy.stats library, I saved myself the trouble of writing to much code I wouldn't be able to go over again later. So now, I have the library to write my solution and integrate my function finally accurate and reliable for bigger numbers.

Here is the final function I wrote for this homemade card probability calculator (note that at this moment I'm still in my first month of learning how to code):

from scipy.stats import hypergeom

def test_prob():
    deck_size = int(input("Number of cards in the deck : "))
    n_draw = int(input("Number of card drawn (sample) : "))
    n_copy = int(input("Number of the target card (population) : "))

    x = min_success = 1
    M = deck_size
    n = n_copy
    N = n_draw

    for min_success in range(n_draw):
        pval = hypergeom.sf(x-1, M, n, N)

        print(f'For {min_success+1} copy(ies) in starting hand : {round(pval*100, 2)}%')

        x = x+1
        n_copy = n_copy-1
        deck_size = deck_size-1
        n_draw = n_draw-1
        if n_copy == 0: break
        if n_draw == 0: break
        if deck_size == 0: break

test_prob()
Enter fullscreen mode Exit fullscreen mode

Now that my main function for the bot was ready, I was going to start building it and testing it before finally showing it to my friends to integrate it in their server.

This project taught me a lot of things, production environment, secret variables (damn, I once uploaded an API key directly on my public GitHub repo...) and also creating local imports of my own functions (like a regex text cleaner).

To spare you the long reading of how to build a discord bot I'm linking you a public repo I made and where I uploaded the source code of this project if you're really curious about it, cause I mainly want to focus on what this experience taught me and how it will bring me to the following chapters.

What I take away

It's actually quite fun to look back at these lines of code as I'm writing this post. It seems like an eternity, while it's only been barely more than six months. It was my first encounter with the developer's field and also my first big application. I take away a lot of things from it, positive and self criticism (not in a negative meaning).

As a positive take away, I'm happy I started this journey on python cause I think it's one of the easiest language to get into (the learning curve gets harder though). I also loved to have to search for various subject not directly related to code and have to translate them into code in order for my functions to work. I, as a very curious person about a lot of subjects, had a lot of fun while building this bot and also a lot of frustrations when it came to do harder things that I didn't know how to do back then, and also how hard it is sometimes to promote a solution to a public that maybe don't know how much work you put into it.

Like I said, it was hard to make people use this bot, mostly cause they were used to go on old websites that were doing the same thing for a long time, and with time the bot came to an end as I decided to stop hosting it. It was probably not attractive enough to be used the way it was intended initially, so I see it more as a token of my journey that continues today. Also taking a look at it I would have done it differently, mostly in term of architecture. But I can not take away the fun memories I made and also the knowledge I gained during all this time.

After this period I took a brief break away from coding as my finals were approaching and I had to get back into reading Plato. As soon as I could I wanted to learn new things, start a new project as I was already looking for schools, preparing my next year in advance. I took the choice that led me here today... I want to become a developer!

And this led me to a choice...

Part 2: My Dev's Life Story #1.2 The choice coming on sunday november 7th.

💖 💪 🙅 🚩
tomatowizard
Antoine

Posted on October 31, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related