{ "q1": { "type": "multiple", "question": "\n

Given the following Python script, arguments.py:

\n\n
\n#!/usr/bin/env python3\n\nimport sys\n\nfor arg in sys.argv:\n    print(arg)\n
\n\n

To run this script, you can do one of the following:

\n\n
\n# 1. Invoke python3 interpreter explicitly\n$ python3 arguments.py hello world\n\n# 2. Make the script executable and then run the script\n$ chmod +x arguments.py\n$ ./arguments.py hello world\n
\n\n

Play around with this script, and then determine which of the following\nstatements are true (select all that apply)?

\n", "responses": { "import": "The import sys statement loads the sys Python module.", "arguments": "The command line arguments to the script are stored in sys.argv.", "print": "It is not necessary to include the parentheses when calling the print function.", "she-bang": "The first line in the script, #!/usr/bin/env python3 is called the she-bang, and it tells the operating system to use the Python interpreter to evaluate this script.", "length": "In the examples above, the len(sys.argv) would be 3." } }, "q2": { "type": "blank", "question": "\n

Given the following Python script, filtered.py:

\n\n
\n#!/usr/bin/env python3\n\nimport sys \n\ntarget = 're'\nwords  = ____                       # 1. Initialize to empty list\n\nfor line in ____:                   # 2. Loop over standard input\n    for word in ____:               # 3. Loop over each word\n        if target in ____:          # 4. Search lowercased word\n            words.____(word)        # 5. Add word to list\n\n# 6. Number of words in list\nprint(f'There are {____(words)} words that contain \"{target}\":')\n\n# 7. Loop over each word in list, along with its index (first index is 1)\nfor index, word in sorted(____(words, 1)):\n    print(f'{index}. {word}')\n
\n\n

Complete the filtered.py script by filling in the blanks.\nThe purpose of the script is to read one line from standard input at a\ntime, then check if any of the words in the line contain the\ntarget string. If so, then add the word to the words\nlist. Afterwards, print out a summary of how many words contain the\ntarget and then all the words in the list in ascending order.

\n\n

Here is an example of how the script can be executed:

\n\n
\n# Filter README.md in assignments repository\n$ ./filtered.py < ../README.md \nThere are 30 words that contain \"re\":\n1. reading\n2. repository\n3. Readings\n4. **reading*\n...\n
\n" }, "q3": { "type": "multiple", "question": "\n

Given the Python script above, filtered.py, which of the\nfollowing statements are true (select all that apply):

\n", "responses": { "stdin": "sys.stdin is a list.", "linear": "The target in expression is a linear search.", "descending": "To have the words print in descending order, you can add the reverse keyword argument to sorted.", "set": "To only track the unique matches, you can use a set() instead of a list for words.", "split": "By default, the .split() method separates words by whitespace." } }, "q4": { "type": "blank", "question": "\n

Given the following Python script, word_count.py:

\n\n
\n#!/usr/bin/env python3\n\nimport sys \n\ncounts = ____                       # 1. Initialize to empty dictionary\n\nfor line in ____:                   # 2. Loop over standard input\n    for word in ____:               # 3. Loop over each word\n        word = ____                 # 4. Lowercase word\n        if word ____ counts:        # 5. Check if counts is missing word\n            ____ = 1                # 6. Insert value of 1 for word in counts\n        else:\n            ____ += 1               # 7. Increment value of word in counts\n\nfor word, count in ____:            # 8. Loop over key, value pairs in counts\n    print(f'{count:>8} {word}')\n
\n\n

Complete the word_count.py script by filling in the blanks.\nThe purpose of the script is keep track of how often each word in standard\ninput appears. After reading all the input and updating a counts\ndictionary, the script prints out the counts of each word it has seen.

\n" }, "q5": { "type": "multiple", "question": "\n

Given the Python script above, word_count.py, which of the\nfollowing statements are true (select all that apply):

\n", "responses": { "if_get": "You can replace the whole if/else statement with counts[word] = counts.get(word, 0) + 1.", "sorted_count": "The counts are printed in sorted order by count.", "sorted_word": "The counts are printed in sorted order by word.", "assignment": "The word = assignment is unnecessary; simply perform the expression on the right-hand side.", "constant": "The if word ____ counts express is a constant time operation." } }, "q6": { "type": "blank", "question": "\n

Given the following Python script, subtract.py:

\n\n
\n#!/usr/bin/env python3\n\nx = 10\n\ndef subtract(x, n=1):\n    ''' Update x by subtracting n '''   # 1. What is this called? ____\n    x = x - n \n    print(x)\n\nprint(x)                                # 2. What does this output? ____\nsubtract(x)                             # 3. What does this output? ____\nprint(x)                                # 4. What does this output? ____\nsubtract(x, x)                          # 5. What does this output? ____\n
\n\n

Answer the questions in the comments by filling in the blanks below:

\n" } }