Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to print a list when first element is first max and second element is first min and so on in Python

1 Answer

0 votes
def print_first_max_second_min(arr): 
    arr.sort()  
  
    size = len(arr)
    i = 0
    j = size - 1

    while (i < j):  
        print(arr[j], end =" ") 
        j -= 1
        print(arr[i], end =" ") 
        i += 1

    if (size % 2 != 0): 
        print(arr[i])  
  
  
lst = [13, 5, 2, 10, 4, 9, 7, 8, 559]  

print_first_max_second_min(lst)  



'''
run:

559 2 13 4 10 5 9 7 8

'''

 



answered Dec 8, 2021 by avibootz
...