Cs50 Problem Set 5 Solution

0 views
Skip to first unread message

Tordis Hurrle

unread,
Aug 5, 2024, 12:30:00 PM8/5/24
to bioricmiebua
Ihave to say that attending a bootcamp helped me improve my coding skills quite a lot even though I had 10 months of previous experience as a Front End developer at the company I currently work for, I was able to gain a lot of knowledge in Back End development as well as Front End development.

After graduating the bootcamp and building a few Full Stack apps and decided to learn more about computer science, and I decided to enroll in the CS50 Computer Science Course from Harvard. I have to say that the classes are amazing the assignments are quite challenging and very interesting. I have to say that I'm learning quite a lot.


It is good practice to create a pseudocode.txt file before you start coding any solution. This will help you to visualize and think for the solution of the problem before jumping to your IDE. Here I'm sharing my pseudocode.txt file.




Then you will need to do a for loop for the rows that will iterate from 1 through 8 depending on the input value from the user, and inside of the brackets you will need to create another for loop for the columns of the pyramid that will determine the width of the pyramid.




I will encourage anyone who is thinking about starting your coding career to check out the Harvard CS50 program certificate, it will give you a great tools to start your career and also the assignments and projects are very challenging and interesting.


Before we start, I have to give the usual disclaimer that these posts are mostly about how to approach the problems instead of giving outright the solutions. I also assume you have read the problem explanations given in the course's website, so that the references I make are clear. And, you can find all the posts on previous problem sets in the archive.


Notice that we have a list comprehension inside the all function, and appending to it a conditional. Then we check if that list has any False in it, if so we return False, but otherwise we return True if all the conditionals in our list are True.


With a graceful Rickroll, in this problem, we are extracting and parsing YouTube URLs for being able to easily embed them. The template for our program is, again, already given, we have to implement the parse() function for it to be called on main(). For a given string, namely s, how can we start thinking about parsing a YouTube URL?


Before going on, you should notice that it is an http link, which we should definitely turn into https for encryption and security reasons. If you have captured that part as a group, it is easy to do it with a conditional, or replacement, however you would like to do it.


You see that src occurs after width and height, and is followed by a bunch of other attributes. Now, if you do not do it in a non-greedy way, you might have something like this result as the URL you get:


We only want to get "A string", not "another string". We are literally looking for a quotation mark, one or more characters in it, and then another quotation mark. For, simplicity sake, let's do it with this pattern:


How you go on to implement these little puzzle pieces is, again, up to you as there are lots of different ways for a solution. Perhaps, one of the important takeaways is knowing the difference between greedy and lazy matching, and how to work with them. Let's see what the next one has in store.


I think this problem has also many different ways for a solution that you might even have an analysis paralysis (at least, this was my experience). What we need to do here is to convert 12-hour input format to 24-hour format. Our input should be in a certain form, though. For example, it has to have the word "to" in it, something like 9 AM to 5 PM. We may or may not be given minutes; our input can be 9 AM to 5 PM or 9:00 PM to 5:00 PM. Additionally, the input can imply a night shift, so that AM and PM given might be reverse, like 10 PM to 8 AM. All of these seem like a lot, especially if you are absolutely new to regular expressions, but again, reading the documentation and poking around might give some insights. I am not extremely satisfied with the solution I came up with, and there is definitely a more elegant way to think about it. But, let's try to understand a potential approach.


One more thing, we also need to create a test_working.py file to test our code. Handling all the cases in "How to Test" section of the problem explanation is quite sufficient here, if not, we know that check50 is our friend to guide us on which tests to cover. For testing if our code indeed raises ValueError in the right cases, we might remember how to do that from the Refueling problem from Week 5.


This one was a bit challenging, and I left some gaps on some points intentionally, but that is really the point of it. The thinking process might differ, this one is just the thinking process of the solution I came up with and hopefully provided you some insight. Let's look at the next one.


In this one, we are checking if the input we are given has "um" in it, but not counting it inside words like "yummy". The important idea is that we are looking for a word, therefore it has to have some boundaries. As the problem explanation suggests, it has to be the boundary between a word and a non-word character. Or, it can also be at the beginning or the end of the sentence. But also, we can have an input like um?, which is followed by a non-word character, so we can have that optionally as well. We also need to take care of both uppercase and lowercase characters, and re.IGNORECASE flag takes care of that.


The hints section already mentions re.findall() function, since it returns a list of all the matches it finds, we can return the length of that list from our count() function. For the tests, the edge cases to consider are already given in the problem explanation page, which will be sufficient as well. It looks daunting at first, but really, that is all there is to it. Let's look at the last problem of this week.


Since this problem's solution depends on which library you use, there is nothing much that I can give a hint about. Documentation really helps you out for each of the libraries, we also do not need to write our own tests for this one as well. It is, of course, a good habit to handle errors, and that is pretty much it.


Dealing with regular expressions might indeed be challenging if you have never used them before. Nevertheless, we have seen that it is a superpower that comes in handy with all kinds of problems. Next week, we are going to take a look at Object-Oriented Programming. Until then, happy coding.


I replaced emoticons with emojis. Happy face emoticons were changed to slightly smiling face emojis and sad face emoticons to slightly frowning face emojis. I implemented a program that accepts a string as input and returns that input with any :) converted to ? and any :( to ?.


In this equation, E represents Energy measured in Joules, m represents mass measured in kilograms, and c represents the speed of light measured as 300000000 meters per second, as stated by Albert Einstein. The program prompts the user to input mass as an integer value and then calculates the Energy in Joules as an integer using the given formula.


I hope you found these solutions useful. Remember the journey to learning to code is all about learning different perspectives and problem-solving techniques. Don't hesitate to experiment with these solutions and explore alternative ways to approach problems, using your unique approach. Keep coding and keep learning.


In software engineering, rubber duck debugging (or rubberducking) is a method of debugging code by articulating a problem in spoken or written natural language. The name is a reference to a story in the book The Pragmatic Programmer in which a programmer would carry around a rubber duck and debug their code by forcing themselves to explain it, line by line, to the duck.[1] Many other terms exist for this technique, often involving different (usually) inanimate objects, or pets such as a dog or a cat. Teddy bears are also widely used.[2] When humans are involved, it is known as confessional programming.[3]


Many programmers have had the experience of explaining a problem to someone else, possibly even to someone who knows nothing about programming, and then hitting upon the solution in the process of explaining the problem. In describing what the code is supposed to do and observing what it actually does, any incongruity between these two becomes apparent.[4] More generally, teaching a subject forces its evaluation from different perspectives and can provide a deeper understanding.[5] By using an inanimate object, the programmer can try to accomplish this without having to interrupt anyone else, and with better results than have been observed from merely thinking aloud without an audience.[6] This approach has been taught in computer science and software engineering courses.[7][8]


On 1 April 2018, Stack Exchange introduced a rubber duck avatar on their websites as a new "feature" called Quack Overflow as an April Fools' Day joke. The duck appeared at the bottom right corner of the browser viewport, and attempted to help visitors by listening to their problems and responding with solutions. However, the duck merely produced a quack sound after apparently thinking and typing. It referenced rubber ducking as a powerful method for solving problems.[9]

3a8082e126
Reply all
Reply to author
Forward
0 new messages