2016年6月12日日曜日

開発環境

Think Python (Allen B. Downey (著)、 O'Reilly Media)のChapter 9.(Case Study Word Play)のExercises 9-2(No. 1961)を取り組んでみる。

Exercises 9-2(No. 1961)

コード(Emacs)

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

def has_no_e(word):
    return word.find('e') == -1

count = 0
count1 = 0
with open('words.txt') as f:
    for words in f:
        for word in words.split():
            word = word.strip()
            if word != '':
                count += 1
                if has_no_e(word):
                    count1 += 1
                    if count1 <= 10:
                        print(word)
                    elif count1 == 11:
                        print('...')
                    
print('{0:.2f}%'.format(count1 / count * 100))

入出力結果(Terminal, IPython)

$ ./sample2.py
aa
aah
aahing
aahs
aal
aalii
aaliis
aals
aardvark
aardvarks
...
33.07%
$

0 コメント:

コメントを投稿