coding utf8 import re result def gramma string re compile 0-9a-z A-Z 0

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# -*- coding:utf8 -*-
import re
result = []
def gramma(string):
r = re.compile(r"([0-9a-z]+)([A-Z]+)([0-9a-z]+)$")
match = r.search(string)
for m in match.groups():
if m[0].isdigit() or m[0].islower():
result.append("<{0}, {1}>".format(0, m))
else:
for ch in m:
result.append("<{0}, {1}>".format(1, ch))
result.append("\n")
if __name__ == '__main__':
with open("input.txt", "r") as f:
for line in f:
leftPart, rightPart = line.split("::=")
gramma(leftPart)
gramma(rightPart[0:len(rightPart) - 1])
f.close()
gramma(leftPart)
gramma(rightPart)
for i in result:
print i