Paste and run code here:
https://www.onlinegdb.com/online_python_interpreter[
^]
I would not call it exactly hard.
lines = [ "test -> expr",
"expr -> ambig1 ambig2 ambig3",
"expr -> ambig1 ambig2 ambig4",
"expr -> ambig1 ambig2",
"expr2 -> ambig2 ambig3",
"expr2 -> ambig2"]
def get_matcher(s):
return s[:s.find("->")+2]
def group_data(lines):
groups = []
new_group = []
matcher = get_matcher(lines[0])
for line in lines:
if line.startswith(matcher):
new_group.append(line)
else:
groups.append(new_group)
new_group = [line]
matcher = get_matcher(line)
groups.append(new_group)
return groups
print(group_data(lines))