Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
829 B
1
Indexable
Never
#Experiment 2
from array import *

my_array = array('i', [0, 1, 1, 1, 0,1, 1, 1, 0])

def bitstuff(array):
    i = 0
    count = 0
    
    while i < len(array):
        if array[i] == 1:
            count += 1
        else:
            count = 0
        
        if count == 5:
            array.insert(i + 1, 0)  
        i += 1


def destuff(array):
    i = 0
    count = 0
    
    while i < len(array):
        if array[i] == 1:
            count += 1
        else:
            count = 0
        
        if count == 5:
            if i + 1 < len(array) and array[i + 1] == 0:
                array.pop(i + 1)  
            count = 0
        i += 1


bitstuff(my_array)
print("After bit-stuffing:", my_array.tolist())
destuff(my_array)
print("After de-stuffing:", my_array.tolist())
Leave a Comment