Now build the -A flag of the cat tool, as a new function. It makes whitespace visible: every tab character (\t) is replaced by the two characters ^I and every line ends with a literal $ marker (so trailing spaces become obvious). Lines are then joined with newlines.
Note: real cat -A also shows other control characters (such as ^M) and non-ASCII bytes; this version only handles tabs and line ends, like cat -T -E. A blank line becomes just $, and an empty list gives an empty string.
Function signature:
def showAllCat(self, lines: List[str]) -> str:
Example:
Input: lines = ["hello\tworld", "foo"] Output: "hello^Iworld$\nfoo$" When printed: hello^Iworld$ foo$
Another example (a blank line, trailing spaces):
Input: lines = ["a\t", "", "x "] Output: "a^I$\n$\nx $" When printed: a^I$ $ x $
Two transformations per line: replace tabs with
"^I", then append "$". After both, join all the transformed lines with "\n".
Python strings have a built-in
replace:
line.replace("\t", "^I") + "$"
Append each transformed line to a list, then "\n".join(out).
The full solution:
class Solution:
def showAllCat(self, lines):
out = []
for line in lines:
out.append(line.replace("\t", "^I") + "$")
return "\n".join(out)
Free plan: 30 tutor messages. Premium is unlimited.
