Question

In: Computer Science

Problem 5: Find Smallest Elements In this problem, we will write a function to find the...

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.

Solutions

Expert Solution

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]

Related Solutions

Problem 3: Minimum In this problem, we will write a function to find the smallest element...
Problem 3: Minimum In this problem, we will write a function to find the smallest element of a list. We are, in a sense, reinventing the wheel since the min() function already performs this exact task. However, the purpose of this exercise is to have you think through the logic of how such a function would be implemented from scratch. Define a function named minimum(). The function should accept a single parameter named x, which is expected to be a...
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an...
3. Suppose we want to find the 2nd largest and 2nd smallest elements simultaneously for an input of n numbers stored in an array A[1:n]. Compare the following strategies in terms of their exact number of comparisons. Strategy 1: adapt the two separate For loops idea for minimum and maximum. Strategy 2: Run mergesort to sort the numbers in ascending or descending order, then output the 2nd ranked and (n-1)th ranked elements. First write each algorithm in pseudocde, then analyze...
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
Write a Python program to find the smallest positive integer that is a perfect square and...
Write a Python program to find the smallest positive integer that is a perfect square and it contains exactly three different digits.
Problem 3 (5 marks). Suppose elements a[i] and a[i+k] are in the wrong order and we...
Problem 3 . Suppose elements a[i] and a[i+k] are in the wrong order and we swap them. Prove that this will remove at least 1 inversion but at most 2k − 1 inversions. (This is textbook problem 7.3.) Further explain why both the lower bound of 1 and the upper bound of 2k − 1 can be attained for any i, k, where k > 0.
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest...
(C++ ) ·In “recursive.cpp”, write a recursive function minDoub() which: ·returns the address of the smallest value in the array. If the array is empty, return the “end” pointer ·and takes as parameters: (1)   a pointer to double. The pointer is the address of the start of an array, (2)   the “end” pointer to the address after the array (3)   and the address of the smallest value seen so far ·Write main() to test this function – try a case where the array...
• Write a C++ program to find the largest umber, smallest number and sum of all...
• Write a C++ program to find the largest umber, smallest number and sum of all the element of a given array of 20 integers • Note − Declare array to 20 numbers − Input values for 20 array elements − Find largest number − Find smallest number − Find sum of all numbers • Display the results
Write two algorithms to find both the smallest and largest numbers in a list of n...
Write two algorithms to find both the smallest and largest numbers in a list of n numbers. In first algorithm, you simply write one loop to find the minimum and maximum. So there will be 2(n - 1) comparisons. In second algorithm, you try to find a method that does at most 1.5n comparisons of array items. Determine the largest list size (i.e., n) that Algorithm 1 can process and still compute the answer within 60 seconds. Report how long...
Write a C++ function which will search for TWO elements. The two elements must be right...
Write a C++ function which will search for TWO elements. The two elements must be right next to each other, contiguously. The prototype of the function should be: bool search_adjacent_elements(int const *x, size_t n, int target1, target2); It will return true if target1 and target2 exist in the array x somewhere, with target1 immediately to the left of target2. For example: int x[] = { 3, 5, 10, 14, 19, 25, 45, 60 }; cout << search_adjacent_elements(x, sizeof x /...
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1.
write a method that returns the index of the second smallest element in an array of integers. If the number of such elements is greater than 1. return the second smallest index. Use the following header:public static int index of seconds sma11eststenent tint array
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT