Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added computer_networks/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions computer_networks/char_stuffing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
FLAG = "~"
ESC = "#"


def char_stuffing(string: str) -> str:
"""
Return the char stuffed message
>>> char_stuffing("abc")
'abc'
>>> char_stuffing("a#b#c")
'a##b##c'
"""
arr = []
# Create a list of characters from the string
for character in string:
arr.append(character)
for i in range(len(arr)):
# If we encounter the FLAG and it's not the first or last character
if arr[i] == FLAG and not (i == 0 or i == len(arr) - 1):
arr[i] = ESC + arr[i] # Prepend ESC to FLAG
elif arr[i] == ESC:
arr[i] += ESC # Duplicate ESC
return "".join(arr) # Join the list of characters back into a string


string = "~abc#~cde~ab~"
print(char_stuffing(string))