This week represents a shift in the technologies that you will learn throughout the CS50 course. In Problem Set 6, we will start using some Python  and a little bit of HTML. In my opinion, this is where things start getting fun. We leave more of the low level details to the programming language, which makes things a bit easier compared to C. In Problem Set 6, we are tasked with implementing some of our previous assignments in Python. I will not be going over these in this blog. Since you have already completed these assignments in C, it is important to learn how to implement them on your own in Python. Instead, I will be focusing on the implementation of Similarities. So, let’s begin.

We will start by looking at the HTML that we must write in index.html. You’ll notice that the first two form controls that we put into the form are comprised of input tags. These allow for box in which the user can upload a file. Next, we create a select tag. The select tag will create a drop-down menu, where the child option tags will be the selectable options. For more information on the select tag, or any other html tags, visit W3Schools. We again implement a select tag of type number for the user to input a number. Finally, we add a button that will be used to submit the form.

Now, we are on to the Python code. Inside of helpers.py we need to write the code for several functions. In order to make the first three functions easier to read, I decided to write another function called list_compare that takes in two lists and returns a list of the items that are found in both lists. To start, I created an empty list called output to store all the like items. Next, I created a nested for loop that will iterate over each item in list a and then compare it to each item in list b. I also use the not operator to ensure that my output list doesn’t contain any duplicates. Finally, I append each item to the output list and once the for loops have executed, I return the output list.

To implement the lines function we must use the split function inside of Python. The split function takes in a string and a delimiter where to split the string and returns a list. In the case of lines, the delimiter will be a carriage return represented by \n. Now, we take our two lists and pass them to our list_compare function which will return a list of the lines that are in common.

To implement the sentences function we will use the sent_tokenize function in Python. The sent_tokenize function takes in a string a returns a list of the sentences in that string. So, we call sent_tokenize on our two lists and then pass those lists through our list_compare function. And now we have a list of all the sentences in common between the two strings.

To implement the substrings function I also created a get_substrings function that takes in a string and returns a list of substrings of size n. So, in the substrings function, I simply pass the two strings into get_substring, and then return the result from list_compare.

And that is all for Similarities. This exercise shows the power behind Python compared to that of C. Now get out there and give it a try. Remember to comment below with any questions and share on your favorite social media site. As always…Happy Programming!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s