#coding=utf-8

class FragmentBox():
    def __init__(self, fragment):
        self.fragments = [fragment,]

    def getFragments(self):
        return self.fragments

    def cut(self, fragment):
        new_fragments = []
        for item in self.fragments:
            new_fragments.extend(item.cut(fragment))
        self.fragments = new_fragments

class Fragment():
    def __init__(self, start, end, prefix, length):
        self.start = int(start)
        self.end = int(end)
        self.prefix = prefix
        self.length = length

    @property
    def start_code(self):
        num = str(self.start)
        num = "0" * (self.length - len(num)) + num
        return self.prefix + num

    @property
    def end_code(self):
        num = str(self.end)
        num = "0" * (self.length - len(num)) + num
        return self.prefix + num

    def cut(self, fragment):
        if self.start == self.end:
            if fragment.end == self.start or fragment.start == self.start:
                return []
            if fragment.start < self.start and fragment.end > self.end:
                return []
            return [self,]

        if fragment.start <= self.start and fragment.end >= self.end:
            return []
        if fragment.start > self.start and fragment.end < self.end:
            start_fragment = Fragment(self.start, fragment.start - 1, self.prefix, self.length)
            end_fragment = Fragment(fragment.end + 1, self.end, self.prefix, self.length)
            return [start_fragment, end_fragment]

        if fragment.end >= self.start and fragment.start <= self.start:
            new_fragment = Fragment(fragment.end + 1, self.end, self.prefix, self.length)
            return [new_fragment, ]
        if fragment.start <= self.end and fragment.end >= self.end:
            new_fragment = Fragment(self.start, fragment.start - 1, self.prefix, self.length)
            return [new_fragment, ]

        return [self,]





