HackerRank Python - Regex Substitution
The re.sub() tool (sub stands for substitution) evaluates a pattern and, for each valid match, it calls a method (or lambda).
The method is called for all matches and can be used to modify strings in different ways.
The re.sub() method returns the modified string as an output.
- # Enter your code here. Read input from STDIN. Print output to STDOUT
-
- import re
-
- for _ in range(int(input())):
- print(re.sub(r'(?<= )(&&|\|\|)(?= )', lambda x: 'and' if x.group() == '&&' else 'or', input()))