This program will calculate the stuffed bits and destuff them as well. For the theoretical part please review your computer network subject book.
'''
bit_stuffing.py
by Vikas Patel
@ www.villageprogrammer.tech
'''
# stuffing the stuffed signal
def stuff(sig):
onec = 0 # one counter
c = 0 # index counter
one = [] # one indexes
s = list(sig)
for i in s:
c += 1
if i == '0':
onec = 0
else:
onec += 1
if onec == 5:
one.append(c)
onec = 0
k = 0 # count extra index number
for i in one:
# print(i)
s.insert(i + k, '0')
k += 1
return s
# destuffing the stuffed signal
def destuff(sig):
onec = 0 # one counter
c = 0 # index counter
one = [] # one indexes
sig = list(sig)
for i in sig:
c += 1
if i == '0':
onec = 0
else:
onec += 1
if onec == 5:
one.append(c)
onec = 0
k = 0 # count extra index number
for i in one:
# print(i)
sig.pop(i + k)
k -= 1
return sig
# ******************** Driver Code ************************* #
sig = input("Enter the signal: ")
print("Original Signal : ", sig)
stf = stuff(sig)
print("Stuffed Signal : ", end="")
print("".join([x for x in stf]))
dstf = destuff(stf)
print("Destuffed Signal : ", end="")
print("".join([x for x in dstf]))
Output
Keep Learning, Keep Practicing. :) :)
That’s All. 😅
Thanks for reading. 🙏
If you liked it feel free to share with your dear ones.💗
And tell me what do you think in the comment box.💬
Stay tuned with Village Programmer.
Post a Comment