Q3

This question will focusing on indexing lists and dictionaries directly, no loops needed.

A

Reassign index to be the middle index of the list list_of_numbers. DO NOT hard-code a number (hard-coding means using literals; see L2, slide 27).


In [1]:
import numpy as np
np.random.seed(8948394)

list_of_numbers = np.random.randint(10, size = 100000).tolist()
index = -1

### BEGIN SOLUTION

### END SOLUTION

B

Reassign middle to be the middle value of the list list_of_numbers. DO NOT hard-code a number (hard-coding means using literals; see L2, slide 27).


In [2]:
import numpy as np
np.random.seed(95448)

list_of_numbers = np.random.randint(10, size = 100000).tolist()
middle = -1

### BEGIN SOLUTION

### END SOLUTION

C

Write a single boolean statement that tests whether the defined key exists in the defined dictionary.


In [ ]:
dictionary = {"voila": "in", "view": "a", "humble": "vaudvillian", "veteran": "cast", "vicariously": "as", "both": "victim", "and": "villain"}
key = "villain"

### BEGIN SOLUTION

### END SOLUTION

D

Write a single boolean statement that tests whether the defined value exists in the defined dictionary.


In [3]:
dictionary = {"voila": "in", "view": "a", "humble": "vaudvillian", "veteran": "cast", "vicariously": "as", "both": "victim", "and": "villain"}
value = "villain"

### BEGIN SOLUTION

### END SOLUTION

E

Write code to split the following string sentence into words, and store only the second-to-last word in the variable last_word.


In [4]:
sentence = "A data scientist is someone who is better at computer science than a statistician, and better at statistics than a computer scientist."
last_word = None

### BEGIN SOLUTION

### END SOLUTION