Question

In: Computer Science

Write a function argmax(somelist=[]) to return the index of the largest number in a numerical list,...

Write a function argmax(somelist=[]) to return the index of the largest number in a numerical list, or None if the list is empty. If there are ties, then return the index of the first occurrence of the largest number. Sample: argmax([2, 2, 4, 4, 1]) returns 2. in python program

Solutions

Expert Solution


def argmax(x):
if x==[]:
#check whether list is empty. or check len(x)==0
return None   #if empty,"None" is returned
largest=x[0] #initally, set first element as largest
for i in x: # search for largest element
if(largest<i): # if the current element i >largest, then set it as the largest
largest=i
position =x.index(largest)
#find the index of largest element
return position

n=int(input("enter the lnumber of elements"))
x=[ ]
for i in range(0,n):
x.append(int(input("enter the element"))) #append each input element to the list x
print("The input list is",x)
print("index of largest element is::",argmax(x))

Note:: The index() method returns the position at the first occurrence of the specified value.

Without using index, the function can be written as

def argmax(x):
if x==[]:
#check whether list is empty. or check len(x)==0
return None #if empty,"None" is returned
largest=x[0] #initally, set first element as largest
position=0
for i in range(0,len(x)-1):
# search for largest element , i varies from 0 to length(x)-1
if(largest<x[i]):
largest=x[i]
position=i
#position =x.index(largest)
return position


Related Solutions

PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list. (please do on racket language)
Write a Racket function that will take a list of numbers as a parameter and return...
Write a Racket function that will take a list of numbers as a parameter and return true if they all are positive, and false otherwise. You are NOT required to check the type of elements in the list.
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
write a function that return a list of row numbers in matrix with removing the wrong...
write a function that return a list of row numbers in matrix with removing the wrong value. Ex: remove_row( matrix, wro) if matrix = [[5,2,8],[6,7,20],[10,25,9]] wro= 20 then output will be [1,2] Do not use any built in functions.
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of...
3. Write a function named "countNonAlpha" that accepts a string. It will return the number of non-alphabet characters (excluding blanks) in the string. For example, if the string is "Hello, World!", it will return 2 for ',' and '!" in the string. 4. Write a function named "deleteZeros" that takes two arguments: a. an array of integer values; b. an integer for the number of elements in the array; The function will return the number of zeros that it has...
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.
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
write a member function in C++ , that takes two lists and return list that contain...
write a member function in C++ , that takes two lists and return list that contain the merge of the two lists in the returned list: first insert the first list and then the second list  
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT