開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.6 (プログラミング言語)
Head First Python (Paul Barry (著)、O'Reilly Media)のChapter 3.(Structured Data: Working With Structured Data) の FREQUENCY/-COUNT MAGNETS(No. 2218) を取り組んでみる。
FREQUENCY/-COUNT MAGNETS(No. 2218)
コード(Emacs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- vowels = ['a', 'e', 'i', 'o', 'u'] # word = input('Provide a word to search for vowels: ') with open('vowels4.py') as f: word = f.read().replace('\n', '') 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(k, 'was found', v, 'time(s)')
入出力結果(Terminal, IPython)
$ ./vowels4.py a was found 8 time(s) e was found 22 time(s) i was found 14 time(s) o was found 28 time(s) u was found 14 time(s) $
0 コメント:
コメントを投稿