Question

In: Computer Science

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 list of elements of the same type. The function should return the smallest element of x. The function should work on lists of integers, floats, and strings. In each case, the "smallest" element is defined as the one with the lowest ranking with respect to the < comparison operator. For strings, this should yield the earliest string when ordered alphabetically.

The function should not print anything. It should not create any new lists, and should involve only one loop.

Furthermore, this function should not make use of ANY built-in Python functions other than range() and len(). No credit will be awarded for solutions that use the min() function.

We will now test the minimum() function. Create a new code cell to perform the steps below. Create three lists as shown below:

list1 = [9.8, 7.4, 5.6, 4.8, 4.8, 5.3, 4.1, 9.6, 5.4]

list2 = [3.4, 7.6, 8.7, 7.5, 9.8, 7.5, 6.7, 8.7, 8.4]

list3 = ['St. Louis', 'Kansas City', 'Chicago', 'Little Rock', 'Omaha']

Use the minimum() function to calculate the minimum of each of these lists, printing the results

Solutions

Expert Solution

CODE -

def minimum(x):
# Initializing the value of minm(smallest value of the list) with the first element of the list
minm = x[0]
# Iterating over the list starting from the 2nd element
for i in range(1, len(x)):
# Assigning the value of the currently processed element to minm if it is less than minm
if x[i]<minm:
minm = x[i]
# Returning the value of minm(smallest value of the list)
return minm

SCREENSHOT -

If you have any doubt regarding the solution, then do comment.
Do upvote.


Related Solutions

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...
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
Problem Description: Write a method that returns the smallest element in a specified column in a...
Problem Description: Write a method that returns the smallest element in a specified column in a matrix using the following header: public static double columnMin(double[][] m, int columnIndex) Write a program that reads from the keyboard the number of rows and columns, and an array of double floating point numbers with the specified number of rows and columns. The program will then invoke the columnMin method and displays the minimum value of all columns.
Given an array of numbers, find the index of the smallest array element (the pivot), for...
Given an array of numbers, find the index of the smallest array element (the pivot), for which the sums of all elements to the left and to the right are equal. The array may not be reordered. Example arr=[1,2,3,4,6] the sum of the first three elements, 1+2+3=6. The value of the last element is 6. Using zero based indexing, arr[3]=4 is the pivot between the two subarrays. The index of the pivot is 3. Function Description Complete the function balancedSum...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
Considering the sodium (11Na) element, a- Write the basic level electronic configuration and find the minimum...
Considering the sodium (11Na) element, a- Write the basic level electronic configuration and find the minimum energy spectral term. b- 1. Find the minimum energy spectral term by writing the electronic configuration corresponding to the excited state. c- Show the split in energy levels under the spin-orbit interaction. d- Show the split in energy levels under a weak outer magnetic field (Bexterior).
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
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...
I need to find the kth smallest element in the union of 2 sorted arrays in...
I need to find the kth smallest element in the union of 2 sorted arrays in O(log |A| + log |B|) time. I understand this is similar to binary search, but I don't understand which parts of the arrays I can throw out at each level of recursion, and why. In the pseudocode below, what should go into $1 through $16 and why? // A and B are each sorted into ascending order, and 0 <= k < |A|+|B| //...
3. In this problem you are to find the effects of a legal minimum wage on...
3. In this problem you are to find the effects of a legal minimum wage on the labor income of unskilled workers. Assume that the marginal product of labor for unskilled labor is MPN = 100 - 0.2N. The supply of unskilled labor is 80 + 2w, where w is the real wage received by unskilled labor. a. If there is no minimum wage, find the equilibrium values of real wage, employment, and the labor income for unskilled workers. b....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT