Reverse only letters - sol2

Run Settings
LanguagePython
Language Version
Run Command
def reverse_only_letters(str_input): """ Use two pointers logic to compare the letters and replace them """ output = list(str_input) left = 0 right = len(output)-1 while(left < right): if not output[left].isalpha(): left+=1 elif not output[right].isalpha(): right-=1 else: output[left], output[right] = output[right], output[left] left+=1 right-=1 return "".join(output) print("Hello World!") sample1 = "ab-cd" sample2 = "a-bC-dEf-ghIj" sample3 = "Test1ng-Leet=code-Q" result1 = reverse_only_letters(sample1) print(f"result1: {result1}") assert(result1 == "dc-ba") result2 = reverse_only_letters(sample2) print(f"result2: {result2}") assert(result2 == "j-Ih-gfE-dCba") result3 = reverse_only_letters(sample3) print(f"result3: {result3}") assert(result3 == "Qedo1ct-eeLg=ntse-T") print("Good-bye World")
Editor Settings
Theme
Key bindings
Full width
Lines