FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ThinkRubyBuild/code/interlock.py at master · learnbyexample/ThinkRubyBuild · GitHub
learnbyexample
/
ThinkRubyBuild
Public
Notifications
You must be signed in to change notification settings
Fork
2
Star
32
Code
Issues
7
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
ThinkRubyBuild
/
code
/
interlock.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
53 lines (36 loc) · 1.21 KB
Breadcrumbs
ThinkRubyBuild
/
code
/
interlock.py
Copy path
File metadata and controls
53 lines (36 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""This module contains a code example related to
Think Python, 2nd Edition
by Allen Downey
http://thinkpython2.com
Copyright 2015 Allen Downey
License: http://creativecommons.org/licenses/by/4.0/
"""
from
__future__
import
print_function
,
division
from
inlist
import
make_word_list
,
in_bisect
def
interlock
(
word_list
,
word
):
"""Checks whether a word contains two interleaved words.
word_list: list of strings
word: string
"""
evens
=
word
[::
2
]
odds
=
word
[
1
::
2
]
return
in_bisect
(
word_list
,
evens
)
and
in_bisect
(
word_list
,
odds
)
def
interlock_general
(
word_list
,
word
,
n
=
3
):
"""Checks whether a word contains n interleaved words.
word_list: list of strings
word: string
n: number of interleaved words
"""
for
i
in
range
(
n
):
inter
=
word
[
i
::
n
]
if
not
in_bisect
(
word_list
,
inter
):
return
False
return
True
if
__name__
==
'__main__'
:
word_list
=
make_word_list
()
for
word
in
word_list
:
if
interlock
(
word_list
,
word
):
print
(
word
,
word
[::
2
],
word
[
1
::
2
])
for
word
in
word_list
:
if
interlock_general
(
word_list
,
word
,
3
):
print
(
word
,
word
[
0
::
3
],
word
[
1
::
3
],
word
[
2
::
3
])
Back
|
FazBrowse Home
|
New Git URL