Array obtained by repeatedly reversing array after every insertion from given array | Python3 program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code
from collections import deque NEW_LINE def generateArray ( arr , n ) : NEW_LINE INDENT ans = deque ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i & 1 != 0 ) : NEW_LINE INDENT ans . appendleft ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT if ( n & 1 != 0 ) : NEW_LINE INDENT ans . reverse ( ) NEW_LINE DEDENT for x in ans : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT n = 4 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE generateArray ( arr , n ) NEW_LINE