In: Computer Science
Problem 5: Find Smallest Elements
In this problem, we will write a function to find the smallest elements of a list.
Define a function named find_smallest() that accepts two parameters: x and n. The parameter x is expected to be a list of values of the same time, and n is expected to be an either integer, or the value None, and should have a default value of None.
• If n is set to None, then the function should return the smallest element of x (not as part of a list).
• If n is set to a positive integer, the function should return a list consisting of the smallest n elements of list x. If n is greater than the length of the list x, then the entire list x should be returned.
Note that n=None and n=1, should produce similar, but not identical results. Both arguments will select only a single value from the list x, but if n=1 then the function should return a list containing the value, whereas if n=None then the function should simply return the value.
This problem would be challenging to do without using built-in functions, and so you are allowed to do so in this problem. As a hint, I recommend using sorting functions and slicing. However, the list that was provided as an argument to the function should not get sorted or altered as a result of the function being called.
We will now test the find_smallest() function. Create a new code cell to perform the steps below.
Create a list named my_list containing the values 39, 74, 28, 64, 17, 28, 54, 53 (in that order). Print the results of each of the following function calls.
find_smallest(my_list)
find_smallest(my_list, 1)
find_smallest(my_list, 2)
find_smallest(my_list, 5)
find_smallest(my_list, 12)
Let's confirm that the original list has not been altered.
Create a new code cell to print the list my_list.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#required code for the method.
def find_smallest(x, n=None):
list_copy = x.copy()
list_copy.sort()
if n == None:
return list_copy[0] if len(list_copy) > 0 else None
else:
if n > len(list_copy):
n = len(list_copy)
return list_copy[0:n]
#code with comments and testing (you may move each part into separate cells if you wish)
# required method
def find_smallest(x, n=None):
# taking a copy of list x
list_copy = x.copy()
# sorting the new list
list_copy.sort()
# if n is None, returning first element, or None if list is empty
if n == None:
return list_copy[0] if len(list_copy) > 0 else None
else:
# else if n exceeds number of elements in list_copy, setting n to len(list_copy)
if n > len(list_copy):
n = len(list_copy)
# returning a list containing values between indices 0 and n-1 from list_copy
return list_copy[0:n]
# code for testing
# creating my_list
my_list = [39, 74, 28, 64, 17, 28, 54, 53]
# testing with different calls
print(find_smallest(my_list))
print(find_smallest(my_list, 1))
print(find_smallest(my_list, 2))
print(find_smallest(my_list, 5))
print(find_smallest(my_list, 12))
# printing my_list to ensure that it is not altered
print(my_list)
#output
17
[17]
[17, 28]
[17, 28, 28, 39, 53]
[17, 28, 28, 39, 53, 54, 64, 74]
[39, 74, 28, 64, 17, 28, 54, 53]