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.

  1. # Enter your code here. Read input from STDIN. Print output to STDOUT
  2. import re
  3. for _ in range(int(input())):
  4. print(re.sub(r'(?<= )(&&|\|\|)(?= )', lambda x: 'and' if x.group() == '&&' else 'or', input()))

download android app