Given a list of integers, write a Python program to reverse the order of consecutively incrementing chunk in given list.
Examples:
Input : [0, 1, 9, 8, 7, 5, 3, 14] Output : [9, 1, 0, 8, 7, 5, 14, 3] Explanation: There are two chunks of strictly increasing elements (0, 1, 9) and (3, 14). Input : [-5, -3, 0, 1, 3, 5, -2, -12] Output : [5, 3, 1, 0, -3, -5, -2, -12] Explanation: Only one chunk of strictly increasing elements exists i.e. (-5, -3, 0, 1, 3, 5).
Approach #1 : Naive (Using extended slices) A naive approach to solve the given problem is to use extended slice. We initialize two variables ‘res’ (to store final output) and block( to store chunks of incrementing integers) with empty lists. Now using a for loop, every time we check if the current element is less than the last element of block or not. If yes, add reversed chunk to ‘res’ using extended slicing (block[::-1]) and clean block (block[:] = [i]). otherwise, simply append the element to ‘block’. At last, extend ‘res’ by reversing block and output it.
Python3
# Python3 program to Reverse order # of incrementing integers in list def reverseOrder(lst): res = [] block = [] for i in lst: # check if the current element is less # than the last element of block if block and i < block[ - 1 ]: # add reversed chunk to 'res' res.extend(block[:: - 1 ]) block[:] = [i] else : # append the element to 'block' block.append(i) # extend 'res' by reversing block res.extend(block[:: - 1 ]) return (res) # Driver code lst = [ 0 , 1 , 9 , 8 , 7 , 5 , 3 , 14 ] print (reverseOrder(lst)) |
[9, 1, 0, 8, 7, 5, 14, 3]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n)
Approach #2 : Using list comprehension An efficient approach to the above problem is to use list comprehension. It first finds out all the positions where the incrementing integers begin and stores them in a variable ‘break_’. After this, all the chunks are reversed and are managed in the form of sublists, which are in turn stored in ‘block’. Finally, ‘block’ is unzipped and returned.
Python3
# Python3 program to Reverse order # of incrementing integers in list def reverseOrder(lst): break_ = [ 0 ] + [i for i in range ( 1 , len (lst)) if lst[i - 1 ] > lst[i]] + [ len (lst)] block = [ list ( reversed (lst[i:j])) for i, j in zip (break_[: - 1 ], break_[ 1 :])] return ( sum (block, [])) # Driver code lst = [ 0 , 1 , 9 , 8 , 7 , 5 , 3 , 14 ] print (reverseOrder(lst)) |
[9, 1, 0, 8, 7, 5, 14, 3]
Time complexity: O(n)
Auxiliary space: O(n)