Skip to content

My new branch #11651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
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))