Created
May 22, 2022 18:49
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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