Practicing with the VS Code Debugger

Practicing with the VS Code Debugger#

You’ve been asked by a colleague to help them figure out why they’re having issues with a package they wrote. The packages designed to compare two sentences, figure out how many words they have in common, and then return the word that (a) appears in both sentences, and (b) when more than one word appears in most sentences, identify the word in both sentences with the most total occurrences.

So for example, if our sentences were:

  • “Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy lies a small unregarded yellow sun.”

  • “Orbiting this at a distance of roughly ninety-two million miles is an utterly insignificant little blue green planet whose ape-descended life forms are so amazingly primitive that they still think digital watches are a pretty neat idea.”

The result would be the word “of” (it appears in both, and appears 3 times in total).

Setup#

Copy the following code into a new .py file (and save it — remember VS Code doesn’t always work correctly when working with an unsaved file!):


"""Debugger exercise."""


def split_string(string):
    """Given a string, split it into a list of 'words'."""
    start_of_word = 0
    words = []
    for i in range(len(string)):
        if string[i] == " ":
            words.append(string[start_of_word:i])
            start_of_word = i
    return words


def count_words(words):
    """Count how often words occur in a list."""
    word_counts = dict()
    for i in words:
        if i in word_counts:
            word_counts[i] += 1
        else:
            word_counts[i] = 1
    return word_counts


def create_collective_count(word_counts1, word_counts2):
    """Take two word counters and combine them into one."""
    collective_count = dict()
    for i in word_counts1:
        if i in word_counts2:
            collective_count[i] = word_counts1[i] + word_counts2[i]
    return collective_count


def word_counter(string1, string2):
    """Find the word that occurs the most across the two strings."""
    string1_words = split_string(string1)
    string2_words = split_string(string2)
    word_counts1 = count_words(string1_words)
    word_counts2 = count_words(string2_words)
    collective_count = create_collective_count(word_counts1, word_counts2)
    curr_max = 0
    for i in collective_count:
        if collective_count[i] > curr_max:
            curr_max = collective_count[i]
            curr_word = i
    return curr_word


if __name__ == "__main__":

    # Test Case 0
    print("Test Case 0:")
    print("The result should be 'of'")

    string1 = (
        "Far out in the uncharted backwaters of the "
        "unfashionable end of the western spiral arm of "
        "the Galaxy lies a small unregarded yellow sun."
    )
    string2 = (
        "Orbiting this at a distance of roughly ninety-two million "
        "miles is an utterly insignificant little blue green "
        "planet whose ape-descended life forms are so amazingly "
        "primitive that they still think digital watches are a pretty neat idea."
    )

    print("Comparing:")
    print(f"Sentence 1: {string1}")
    print(f"Sentence 2: {string2}")

    test_word = word_counter(string1, string2)
    print(f"Result: {test_word}")

    # Test Case 1
    print("Test Case 1:")
    print("The result should be 'No matching words'")

    string1 = "I love Practical Data Science."
    string2 = "Who is Nick Eubank?"

    print("Comparing:")
    print(f"Sentence 1: {string1}")
    print(f"Sentence 2: {string2}")

    test_word = word_counter(string1, string2)
    print(f"Result: {test_word}")

    # Test Case 2
    print("Test Case 2:")
    print("The result should 'code'")

    string1 = "This is a test of my code."
    string2 = "I hope there's not bugs in this code."

    print("Comparing:")
    print(f"Sentence 1: {string1}")
    print(f"Sentence 2: {string2}")

    test_word = word_counter(string1, string2)
    print(f"Result: {test_word}")

Exercise 1:#

Familiarize yourself with the code and its contents.

Note that when you run this file from the command line (e.g. navigate to where it’s located and run python debugger.py), the first thing executed are the three test cases under if __name__ == "__main__":. As you can see, these all call word_counter, so you should start your examination there.

One trick, for those who aren’t familiar with string manipulation: You can subset a string just the way you would a list. So for example if I have a string:

my_string = "My name is Nick"

I can shorten it to my name by subsetting:

my_string[11:]
'Nick'

Exercise 2:#

Run the script from the command line. What happens? Did all three tests pass?

Exercise 3:#

What type of error did you just encounter—a logical error or a syntax error?

Exercise 4:#

Now run debugger.py in VS Code’s Python Debugger. Try to figure out the problem you saw when you ran your code at the command line.

Exercise 5:#

Once you’ve fixed that bug, run your script from the command line again. Did the code run without problems? Did all the tests turn out as expected?

Exercise 6:#

Based on your answers to Exercise 5, which kind of bug are you encountering: a syntax error or a logical error?

Exercise 7:#

Using the debugger, try and track down the source of the problem you’ve now found.