Skip to content

Instantly share code, notes, and snippets.

@ZakriaJanjua
Created May 22, 2022 18:49
Show Gist options
  • Save ZakriaJanjua/481d6c025c44967bad483e6b8239e383 to your computer and use it in GitHub Desktop.
Save ZakriaJanjua/481d6c025c44967bad483e6b8239e383 to your computer and use it in GitHub Desktop.
Given a string as input. When `M` is hit program would copy the preceding character at the end of the string and then remove itself and when `N` is hit program will remove the next character of the string and remove itself too
def MandN(string):
result = ""
string_iter = iter(string)
for i in string_iter:
if (i == "M"):
if (len(result) != 0):
result += result[-1]
elif (i == "N"):
try:
next(string_iter)
except:
continue
else:
result += i
return result
print(MandN("aNxMrgMM"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment