Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed

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

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

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

Th ey told me Id have to write a lett er. Whos got time for that?

Answered: 1 week ago

Question

How does an organization know if it is pursuing optimal strategies?

Answered: 1 week ago