0

Python program to print new list by sorted number in given list is like [1st largest,1st smallest, 2nd largest, 2nd smallest]

Example Input: [3,7,8,2,5,1] Output: [8,1,7,2,5,3]

7th Jan 2022, 2:29 PM
Suyog Kapdekar
Suyog Kapdekar - avatar
4 Respuestas
+ 1
tmp_list=input_list.sort() output=[] for i in range(len(tmp_list)): j=len(tmp_list)-i-1 if (j>i): output.append(tmp_list[j]) output.append(tmp_list[i]) elif (j=i): output.append(tmp_list[i]) else: pass print(output) I hope this help. It’s not probably the best solution but it should work.
7th Jan 2022, 2:54 PM
Mattia Nardoni
+ 1
What if the list has odd number of items? what should be done with the orphaned item?
7th Jan 2022, 3:15 PM
Ipang
0
Show us your attempt and we will help you!
7th Jan 2022, 2:55 PM
Devnull
Devnull - avatar
0
I think that it could be added at the end-> as the 4 in the ex below. Ex. Input= [1,2,3,4,5,6,7] Output=[7,1,6,2,5,3,4]
7th Jan 2022, 3:17 PM
Mattia Nardoni