<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def merge_bed_by_name(bt):
 """ Merge intervals in a bed file when the intervals have the same name. Intervals with the same name must be adjacent in the bed file. """

    name_lines = dict()
    for r in bt:
        name = r.name
        name_lines[name] = name_lines.get(name, []) + [[r.chrom, r.start, 
                                                         r.end, r.name,
                                                         r.strand]]
    new_lines = []
    for name in name_lines.keys():
        new_lines += _merge_interval_list(name_lines[name])
    new_lines = ['\t'.join(map(str, x)) for x in new_lines]
    return pbt.BedTool('\n'.join(new_lines) + '\n', from_string=True)