Mohib Ur Rehman

Mohib Ur Rehman

Week 6: The Python Awakening

Hello folks! Welcome back to Soul’s yap session about what he learned in the gut-wrenching Harvard course known as CS50. Once again, I will be talking about my experiences and the challenges I faced and mainly about my journey of this week of CS50. 

For those of you who are unfamiliar with what I am talking about, Harvard offers a course called CS50 which any noob like me can take and learn about computer sciences from scratch. I have been taking this course for some time and so far I have reached week 6 in which we are introduced to the famous programming language known as Python.

To be honest, this week of CS50 as compared to the last one was really easy. I am the type of guy who has been programming in the lower level languages C|C++ for some time, I am still a noob at it, I admit it and recently even started learning assembly (I can’t stay consistent with it, I need beating) But due to all this experience, the shift to python didn’t seem that hard.

Well, of course – It’s not like I didn’t face any hardship at all, whatever challenged me, every little detail about it, and most importantly how I tackled it. I will go through it all, so this blog can be helpful for anyone who reads it.

Experience with Lecture Series

So, when I started this week, I was pretty excited for the shift from lower-level languages to python. I always wanted to learn python and did have some experience in it but it wasn’t enough. I started learning python a year ago but I left it in between and forgot about it. Later I jumped to another programming language C++.

And now…because of CS50 I was back to learning python after a long time but this time, I was a different person, so I knew I wouldn’t quit it like last time even if bored me to death. (Yeah…not really a big Python fan.)

Starting of Week 6

So, when I started week 6, our teacher David threw the translation of the previous week’s code in Python right in our faces and just like always I didn’t understand the code, I got overwhelmed by it thoughts like: “am I that bad in programming?” started occurring to me but I quickly snapped out of it and took a unique approach to tackle this. What approach?

I made a new note section in Obsidian and wrote the following:

“So the week started and David translated the problem set of weeks 4 and 5 into Python, by the end of the lecture Goal is to translate my version of these problem sets to Python”

It might seem like a small thing but it gave me hope and courage to continue my journey of week 6. Another important thing to note is that, unlike previous week, where I only took notes for the concepts which troubled me. I changed that approach completely this time.

You might be wondering: “What approach I took, right?” Well, I basically started noting everything down, things that seemed easy and especially the things that were hard. “Why I did do that? I did it because it seemed right, I was learning a new language, and I simply wanted to note everything down in my own wording.

How I Utilized C to Help Me in Python

You thought this was it? Nah! One of the most important things that I did was I didn’t just leave the previous languages in which I had some experience. Yes, I am referring to C and C++.

The way I used both of these languages to help me learn Python is pretty interesting. The concepts to which we were introduced in python were similar to C. Well, because the basics of programming are always the same, it’s just the syntax, that you need to get used to.

I simply referred back to C, each time I was introduced to a different topic in python. I practiced that concept in C and then I just applied all that knowledge to python and boom!

For instance, even something as simple as printing “Hello World” works similarly across languages – the syntax just becomes much cleaner in Python.

I kept repeating this method over and over again. The example above is really simple, but you can take the same approach when programming something complex – at least that is what I did. I always referred back to the programming language in which I had faith over, I solved the problem in that language, and then boom! I made the switch to Python and translated it.

I simply used this approach throughout the lecture series. Sure, there were times when things were getting a bit hard. Topics such as truncation always got to my head. But guess what? C always had my back, so I used it whenever I could to help me understand whatever was challenging me.

Basically, this is how I completed the lecture but completing the lecture is the easy part, now we had to deal with the ending boss. Yes, I am talking about the problem sets.

Experience with Problem-sets

This week’s problem sets were different. Different in what sense? Well, we had to translate some of the most important problem sets from the previous week into Python. Sounds easy, right? Well, not for me.

Turns out, I had accidentally missed some of the problem sets from the previous week and I hadn’t realized that until some time ago. Week 6 problem sets included translating those same problem sets that I had missed earlier. Can you actually believe this? True definition of being cursed.

I did plan to implement the problem sets I had missed earlier in the near future… but nature wanted me to do them right now, I guess. So, how exactly did I translate those problem sets? When they didn’t exist in the first place, let’s get to that.

Problem Set 1

So, the first problem set we are supposed to translate is Mario, we do have 2 versions of it.

The first version of that problem set is known as Mario-less which is for people who are less comfortable solving problems and the next is called Mario-more which is for people who are just stronger, smarter, and better (just kidding). Luckily I did complete both of them in the past.

But here is the twist, unfortunately, I didn’t save the code for both of them. I thought I had it but when I checked it, it wasn’t there. So, I had to rewrite it as well. I wrote both of them in C first which did challenge me a bit because I don’t have 1 TB of memory rather my brain is like a register.

Anyway, after completing both versions code in C, I had to translate it into Python. Although It is not really necessary to solve both of them, you can just complete 1 of them and you are good to go. But.. I am me, I had to do both of them to satisfy my inner self. Damn, I should become a philosopher at this point, I yap too much.

While translating to Python, I faced one small issue with Python’s loop behavior – specifically how it handles range limits differently from C. It took me a moment to adjust to that difference, but once I got it, the rest went smoothly.

The difficult version (Mario-more) was quite similar, just with a few more tweaks. My fundamentals were cleared because of this problem which is why It didn’t take me much time to solve the Mario-more version.

Problem Set 2

Time passes fast… we are now onto the second problem set, so what was this about? It’s called cash and yeah you have to deal with currency in this problem set. You can visit the website called edx.com, search for CS50, find this problem set, and understand it yourself because if I start explaining the problem set itself, I would turn old.

So, the main key in this problem set was actually understanding the problem set, although this is the key to solving 90% of programming problems, the rest is just being familiar with the syntax. So, how did I go about implementing the solution to solve this problem set? You are probably curious about it, right? If you aren’t then I will cry.

Anyway, the main challenge revolved around a function that handled the logic of breaking down amounts efficiently. Once you get that core function right, everything else just falls into place.

I originally implemented this program in C using a concept called pass by reference, which involves pointers. I did that to reduce my lines of code and just because I wanted to. And also, there’s a quote:

“If you can, just do it.” – me

Don’t go and murder your friend because of this quote, I just said it in the context of this particular problem.

Well, the only thing I didn’t know was how the hell pass-by reference works in Python because to my current knowledge, I don’t think there are pointers or any of that stuff in Python.

Turns out, you don’t need to. You can simply return the values, make new variables, and just store those return values into those variables by calling the function. I figured all this out by googling, and asking AI how stuff works in python. Using AI isn’t a bad thing in my opinion, just use it wisely and don’t tell it to give you the solution.

Problem Set 3

he last problem set is called readability. I was also supposed to implement it a while ago but I didn’t because I am blind. That is why I had to implement this as well in C first, after that I translated the code into Python which was fairly simple.

The trickiest part was counting letters. They told us to count letters but didn’t give us any clue about how to identify them. Maybe they did it purposely, so we can figure it out. I had a friend who banged his head against the wall for days to figure it out. He did give me some clues such as using the ASCII table somehow, and that’s how I thought I was going to do it as well.

But then I found a much simpler way – using Python’s built-in .isalpha() function. That single function solved the headache. In C, things were much more manual – you have to check for each character and manage strings using null terminators. That’s one of the differences I noticed and appreciated between the two languages.

The other functions in this problem set required counting words and sentences – those were fairly simple to handle by adapting the same logic.

The next challenge I faced in implementing this problem was the Coleman–Liau formula (whatever the hell it is called, I don’t care). I didn’t initially realize that I had to implement it directly in code. After some time and help, I figured out that you are just supposed to apply the formula in code and that’s it.

And with that, I ended up solving this problem set as well. Freedom finally!

There is another problem set called DNA after readability, but I didn’t solve it. It had too much biology in it. I am not doing that shit again. I already solved another version of DNA in week 5 but this time? Hell nah! I ain’t doing that. Perhaps in the future… maybe.

Personal Advice

I don’t have much to say, but still I would like to again emphasize on the point that some of you reading this might think that I am some genius who solved this very quickly. No! Everyone has their own way of doing things, don’t compare yourself with other’s on the internet or even your friend group in a way that gets you demotivated.

And if you do end up solving some problems quicker than others, no need to get cocky as well. Just stay humble, and consistent and keep going. I am rooting for all of you out there.

And as always stay safe, stay secure.