Build a tiny version of the Unix tr command. Given a list of lines and two single characters fromChar and toChar, replace every occurrence of fromChar with toChar in each line, then return the lines joined by newline characters (\n).
Notes: fromChar and toChar are single ASCII characters, taken literally (no escapes like \, no ranges). Upper and lower case are different characters. A line may be empty, and may not contain fromChar at all.
Function signature:
def tr(self, lines: List[str], fromChar: str, toChar: str) -> str:
Example:
Input: lines = ["banana", "apple"], fromChar = "a", toChar = "X" Output: "bXnXnX\nXpple" When printed: bXnXnX Xpple
Use Python's built-in string method
replace(old, new) on each line, then join with "\n".
The full solution:
class Solution:
def tr(self, lines, fromChar, toChar):
result = []
for line in lines:
result.append(line.replace(fromChar, toChar))
return "\n".join(result)
Free plan: 30 tutor messages. Premium is unlimited.
