from functools import reduce
import operator


def add_strings(x, z=0):
    """
    This was in sage.misc.misc but commented out. Needed to add
    lists of strings in the word_problem method below.

    Return the sum of the elements of x.  If x is empty,
    return z.

    INPUT:

    - ``x`` -- iterable

    - ``z`` -- the ``0`` that will be returned if ``x`` is empty.

    OUTPUT:

    The sum of the elements of ``x``.

    EXAMPLES::

        sage: from sage.groups.abelian_gps.dual_abelian_group_element import add_strings
        sage: add_strings([], z='empty')
        'empty'
        sage: add_strings(['a', 'b', 'c'])
        'abc'
    """
    if len(x) == 0:
        return z
    if not isinstance(x, list):
        m = iter(x)
        y = next(m)
        return reduce(operator.add, m, y)
    else:
        return reduce(operator.add, x[1:], x[0])