2018年8月13日月曜日

開発環境

  • macOS High Sierra - Apple
  • Emacs (Text Editor)
  • Python 3.7 (プログラミング言語)

Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 3.(Structured Data: Working with Structured Data)、FREQUENCY COUNT MAGNETS(No. 2258)を取り組んでみる。

コード(Emacs)

Python 3

#!/usr/bin/env python3

vowels  = ['a', 'e', 'i', 'o', 'u']
word = input('Provide a word to search for vowels: ')
found = {}
found['a'] = 0
found['e'] = 0
found['i'] = 0
found['o'] = 0
found['u'] = 0

for letter in word:
    if letter in vowels:
        found[letter] += 1

for k, v in sorted(found.items()):
    print(f'{k} was found {v} time(s).')

入出力結果(Terminal, Jupyter(IPython))

$ ./sample1.py
Provide a word to search for vowels: Python
a was found 0 time(s).
e was found 0 time(s).
i was found 0 time(s).
o was found 1 time(s).
u was found 0 time(s).
$ ./sample1.py
Provide a word to search for vowels: aaaaa
a was found 5 time(s).
e was found 0 time(s).
i was found 0 time(s).
o was found 0 time(s).
u was found 0 time(s).
$ 

0 コメント:

コメントを投稿