Question: Write a function that finds the second-smallest number in a list of numbers. Your function should: be named second_smallest take 1 argument, a list of

Write a function that finds the second-smallest number in a list of numbers. Your function should: be named second_smallest take 1 argument, a list of numbers return 1 number, the second-smallest number in the list throw an Exception if there are not enough elements in the list Take note of that last point. There's a possibility, after all, that the list your function receives as input will have 1 or even 0 elements in it. How, then, do you return the second-smallest element of a 0-element list? Well, simply, you can't. So instead, you'll raise an error. Also note: you're the one raising the error, so it's not your job to catch it! It's the job of the person using your function to note the possibility that it will throw an error, and to take appropriate action to handle it gracefully. So if your code includes any try or except statements, you're doing it wrong! For example, second_smallest([1, 2, 3]) should return 2. Another example second_smallest([10, 100, 1000, 10000]) should return 100. And second_smallest([1]) should raise an exception You cannot use any built-in sorting functions! Only range() and len() Graded de Read Only 40]; import numpy as np np.random.seed(89547) 11 = np.random.randint(-100, 100, 100) al = np.sort(11)[1] np.testing.assert_allclose(al, second_smallest(11.tolist()) 41]: np.random.seed(485) Read Only 12 = np.random.randint(0, 1000, 100) a2 = np.sort(12)[1] np. testing.assert_allclose(a2, second_smallest(12.tolist())) Read Only 42]: try: second_smallest([]) except: assert True else: assert False Read Only 43]: try: second_smallest([100]) except: assert True else: assert False
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
