def to_kdmers(s: str, k: int, d: int) -> List[str]: """ >>> to_kdmers("TAATGCCATGGGATGTT", 3, 2) ['AAT|CAT', 'ATG|ATG', 'ATG|ATG', 'CAT|GAT', 'CCA|GGA', 'GCC|GGG', 'GGG|GTT', 'TAA|CCA', 'TGC|TGG', 'TGG|TGT'] """ intervals1 = map(lambda cs: "".join(cs), windowed(s[:-(k + d)], k)) intervals2 = map(lambda cs: "".join(cs), windowed(s[k + d:], k)) return sorted(["|".join(pair) for pair in zip(intervals1, intervals2)])
def find_contiguous_set_that_sums_to(desired_sum: int, numbers: List[int]) -> List[int]: combination_length = 2 combos = windowed(numbers, combination_length) correct_combo = next( (combo for combo in combos if sum(combo) == desired_sum), None) while correct_combo is None: combination_length += 1 combos = windowed(numbers, combination_length) correct_combo = next( (combo for combo in combos if sum(combo) == desired_sum), None) return correct_combo
def find_missing_seat(seat_ids: List[int]) -> int: sorted_seats: List[int] = sorted(seat_ids) zipped_lists = windowed(sorted_seats, 2) missing_seats = [get_missing_seat(t[0], t[1]) for t in zipped_lists] return next(filter(lambda x: x is not None, missing_seats))
def peptide_encoding2(dna: Dna, peptide: Peptide) -> List[Dna]: """ >>> dna = "ATGGCCATGGCCCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA" >>> peptide_encoding2(dna, "MA") ['ATGGCC', 'GGCCAT', 'ATGGCC'] """ candidates = frozenset(_dna_candidates(peptide)) candidates |= frozenset(rc(s) for s in candidates) nuc_size = len(peptide) * 3 assert len(next(iter(candidates))) == nuc_size iterator = ("".join(tup) for tup in windowed(dna, nuc_size)) res = [s for s in iterator if s in candidates] return res
def peptide_encoding(dna: str, peptide: str) -> List[str]: """ >>> dna = "ATGGCCATGGCCCCCAGAACTGAGATCAATAGTACCCGTATTAACGGGTGA" >>> peptide_encoding(dna, "MA") ['ATGGCC', 'GGCCAT', 'ATGGCC'] """ nuc_size = len(peptide) * 3 iterator = ("".join(tup) for tup in windowed(dna, nuc_size)) res = [ s for s in iterator if peptide in (translate(transcribe(s)), translate(transcribe(rc(s)))) ] return res
def to_debruijn(text: str, k: int) -> AdjacencyList: """ >>> computed = to_debruijn("AAGATTCTCTAAGA", 4) >>> expected = {"AAG":["AGA","AGA"], "AGA":["GAT"], "ATT":["TTC"], "CTA":["TAA"], "CTC":["TCT"], "GAT":["ATT"], "TAA":["AAG"], "TCT":["CTA", "CTC"], "TTC":["TCT"]} >>> computed == expected True """ d = collections.defaultdict(list) w1, w2 = it.tee(map(lambda cs: "".join(cs), windowed(text, k - 1))) next(w2) for this, that in zip(w1, w2): d[this].append(that) return {k: sorted(d[k]) for k in sorted(d.keys())}
def calculate_for_n(n: int) -> int: number_strs = [str(i) for i in range(1, n+1)] illegal_parts: List[str] = [''.join(seq) for seq in windowed(number_strs, 2)] all_permutations = [''.join(perm) for perm in permutations(number_strs, n)] return count_where(lambda permutation: is_allowed(permutation, illegal_parts), all_permutations)