def i_optional_att_group_cardinality(ss: Substrate, max_: unlimitedNat, qore: Quads_or_Error) -> Set(sctIdGroup):
    if quad_direction(qore) == source_direction:
        return Set(sctIdGroup)(minus([sctIdGroup(rel.s, quadGroup(rel)) for rel in ss.relationships],
                                     union([sctIdGroup(q.s, quadGroup(q)) for q in quads_for(qore).v],
                                           i_required_att_group_cardinality(N(0), max_, qore))))
    else:
        return Set(sctIdGroup)(
            minus([sctIdGroup(rel.t.t_sctid, quadGroup(rel)) for rel in ss.relationships if rel.t.inran('t_sctid')],
                  union([sctIdGroup(q.t.t_sctid, quadGroup(q)) for q in quads_for(qore).v],
                        i_required_att_group_cardinality(N(0), max_, qore))))
def i_required_att_group_cardinality(min_: N, max_: unlimitedNat, qore: Quads_or_Error) -> Set(sctIdGroup):
    if quad_direction(qore) == source_direction:
        nzg = set([q for q in quads_for(qore).v if q.g != zero_group])
        zg = set([q for q in quads_for(qore).v if q.g == zero_group])
        nzg_pass = [q_nz for q_nz in nzg
                    if evalCardinality(min_, max_, [q for q in quads_for(qore).v if q.s == q_nz.s and q.g == q_nz.g])]
        zg_pass = [q_zg for q_zg in zg if evalCardinality(min_, max_, set(q_zg))]
        return Set(sctIdGroup)([sctIdGroup(q.s, quadGroup(q)) for q in (nzg_pass + zg_pass)])
    else:
        nzg = set([q for q in quads_for(qore).v if q.g != zero_group and q.t.inran('t_sctid')])
        zg = set([(q.t, q) for q in quads_for(qore).v if q.g == zero_group and q.t.inran('t_sctid')])
        nzg_pass = [q_nz for q_nz in nzg
                    if evalCardinality(min_, max_, [q for q in quads_for(qore).v if q.t == q_nz.t and q.g == q_nz.g])]
        zg_pass = [q_zg for q_zg in zg if evalCardinality(min_, max_, set(q_zg))]
        return Set(sctIdGroup(nzg_pass + zg_pass))