## 2014年7月17日木曜日

### Python - 関数引数(string, find method)

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の7章(高度な関数: 関数を最大限に活用する)、エクササイズ(p.321)をpythonで考えてみる。

エクササイズ(p.321)

コード(BBEdit, Emacs)

sample321.py

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

ads = ["William: SBM GSHO likes sports, TV, dining",
"Matt: SUM NS likes art, movies, theater",
"Luis: SLM ND likes books, theater, art",
"Mike: DWM DS likes trucks, sports and bieber",
"Peter: SAM likes chess, working out and art",
"Josh: SJM likes sports, movies and theater",
"Jed: DBM likes theater, books and dining"]

def sports_or_workout(s):
return s.find('sports') != -1 or s.find('working') != -1

def ns_theater(s):
return s.find('NS') != -1 and s.find('theater') != -1

def arts_theater_or_dining(s):
return s.find('art') != -1 or s.find('theater') != -1 or \
s.find('dining') != -1

def find(f):
print('検索結果:')
print('--------------------------------------------------')
print('--------------------------------------------------')

if __name__ == '__main__':
for f in [sports_or_workout, ns_theater, arts_theater_or_dining]:
find(f)

```

```\$ ./sample321.py
['William: SBM GSHO likes sports, TV, dining', 'Matt: SUM NS likes art, movies, theater', 'Luis: SLM ND likes books, theater, art', 'Mike: DWM DS likes trucks, sports and bieber', 'Peter: SAM likes chess, working out and art', 'Josh: SJM likes sports, movies and theater', 'Jed: DBM likes theater, books and dining']

--------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Mike: DWM DS likes trucks, sports and bieber
Peter: SAM likes chess, working out and art
Josh: SJM likes sports, movies and theater
--------------------------------------------------

--------------------------------------------------
Matt: SUM NS likes art, movies, theater
--------------------------------------------------

--------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Matt: SUM NS likes art, movies, theater
Luis: SLM ND likes books, theater, art
Peter: SAM likes chess, working out and art
Josh: SJM likes sports, movies and theater
Jed: DBM likes theater, books and dining
--------------------------------------------------
\$
```