Ejemplo n.º 1
0
def palin_partition(s):
    global memo_palin
    n = len(s)
    if n == 0: yield []
    elif n == 1: yield [s]
    else:
        s = list(s)
        for i in range(n):
            first = s[:i + 1]
            if first in memo_palin:
                # Use memotization to save time
                is_palin = memo_palin[first]
            else:
                is_palin = valid_palin(first)
                memo_pali[first] = is_palin
            if is_palin:
                for p in pali_partition(s[i + 1:]):
                    yield [first_sub] + p
Ejemplo n.º 2
0
def palin_partition(s):
    global memo_palin
    n = len(s)
    if n == 0: yield []
    elif n == 1: yield [s]
    else:
        s = list(s)
        for i in range(n):
            first = s[:i+1]
            if first in memo_palin:
                # Use memotization to save time
                is_palin = memo_palin[first]
            else:
                is_palin = valid_palin(first)
                memo_pali[first] = is_palin
            if is_palin:
                for p in pali_partition(s[i+1:]):
                    yield [first_sub] + p
Ejemplo n.º 3
0
def palin_partition_min_cut(s):
    global memo_palin
    n = len(s)
    if n == 0: yield []
    elif n == 1: yield [s[0]]
    else:
        for i in range(n):
            first = s[:i + 1]
            if first in memo_palin:
                # Use memotization to save time
                is_palin = memo_palin[first]
            else:
                is_palin = valid_palin(first)
                memo_palin[first] = is_palin
            if is_palin:
                min_cut = n - i
                min_p = None
                for p in palin_partition_min_cut(s[i + 1:]):
                    if len(p) < min_cut:
                        min_cut = len(p)
                        min_p = p
                yield [first] + min_p
Ejemplo n.º 4
0
def palin_partition_min_cut(s):
    global memo_palin
    n = len(s)
    if n == 0: yield []
    elif n == 1: yield [s[0]]
    else:
        for i in range(n):
            first = s[:i+1]
            if first in memo_palin:
                # Use memotization to save time
                is_palin = memo_palin[first]
            else:
                is_palin = valid_palin(first)
                memo_palin[first] = is_palin
            if is_palin:
                min_cut = n-i
                min_p = None
                for p in palin_partition_min_cut(s[i+1:]):
                    if len(p) < min_cut:
                        min_cut = len(p)
                        min_p = p
                yield [first] + min_p