Section 8 Solutions

November 14th, 2021

Written by Erin McCoy and Juliette Woodrow

List Comprehensions

#Construct a list of all the numbers 1-1000 inclusive
lst = [num for num in range(1001)]

#Construct a list of all multiples of 2 from 0 to 2000 inclusive
lst = [num*2 for num in range(1001)]

#starting list of numbers
nums = range(20) #this makes a list of numbers from 0 to 19 [0, 1, 2, ... , 19]

#Produce a list of the absolute difference between each of the numbers in lst and 10.
abs_diff = [abs(num - 10) for num in nums]

#this is the output on the given example list
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

#Produce a list of the absolute difference between the numbers in lst that are between 10 and 15 inclusive, and 10.
restricted_abs_diff = [abs(num - 10) for num in nums if num >= 10 and num <= 15]

#this is the output on the given example list
[0, 1, 2, 3, 4, 5]

#pairs list
pairs = [('zzz', 3), ('bbb', 10), ('ccc', 4), ('aaa', 6)]

#Produce a list of the second elements of each tuple in pairs.
second_elems = [pair for pair in pairs]

#this is the output on the given example list
[3, 10, 4, 6]

#Produce a list of the first elements of each tuple in pairs, except that the first character of each of these elements is made #uppercase. You can assume that each such element in the tuples has at least one character.
upper = [pair.upper() + pair[1:] for pair in pairs]

#this is the output on the given example list
['Zzz', 'Bbb', 'Ccc', 'Aaa']

Bechdel Test Solution

import sys
import matplotlib.pyplot as plt

FILENAME = 'data/nextBechdel_allTests.txt'
TESTS = ['Bechdel', 'Peirce', 'Landau','Feldman','Villareal', 'Hagen', 'Ko', 'Villarobos','Waithe', 'Koeze-Dottle','Uphold','White', 'Rees-Davies' ]

"""
Reads the information from the specified file and builds a new
movie_data dictionary with the data found in the file. Returns the
newly created dictionary.
"""
movie_data = {}
with open(filename) as f:
lines = lines[1:] #ignore the first line, which has column labels
for line in lines:
review_data = line.split(',')
movieName = (review_data).lower()
rest_data = [int(val) for val in review_data[1:]]
movie_data[movieName] = rest_data
return movie_data

def plot_tests_per_movie(movie_data, movies):
"""
This funciton plots the total number of test passed for all movies.
"""
# provided code to create the lists you are building
labels = [movie.title() for movie in movies] # str.title() capitalizes the first char of each word in str
failed_tests = []
passed_tests = []

# Populate failed_tests and passed_tests by movie in movies
# Ex// Movies = [Arrival, Kung Fu Panda 3, Trolls, Zootopia]
# failed_tests = [8, 8, 10, 10] where each number is the number of failed test per movie at same index in movies
# passed_tests = [5, 5, 3, 3] where each number is the number of passeed tests per movie at same index in movies

for movie in movies:
low = movie.lower()
num_passed = 0
num_failed = 0
for datapoint in movie_data[low]:
if datapoint == 1:
num_passed += 1
if datapoint == 0:
num_failed += 1

passed_tests.append(num_passed)
failed_tests.append(num_failed)

# provided code to make a new pyplot figure
failed_plt = plt.figure(1)

# write code below to plot a bar plot for the failed tests per movie
plt.bar(labels, failed_tests, color='tab:red')
plt.xlabel('Movies')
plt.ylabel('Num Failed Tests')
plt.title('Num Failed Tests per Movie')

# provided code to make a new pyplot figure
passed_plt = plt.figure(2)

# write code below to plot a bar plot for the passed tests per movie
plt.bar(labels, passed_tests, color='tab:green')
plt.xlabel('Movies')
plt.ylabel('Num Passed Tests')
plt.title('Num Passed Tests per Movie')

plt.show()

def plot_all_tests(movie_data):
"""
This function plots tests for all movies.
"""
# provided code to create the lists you are building
labels = TESTS
failed_tests = [0,0,0,0,0,0,0,0,0,0,0,0,0]
passed_tests = [0,0,0,0,0,0,0,0,0,0,0,0,0]

# Populate total failed_tests and passed_tests across all movies in movie_data for each test
# Ex //  ['Bechdel', 'Peirce', 'Landau','Feldman','Villareal', 'Hagen', 'Ko', 'Villarobos','Waithe', 'Koeze-Dottle','Uphold','White', 'Rees-Davies' ]
# failed_tests = [18 (num movies that fail bechdel test), 10, ....]
# passed_tests =  [32 (num movies that pass bechdel test), 40, ....]

for movie in movie_data:
inner = movie_data[movie]
for i in range(len(inner)):
pf = inner[i]
if pf == 0:
failed_tests[i] += 1
if pf == 1:
passed_tests[i] += 1

# provided code to make a new pyplot figure
failed_plt = plt.figure(1)

# write code below to plot a bar plot for the failed tests per movie
plt.bar(labels, failed_tests, color='tab:red')
plt.xlabel('Tests')
plt.ylabel('Num Tests Failed')
plt.title('Combined Tests Failed Per Test for All Movies')

# provided code to make a new pyplot figure
passed_plt = plt.figure(2)

# write code below to plot a bar plot for the passed tests per movie
plt.bar(labels, passed_tests, color='tab:green')
plt.xlabel('Tests')
plt.ylabel('Num Tests Passed')
plt.title('Combined Tests Passed Per Test for All Movies')

plt.show()

def main():
# no need to edit main
args = sys.argv[1:]