The following is my solution to Java vs C++. I think the way I have used the re library is inefficient, and possible erroneous as I am getting tle.
import sys
import re
cpp = re.compile("^[a-z]([a-z_]*[a-z])*$")
java= re.compile("^[a-z][a-zA-Z]*$")
r = re.compile("[A-Z][a-z]*")
lines = [line for line in sys.stdin.read().splitlines() if line!=""]
for line in lines:
if cpp.match(line) :
line = line.split("_")
for i in range(1,len(line)):
line[i] = line[i].capitalize()
print "".join(line)
elif java.match(line):
namelist = r.findall(line)
for name in namelist:
line = line.replace(name , name.replace(name[0],"_"+name[0].lower()))
print line
else : print "Error!"
For instance, is there a better way to replace string components inline instead of creating a new string and copying the way I have used :
line = line.replace(name , name.replace(name[0],"_"+name[0].lower()))
or is there a way that is entirely different from my approach?