123k views
5 votes
Documentation: Use the following template for EVERY function that you write. The docstring below should be placed inside of the function, just ffter the function signature. Surpose: barameter(s): Return Value: Problem A. (10 points) Total Time Write a function total_time(fname, employee) that takes in a string fname representing the name of a file in the same folder which contains data on how many hours various employees reported working in a given week, and a string employee representing the name of one of the employees. The file will be formatted as follows: Each line will have an employee name, then a space, then the number of hours worked. It is possible that a name will appear multiple times in a file, since some employees may report hours worked more frequently than once a week. If this happens then the value returned should be the sum of all hours reported by the employee. You must return a float representing the total number of hours worked by the designated employee. - If the employee does not report any hours during the week, the function should return 0.0 . - If the file requested does not exist within the current folder, the function should return -1.0 , without causing any runtime errors. - Use a try-except block to avoid a FileNotFoundError in the case that the specified file does not exist. - Be sure to close the file before you return from the function. Examples (assumes that fakefile.txt does not exist within your current directory): ১> total_time('hours1.txt', 'Leslie') 22.0 ১> total_time('hours2.txt', 'Jeff') 24.0 ১> total_time('hours3.txt', 'Tian') 0.0 Problem B. (10 points) Most Populous City n this problem, we'll use a data set containing information bout the twelve most populous cities for a given year. Take a ook at the cities. csv file. Write a function most_populous (year, region) that akes in a string year that has the year and another string region that has the region to look for. The function should ead through the cities.csv file and return a list of all the ities that were from the given region in the given year. - Regions named in the data are "East Asia", "Middle East", "South Asia", "Europe", "North America", and "Latin America" (odd choices, we know, the data set is compiled from other sources.) - Only the twelve most populous cities for a given year are in the data set, so a city that is among the most populous one year might not appear in a later year. - You may assume that cities.csv exists in the folder you're running Python from: no need to use a tryexcept. - It might be helpful to open the cities.csv file with a text editor program (e.g. VSCode, Notepad, TextEdit) rather than something like Excel. A text editor will show you the data in the same format that Python will read it. >> most_populous('1500', 'East Asia') 'Beijing', 'Guangzhou', 'Hangzhou', Nanjing'] >> most_populous('1673', 'Europe') 'Istanbul', 'London', 'Naples', 'Paris'] ১> most_populous( '1499', 'South Asia') most_populous('1500', 'USA') Problem C. (10 - 10 Tempentstasis

1 Answer

4 votes

Final answer:

The first problem requires writing a function to calculate total work hours from file data for a specific employee, handling file non-existence. The second involves creating a function that identifies the most populous cities in a given region and year by accessing and processing information from a CSV file.

Step-by-step explanation:

Problem A is about writing a function called total_time, which reads a file containing employee work hours and calculates the total hours worked by a specified employee. The function takes two parameters: a string representing the filename (fname) and another string representing the employee's name (employee). It returns a float which is the sum of hours, or 0.0 if the employee did not work any hours, or -1.0 if the file does not exist.

For Problem B, the task is to write a function called most_populous that reads data from a 'cities.csv' file and returns a list of the most populous cities from a specified region for a given year. The function needs to handle strings for both the year and region parameters. By analyzing the CSV data, the function returns a list of city names that match the criteria.

User Gareththegeek
by
8.2k points