Question

In: Computer Science

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

Solutions

Expert Solution

def stringlist_min_index(s):

    l=1

    i=0

    li=[]

    while(l<len(s)-2):

        j=s.find(',',l,len(s)-1)

        li.append(int(s[l:j].strip()))

        l=j+1

    li.append(int(s[l:-1].strip()))

    

    result = 0

    i = 1

    while i < len(li):

        if li[result] > li[i]:

            result = i

        i += 1

    return result


# Testing

print(stringlist_min_index('[123 ,53 ,1 ,8]'))

print(stringlist_min_index('[1 ,2 ,345 ,0]'))

print(stringlist_min_index('[5]'))

PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT SECTION


Related Solutions

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)
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
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.
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest 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 indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
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...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose...
Write a modification of the recursive binary search algorithm that always returns the smallest index whose element matches the search element. Your algorithm should still guarantee logarithmic runtime. Give a brief discussion of the best- and worst-case runtimes for this new algorithm as they compare to the original. NOTE: You do not have to re-write the entire algorithm. You just need to indicate any changes you would make and show the pseudocode for any portions that are changed. Example: Given...
You will build some methods that search for the smallest element and the index of the...
You will build some methods that search for the smallest element and the index of the smallest element in an array in a recursive fashion. I know you can already do this without recursion. The point is to use recursion, so use it. public class Driver { public static void main(String[] args) { int[] one = {2, 4, 6, 1, 6, 3, 8}; int[] two = {43, 76, 3, 23, 95, 23}; int[] three = {9, 8, 7, 6, 5,...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT