開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 9.(Case Study Word Play)のExercises 9-7(No. 2052)を取り組んでみる。
Exercises 9-7(No. 2052)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def is_three(word):
i = 0
n = 0
while i < len(word) - 1:
if word[i] == word[i + 1]:
n += 1
if n == 3:
return True
i += 2
else:
i += 1
n = 0
return False
if __name__ == '__main__':
with open('words.txt') as f:
for line in f:
for word in line.split():
if is_three(word):
print(word)
入出力結果(Terminal, IPython)
$ ./sample7.py bookkeeperc bookkeepers bookkeeping bookkeepings $
0 コメント:
コメントを投稿