Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE PYTHON3; DO NOT IMPORT ANY PACKAGES Please do all of question 2, which has 3 parts. Thank you! Here's what the code looks like

USE PYTHON3; DO NOT IMPORT ANY PACKAGES

Please do all of question 2, which has 3 parts. Thank you!

image text in transcribed

image text in transcribedimage text in transcribed

Here's what the code looks like in the editor:

# Question 2.1 def fix_1(lst1, lst2): """ Divide all of the elements in `lst1` by each element in `lst2` and return the values in a list.

>>> fix_1([1, 2, 3], [0, 1]) [1.0, 2.0, 3.0] >>> fix_1([], []) [] >>> fix_1([10, 20, 30], [0, 10, 10, 0]) [1.0, 2.0, 3.0, 1.0, 2.0, 3.0] """ out = [] for div in lst2: for num in lst1: out.append(num / div) # add try-except block return out

# Question 2.2 def fix_2(*filepaths): """ Open all of the files in `filepaths` and PRINT a string for each file that indicates if this file can be opened or not.

>>> fix_2('files/a.txt', 'files/b.txt', 'files/c.txt') files/a.txt opened files/b.txt not found files/c.txt not found

>>> fix_2('docs.txt') docs.txt not found """ for filepath in filepaths: cur_file = open(filepath, "r") # add try-except block cur_file.close()

# Question 2.3 def fix_3(lst): """ For each element in `lst`, add it with its following element in the list. Returns all of the summed values in a list.

>>> fix_3([1, '1', 2, None]) []

>>> fix_3([1, 2, 3, 4]) [3, 5, 7]

>>> fix_3([]) [] """ sum_of_pairs = [] for i, _ in enumerate(lst): sum_of_pairs.append(lst[i] + lst[i + 1]) # add try-except block return sum_of_pairs

Question 2 In this question, you need to fix the implementation of three functions so that they no longer crash. The provided implementation will not work for certain values due to exceptions, thus you need to add a try-except block in each function to deal with this. You should run the doctests to see which exceptions will be thrown. For this question, all you need to do is add a try-except block to handle the potential exception. You should not modify the implementation of the given code, and you are not required to add input validations. Part 1 This function is supposed to divide each element in Ist1 by each element in /st2, and append each result to an output list. However, our current implementation results in a crash for certain values. If we catch the exception, we should just move on to the next iteration of the loop instead of terminating the entire loop. def fix_1(lsti, lst2): >>> fix_1([1, 2, 3], [0, 1]) [1.0, 2.0, 3.0] >>> fix_1([], []) [] >>> fix_1([10, 20, 30], [0, 10, 10, 0]) [1.0, 2.0, 3.0, 1.0, 2.0, 3.0] out = [] for div in Ist2: for num in lsti: out.append(num / div) # add try-catch block return out Part 2 This function is supposed to open each filepath in *filepaths. If we are able to open the file, we should print a string '{filepath} opened'. If we are not able to open the file, we should print '{filepath} not found'. Note that {filepath) should be the given file path string, not the actual string '{filepath}'. def fix_2(+filepaths) : >>> fix_2('files/a.txt', 'files/b.txt', 'files/c.txt') files/a.txt opened files/b.txt not found files/c.txt not found >>> fix_2('docs.txt') docs.txt not found for filepath in filepaths: cur_file open(filepath, "1") # add try-catch block cur_file.close() Part 3 This function is supposed to add each element with its following element in the list and return all of the summed values in a list. In the current implementation, there are two potential errors that may arise. If they are thrown, you should catch them and print their types (you can use the type() function). Otherwise, you would just append Ist[i] + Ist[i+1] to the output list. def fix_3(1st): 11 !!!! >>> fix_3([1, 'l', 2, None]) [] >>> fix_3([1, 2, 3, 4]) [3, 5, 7] >>> fix_3([]) [] sum_of_pairs [] for i, - in enumerate (lst): sum_of_pairs.append(lst[i] + lst[i + 1]) # add try-catch block return sum_of_pairs

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions