April 30 2020
Hello,
I hope you and your loved ones are doing well during these tough times
which is affecting us all globally.
I am independently learning python language and I seemed to have come
across a bug.
The following is my code:
# SWAPPING ELEMENTS
i = [10, 3, 8, 7, 4, 12, 6]
i [ 0 ] , i [ i.index ( max ( i )) ] = i [ i.index ( max ( i ))] , i [ 0 ]
print( i )
i [ -1 ] , i [ i.index ( min ( i ))] = i [ i.index ( min ( i ))] , i [ -1 ]
print( i )
# Lines 4 & 5 don't result in a swap even though they're executed.
# The same code is written on line 6 & 7 and it gives perfect results
# please solve this issue and provide explanation as to why
# this occurs.
I the above code I intend to swap the largest element in the given list
with the first element in the list and similarly the smallest magnitude
element with the last element in the given list.
Expected result is that the above code will swap on max(i) with i[0] but
this does not occur. Surprisingly min(i) and i[-1] works absolutely fine.
I am aware that there are ways around it but this seemed intuitive to me
and was not resulting in the desired solution, so I decided to mention it
to you folks.
Stay safe and healthy,
Regards,
Sami