<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def expand_elements(compact_el, as_str=False):
 """ Create a list of integers given a string or list of compacted elements This is partly the opposite of compact_elements, but is more flexible. compact_el can be a list or a string. If compact_el is a list, each element is processed individually as a string (meaning list elements can contain commas, ranges, etc) If compact_el is a string, it is split by commas and then each section is processed. In all cases, element symbols (case insensitive) and Z numbers (as integers or strings) can be used interchangeably. Ranges are also allowed in both lists and strings. Some examples: "H-Li,C-O,Ne" will return [1, 2, 3, 6, 7, 8, 10] "H-N,8,Na-12" will return [1, 2, 3, 4, 5, 6, 7, 8, 11, 12] ['C', 'Al-15,S', 17, '18'] will return [6, 13, 14, 15, 16, 17, 18] If as_str is True, the list will contain strings of the integers (ie, the first example above will return ['1', '2', '3', '6', '7', '8', '10'] """


    # If an integer, just return it
    if isinstance(compact_el, int):
        if as_str is True:
            return [str(compact_el)]
        else:
            return [compact_el]

    # If compact_el is a list, make it a comma-separated string
    if isinstance(compact_el, list):
        compact_el = [str(x) for x in compact_el]
        compact_el = [x for x in compact_el if len(x) > 0]
        compact_el = ','.join(compact_el)

    # Find multiple - or ,
    # Also replace all whitespace with spaces
    compact_el = re.sub(r',+', ',', compact_el)
    compact_el = re.sub(r'-+', '-', compact_el)
    compact_el = re.sub(r'\s+', '', compact_el)

    # Find starting with or ending with comma and strip them
    compact_el = compact_el.strip(',')

    # Check if I was passed an empty string or list
    if len(compact_el) == 0:
        return []

    # Find some erroneous patterns
    # -, and ,-
    if '-,' in compact_el:
        raise RuntimeError("Malformed element string")
    if ',-' in compact_el:
        raise RuntimeError("Malformed element string")

    # Strings ends or begins with -
    if compact_el.startswith('-') or compact_el.endswith('-'):
        raise RuntimeError("Malformed element string")

    # x-y-z
    if re.search(r'\w+-\w+-\w+', compact_el):
        raise RuntimeError("Malformed element string")

    # Split on commas
    tmp_list = compact_el.split(',')

    # Now go over each one and replace elements with ints
    el_list = []
    for el in tmp_list:
        if not '-' in el:
            el_list.append(_Z_from_str(el))
        else:
            begin, end = el.split('-')
            begin = _Z_from_str(begin)
            end = _Z_from_str(end)
            el_list.extend(list(range(begin, end + 1)))

    if as_str is True:
        return [str(x) for x in el_list]
    else:
        return el_list