I want to find the index of the minimum index.

Asked 2 years ago, Updated 2 years ago, 20 views

I'm at a loss because I don't understand this problem.

Find the minimum value from the stth to the last element of the array, but find the index, not the minimum value itself.Enter the code in [Write Here] below (returns the minimum number).

defmin_index(a,st):
  minidx=st#Index the leading element as the lowest index
  for i in range(st+1,len(a))# If i is less than minimum
    if Write Here: Index #i to the minimum value
      minidx = i
  return [Write here]

The example I just gave you was a different program, sorry!
There were no examples of how to perform this problem.

python

2022-09-29 22:44

3 Answers

This type of writing is a pattern in which you look at elements from beginning to end, one by one, while updating the record.

If you want to know which of the elements in the list [a,b,c,...,d,e], and if you know which is the smallest in [a,b,c,...,d], you can find the minimum value by comparing it to e only once.You don't have to compare all other elements a,b,c,...,d to e.

In addition, the lowest value in [a,b,c,...,d] can be found by comparing d with d.If you repeat this, you can first find out which one is smaller, a or b, and which one is smaller, c and ….

This is how the program finds the minimum value (index).

First, write and repeat the for statement to see the elements in order from the beginning:

for i in range(st+1,len(a)):

In this part of the next section, we'll see if it's smaller, the "minimum previous" or the "elements you're looking at now."

 if Write Here:

Finally, if the element you are looking at is smaller, remember the index of that element.

minidx=i

After this operation, minidx should have been substituted with the lowest index.So you can return it as it is.

return [write here]

If you summarize the above, you can write a program.Just to be sure, I have placed a fully functioning program in Wandbox, so if you need it, please check it out.


2022-09-29 22:44

I think the question sentence is "the st-to-last element of array a".
Also, the for statement must end with :

defmin_index(a,st):
  minidx=st#Index the leading element as the lowest index
  for i in range(st+1,len(a))—If #i is less than the minimum value,
    if a[i]<a[minidx]—Index #i with minimum value
      minidx = i
  return minidx


2022-09-29 22:44

If it's homework, I'd probably like you to write it differently, but with python, you can get it easily like this:

def get_min_index(items, start_index):
    min_value=min(items[start_index:])
    min_value_index=start_index+items[start_index:].index(min_value)
    return min_value_index

samples = [1,1,3,3,5,1,3,5,9,3]
result_index=get_min_index(samples,6)

print(f'index={result_index}, value={samples[result_index]}')

index=6,value=3


2022-09-29 22:44

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.