Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Increment 1’s in list based on pattern

Python | Increment 1’s in list based on pattern

Given a list of binary numbers 0 and 1, Write a Python program to transform the list in such a way that whenever 1 appears after the occurrence of a sequence of 0’s, increment it by n+1, where ‘n’ is the last increment. 

Examples:

Input : [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1]
Output : [0, 1, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3]

Input : [1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1]
Output : [1, 0, 2, 0, 0, 0, 3, 3, 0, 0, 4, 4, 4, 0, 5, 0, 6]

  Approach #1 : Naive Approach This is a naive approach to the given problem. It uses two variable ‘previous’ and ‘grp’ to store previously incremented number and to store the number of 1’s in a group. Now, using a for loop, increment 1’s accordingly. 

Python3




# Python3 program to increment 1's in
# list based on pattern
 
def transform(lst):
     
    previous = 0
    grp = 0
    for elem in lst:
        if elem and not previous:
             grp += 1
        previous = elem
        yield (grp if elem else 0)
     
# Driver code
lst = [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1]
x = (transform(lst))
res = []
for i in range(0, len(lst)):
    res.append(next(x))
print(res)


Output:

[0, 1, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3]

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Approach #2 : Using count, chain and groupby from itertools module. This is an efficient and more pythonic approach towards the given problem where we use count, chain and groupby from itertools module. 

Python3




# Python3 program to increment 1's in
# list based on pattern
from itertools import *
 
def transform(lst):
     
    c = count(1)
    return list(chain(*[list(g) if k != 1 else [next(c)]*len(list(g))
    for k, g in groupby(lst)]))
     
# Driver code
lst = [0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1]
print(transform(lst))


Output:

[0, 1, 0, 0, 2, 2, 0, 0, 0, 3, 3, 3]

Time Complexity: O(n*n), where n is the number of elements in the list 
Auxiliary Space: O(n), where n is the number of elements in the list 

RELATED ARTICLES

Most Popular

Recent Comments