We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Regex Substitution
Regex Substitution
+ 0 comments import re for _ in range(int(input())): print(re.sub(r"(?<=\s)([&|])\1(?=\s)", lambda match: "and" if match.group(0) == "&&" else "or", input()))
+ 0 comments import re n = int(input()) pattern1 =r'(?<=\s)&&(?=\s)' pattern2 = r'(?<=\s)\|\|(?=\s)' repl1 = 'and' repl2 = 'or' for _ in range(n): string = input() print(re.sub(pattern1, repl1, re.sub(pattern2, repl2, string)))
+ 0 comments All test cases passed!!!
import re def replace_symbols(line): pattern_1=r'(?<= )&&(?= )' pattern_2=r'(?<= )||(?= )' substitute_1="and" substitute_2="or" line=re.sub(pattern_1,substitute_1,line) line=re.sub(pattern_2,substitute_2,line) return line N=int(input()) lines=[input() for _ in range(N)] for line in lines: modified_line=replace_symbols(line) print(modified_line)
+ 0 comments Python:
import re N = int(input()) content = "" for _ in range(N): s = input() + '\n' new = re.sub(r" && ", " and ", s) new = re.sub(r" \|\| ", " or ", new) while new != s: s = new new = re.sub(" && ", " and ", s) re.sub(r" \|\| ", " or ", new) content += s print(content)
+ 0 comments import re
for i in range(int(input())): code = input() while re.search(" && ",code) or re.search(" || ", code): newcode = re.sub(" && ", " and ", code) code = re.sub(" || ", " or ", newcode) print(code)
Load more conversations
Sort 262 Discussions, By:
Please Login in order to post a comment