swap max and min in list

Question:

I need to swap the max and min in a list of distinct numbers. Like so:

Example input

3 4 5 2 1

Example output

3 4 1 2 5

I figured using this would make the most sense:

a = [3, 4, 5, 2, 1]
a[a.index(max(a))], a[a.index(min(a))] = min(a), max(a)

However, it seems to work on some lists of distinct numbers but not others (including the given example). I have no idea why. Can anyone see the bug?

Asked By: Levi Baguley

||

Answers:

You may want to use this function to help you permuting min and max. In the distinct-list case, it’ll work. If you have duplicates, it would also handle them.

def swap_min_max(l):
    max_value = max(l)
    min_value = min(l)

    # get all potential indices
    idx_min = [i for i,e in enumerate(l) if e==min_value]
    idx_max = [i for i,e in enumerate(l) if e==max_value]

    # replace values
    for idx in idx_min:
        l[idx] = max_value
    for idx in idx_max:
        l[idx] = min_value

    return l

>>>> swap_min_max([4, 5, 5, 2, 1, 1])
[4, 1, 1, 2, 5, 5]

>>>> swap_min_max([3, 4, 5, 2, 1])
[3, 4, 1, 2, 5]
Answered By: arnaud

In multiple assignments, Python calculates all of the right hand side parts, left to right, then assigns them one by one to the left hand side parts, left to right (see the language reference for details). So:

a = [3, 4, 5, 2, 1]
a[a.index(max(a))], a[a.index(min(a))] = min(a), max(a)

a = [3, 4, 5, 2, 1]
a[a.index(max(a))], a[a.index(min(a))] = 1, max(a)

a = [3, 4, 5, 2, 1]
a[a.index(max(a))], a[a.index(min(a))] = 1, 5

a = [3, 4, 5, 2, 1]
a[a.index(5)], a[a.index(min(a))] = 1, 5

a = [3, 4, 5, 2, 1]
a[2], a[a.index(min(a))] = 1, 5

a = [3, 4, 1, 2, 1]
a[a.index(min(a))] = 5

a = [3, 4, 1, 2, 1]
a[a.index(1)] = 5

a = [3, 4, 1, 2, 1]
a[2] = 5
# ^ uh oh

a = [3, 4, 5, 2, 1]

If the minimum value is before the maximum value this works fine, because we always replace the first occurrence of the minimum value and that turns out to be the original one.

To fix it, just calculate the indices separately before doing the swap.

Answered By: jonrsharpe

You can use this solution if number are distinct.

def swap(a) :
  max_index=a.index(max(a))
  min_index=a.index(min(a))
  ma=max(a)
  mi=min(a)
  a[max_index]=mi
  a[min_index]=ma
a=[3,4,5,2,1]
swap(a)
print(a)
Answered By: nsr

All you need is index no of max and min which you can find by index method:

data=[3,4, 5, 2, 1]

max_va=data.index(max(data))
min_va=data.index(min(data))

data[max_va],data[min_va]=data[min_va],data[max_va]

print(data)

output:

[3, 4, 1, 2, 5]
Answered By: Aaditya Ura

How about this solution for the exercise?:

It is required to swap the first element of the array with the
maximum.

It passed the tests. Anything wrong & Can you advise smt to optimise like one-line solution?

a = list(map(int, input().split()))
maxx = max(a)
s = a[0]
a[a.index(max(a))] = s
a[0] = maxx
print(*a)
Answered By: HalimHamidov
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.