Q
Q
qarwest2020-07-02 16:20:24
Python
qarwest, 2020-07-02 16:20:24

The search functions for the maximum and minimum element output the same index. Why?

def f_max(mas):
    i = 0
    max_ar = mas[0]
    for i in range(len(mas)):
        if mas[i] > max_ar:
            max_ar = mas[i]
    return max_ar

def f_maxIndex(mas):
    i = 0
    max_ar = mas[0]
    for i in range(len(mas)):
        if mas[i] < max_ar:
            max_ar = mas[i]
    return i

def f_min(mas):
    i = 0
    min_ar = mas[0]
    for i in range(len(mas)):
        if mas[i] < min_ar:
            min_ar = mas[i]
    return min_ar

def f_minIndex(mas):
    i = 0
    min_ar = mas[0]
    for i in range(len(mas)):
        if mas[i] < min_ar:
            min_ar = mas[i]
    return i

count = input().split()
array = [int(x) for x in count]

print('Максимальный элемент массива=', f_max(array), ',а его индекс=', f_maxIndex(array))
print('Минимальный элемент массива=', f_min(array), ',а его индекс=', f_minIndex(array))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-07-02
@qarwest

Because you don't change these functions in any way i. She ran the whole cycle and came back.

for i in range(len(mas)):
        if mas[i] < max_ar:
            max_ar = mas[i]
    return i

def f_maxIndex(mas):
    max_index = 0
    max_ar = mas[0]
    for i in range(len(mas)):
        if mas[i] > max_ar:
            max_ar = mas[i]
            max_index = i
    return max_index


def f_minIndex(mas):
    min_index = 0
    min_ar = mas[0]
    for i in range(len(mas)):
        if mas[i] < min_ar:
            min_ar = mas[i]
            min_index = i
    return min_index

In general, of course, an unusual code. Are you learning how I get it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question