Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96168
License: OTHER
1
"""This module contains a code example related to
2
3
Think Python, 2nd Edition
4
by Allen Downey
5
http://thinkpython2.com
6
7
Copyright 2015 Allen Downey
8
9
License: http://creativecommons.org/licenses/by/4.0/
10
"""
11
12
from __future__ import print_function, division
13
14
from inlist import make_word_list, in_bisect
15
16
17
def interlock(word_list, word):
18
"""Checks whether a word contains two interleaved words.
19
20
word_list: list of strings
21
word: string
22
"""
23
evens = word[::2]
24
odds = word[1::2]
25
return in_bisect(word_list, evens) and in_bisect(word_list, odds)
26
27
28
def interlock_general(word_list, word, n=3):
29
"""Checks whether a word contains n interleaved words.
30
31
word_list: list of strings
32
word: string
33
n: number of interleaved words
34
"""
35
for i in range(n):
36
inter = word[i::n]
37
if not in_bisect(word_list, inter):
38
return False
39
return True
40
41
42
if __name__ == '__main__':
43
word_list = make_word_list()
44
45
for word in word_list:
46
if interlock(word_list, word):
47
print(word, word[::2], word[1::2])
48
49
for word in word_list:
50
if interlock_general(word_list, word, 3):
51
print(word, word[0::3], word[1::3], word[2::3])
52
53
54
55