Skip to content
Closed
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
23 changes: 23 additions & 0 deletions computer_networks/char_stuffing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FLAG = '~'

Check failure on line 1 in computer_networks/char_stuffing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (INP001)

computer_networks/char_stuffing.py:1:1: INP001 File `computer_networks/char_stuffing.py` is part of an implicit namespace package. Add an `__init__.py`.
ESC = '#'


def char_stuffing(s: str) -> str:
"""
Return the char stuffed message
>>> char_stuffing("abc")
"abc"
>>> char_stuffing("a#b#c")
"a##b##c"
"""
arr = [i for i in s]

Check failure on line 13 in computer_networks/char_stuffing.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (C416)

computer_networks/char_stuffing.py:13:11: C416 Unnecessary `list` comprehension (rewrite using `list()`)
for i in range(len(arr)):
if arr[i] == FLAG and not (i == 0 or i == len(arr) - 1):
arr[i] = ESC + arr[i]
elif arr[i] == ESC:
arr[i] += ESC
return ''.join(arr)


s = '~abc#~cde~ab~'
print(char_stuffing(s))
Loading