def result(seq): """Returns copy of seq shuffled between specified items.""" #figure out the appropriate constructor, if not supplied if c is None: if isinstance(seq, str): constructor = lambda x: seq.__class__(''.join(x)) else: constructor = seq.__class__ else: constructor = c #figure out where to cut the sequence cut_sites = find_many(seq, items) new_seq = list(seq) shuffle_except_indices(new_seq, cut_sites) return constructor(new_seq)
def result(seq): """Returns copy of seq shuffled between specified items.""" #figure out the appropriate constructor, if not supplied if c is None: if isinstance(seq, str): constructor = lambda x: seq.__class__(''.join(x)) else: constructor = seq.__class__ else: constructor = c #figure out where to cut the sequence cut_sites = find_many(seq, items) #want to shuffle sequence before first and after last match as well if (not cut_sites) or cut_sites[0] != 0: cut_sites.insert(0, 0) seq_length = len(seq) if cut_sites[-1] != seq_length: cut_sites.append(seq_length) #shuffle each pair of (i, i+1) matches, excluding position of match curr_seq = list(seq) for start, end in zip(cut_sites, cut_sites[1:]): shuffle_range(curr_seq, start+1, end) #remember to exclude start #return the shuffled copy return constructor(curr_seq)
def result(seq): """Returns copy of seq shuffled between specified items.""" #figure out the appropriate constructor, if not supplied if c is None: if isinstance(seq, str): constructor = lambda x: seq.__class__(''.join(x)) else: constructor = seq.__class__ else: constructor = c #figure out where to cut the sequence cut_sites = find_many(seq, items) #want to shuffle sequence before first and after last match as well if (not cut_sites) or cut_sites[0] != 0: cut_sites.insert(0, 0) seq_length = len(seq) if cut_sites[-1] != seq_length: cut_sites.append(seq_length) #shuffle each pair of (i, i+1) matches, excluding position of match curr_seq = list(seq) for start, end in zip(cut_sites, cut_sites[1:]): shuffle_range(curr_seq, start + 1, end) #remember to exclude start #return the shuffled copy return constructor(curr_seq)