Part A and B in Python, Part C in Node.JS

Part A (7 points) – writing text files

You will need a to solve part A.

Define a Python function named write_authors with two parameters. The first parameter will be a list of dictionaries. Each dictionary in the list will include “author” among its keys. The second parameter will be a string specifying the name of the file to write to. Your function should open the file in a manner that ERASES the file’s contents. For each entry in the first parameter, write the value associated with its “author” key to the file and then a newline. Your function does not need to return anything.

Sample test cases:
write_authors([], “empty.txt”) ensures a file named empty.txt exists and is empty. (finds bug if file not opened properly)
write_authors([ {“author” : “Hertz”}, {“title” : “Hop on Pop”, “author” : “Seuss”} ], “simple.txt”) results in a file named simple.txt containing Hertz on its initial line and Suess on its other line. (finds bug if not looping through each entry in the list)

 

Part B (4 points) – writing CSV files

You will need a to solve part B.

Define a Python function named mult_years that two parameters. The first parameter will be a list. The second parameter will be a string specifying the name of a CSV file. Each entry in the first parameter is itself a list of 3 values representing the following fields:
Location Name, 2016 Malaria Cases, 2017 Malaria Cases
The location name will be a str and the latter two values will be ints. Your function will need to open the CSV file so that it can write to that file WITHOUT erasing any existing contents. For each entry in the first parameter, if it has non-zeros at both index 1 and index 2 (e.g., the location reported malaria cases in both years), write the entry to the CSV file. Since location name data may include commas, you should do this using Python’s built-in CSV library. You do not need to do anything when either (or both values) are equivalent to 0.

Sample test cases:
mult_years([], “unchanged.csv”) ensures a file named unchanged.csv exists, but that its contents are preserved (finds bug if file not opened properly)
mult_years([ [“Bronx”, 138, 51], [“Mongomery, MD”, 101, 99] ], “a.csv”) ends with the file named a.csv file having its next-to-last row with Bronx, 138, 51 and its last row containing “Mongomery, MD”, 101, 99 (finds bug if not using the CSV libraries for writing to the file)

 

Part C (4 points) – using DOM functions

You will need s to solve parts C1 and C2.

C1.

Create a JavaScript function named displayGrades that has 1 parameter. This parameter will be an object containing 2 keys. The first key is “score” and it’s value will be a Number. The second key is “letter” and its value is a String. Your function needs to set the contents of the div element with an id of “num” to the value associated with the key “score” and the contents of the div element with an id of “result” to the value associated with the key “letter”. Your function does not need to return anything.
Hint: variableName.toString() returns a human-readable representation of variableName‘s value. This is often used when updating the contents of an HTML element to a Number.

Sample test cases require JavaScript code AND a webpage and so cannot be shown on this document. Sorry!

C2.

Create a JavaScript function named labExamScore that does not have any parameters. Your function should return the larger of the values stored in the text boxes (input widgets) whose ids are “original” and “makeup”.
Hint: The built-in function parseFloat(string_value) returns the Number represented by the string string_value.

Sample Test Cases require JavaScript code AND a webpage and so cannot be shown on this document. Sorry!