📚 The CoCalc Library - books, templates and other resources
License: OTHER
"""This module contains a code example related to12Think Python, 2nd Edition3by Allen Downey4http://thinkpython2.com56Copyright 2015 Allen Downey78License: http://creativecommons.org/licenses/by/4.0/9"""1011from __future__ import print_function, division1213from inlist import make_word_list, in_bisect141516def interlock(word_list, word):17"""Checks whether a word contains two interleaved words.1819word_list: list of strings20word: string21"""22evens = word[::2]23odds = word[1::2]24return in_bisect(word_list, evens) and in_bisect(word_list, odds)252627def interlock_general(word_list, word, n=3):28"""Checks whether a word contains n interleaved words.2930word_list: list of strings31word: string32n: number of interleaved words33"""34for i in range(n):35inter = word[i::n]36if not in_bisect(word_list, inter):37return False38return True394041if __name__ == '__main__':42word_list = make_word_list()4344for word in word_list:45if interlock(word_list, word):46print(word, word[::2], word[1::2])4748for word in word_list:49if interlock_general(word_list, word, 3):50print(word, word[0::3], word[1::3], word[2::3])5152535455