Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

📚 The CoCalc Library - books, templates and other resources

Views: 96169
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
15
def is_triple_double(word):
16
"""Tests if a word contains three consecutive double letters.
17
18
word: string
19
20
returns: bool
21
"""
22
i = 0
23
count = 0
24
while i < len(word)-1:
25
if word[i] == word[i+1]:
26
count = count + 1
27
if count == 3:
28
return True
29
i = i + 2
30
else:
31
i = i + 1 - 2*count
32
count = 0
33
return False
34
35
36
def find_triple_double():
37
"""Reads a word list and prints words with triple double letters."""
38
fin = open('words.txt')
39
for line in fin:
40
word = line.strip()
41
if is_triple_double(word):
42
print(word)
43
44
45
print('Here are all the words in the list that have')
46
print('three consecutive double letters.')
47
find_triple_double()
48
print('')
49
50
51
52