human
Complete this python program:
def decode_entities(string):
assistant
#Here is the Python code you requested.

    """Decodes HTML entities in the given string ("&lt;" => "<").
    """
    def replace_entity(match):
        (hash, hex, name) = (match.group(1), match.group(2), match.group(3))
        if ((hash == '#') or name.isdigit()):
            if (hex == ''):
                return unichr(int(name))
            if (hex.lower() == 'x'):
                return unichr(int(('0x' + name), 16))
        else:
            cp = htmlentitydefs.name2codepoint.get(name)
            return (unichr(cp) if cp else match.group())
    if isinstance(string, basestring):
        return RE_UNICODE.subn(replace_entity, string)[0]
    return string