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
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...
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...
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....
Using the trend in the periodic table, and position of an element in it, the smallest element is
Using the trend in the periodic table, and position of an element in it, the smallest element is a. hydrogen b. Helium c. Oxygen d. Carbon
I have the following question: Write a recursive function to find the Nth element from the...
I have the following question: Write a recursive function to find the Nth element from the top of a stack. For example, if N is 3 the function should return the third element in the stack. Use the following header: template <class Type> Type getNth( stack<Type> & s, int n) Although your function may push and pop values from the stack, it must eventually leave the stack in its original state. Your function may NOT use a help stack or...
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT