February 27th, 2022
Written by Erin McCoy and Juliette Woodrow
#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[1] 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[0][0].upper() + pair[0][1:] for pair in pairs]
#this is the output on the given example list
['Zzz', 'Bbb', 'Ccc', 'Aaa']
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' ]
def read_file(filename):
"""
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 = f.readlines()
lines = lines[1:] #ignore the first line, which has column labels
for line in lines:
review_data = line.split(',')
movieName = (review_data[0]).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
movie_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
# start your code
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(movie_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(movie_labels, passed_tests, color='tab:green')
plt.xlabel('Movies')
plt.ylabel('Num Passed Tests')
plt.title('Num Passed Tests per Movie')
# end your code
plt.ylim([0, 50])
plt.show()
def plot_test(movie_data, test):
# fix the y axes so they are both at 50
test_index = TESTS.index(test)
num_passed = 0
num_failed = 0
# your code here to count number of movies that pass and fail this test
for movie in movie_data:
inner = movie_data[movie]
test_score = inner[test_index]
if test_score == 1:
num_failed += 1
else:
num_passed += 1
# provided code to make a new pyplot figure
failed_plt = plt.figure(1)
# your code here for number of movies that fail this test
plt.bar([test], num_failed, color='tab:red')
plt.xlabel(test)
plt.ylabel('Num Movies that Fail ' + test)
plt.title('Num ' + test + ' Failed in All Movies')
# provided code to make a new pyplot figure
plt.ylim([0, 50])
passed_plt = plt.figure(2)
# your code here for number of movies that pass this test
plt.bar([test], num_passed, color='tab:green')
plt.xlabel(test)
plt.ylabel('Num Movies that Pass ' + test)
plt.title('Num ' + test + ' Passed in All Movies')
# end your code
plt.ylim([0, 50])
plt.show()
def plot_all_tests(movie_data):
"""
This function plots the number of movies that pass a given test for all movies and all tests.
"""
# 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, ....]
# start your code
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
plt.ylim([0, 50])
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')
# end your code
plt.ylim([0, 50])
plt.show()
def main():
# no need to edit main
args = sys.argv[1:]
all_movie_data = read_file(FILENAME)
# use all movies as the movies to plot
if len(args) == 1 and args[0] == '-movietest':
plot_tests_per_movie(all_movie_data, all_movie_data.keys())
# use the args that the user types in as movies to plot
elif len(args) > 1 and args[0] == '-movietest':
# if user types in movies, grab all args after the -tests flag
movies = args[1:]
plot_tests_per_movie(all_movie_data, movies)
elif len(args) > 1 and args[0] == '-onetest':
test = args[1]
if test not in TESTS:
print('That is not a test we know. Here are your options:')
print(TESTS)
else:
plot_test(all_movie_data, test)
elif len(args) == 1 and args[0] == '-alltests':
plot_all_tests(all_movie_data)
if __name__ == '__main__':
main()