2014年9月7日日曜日

開発環境

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 sportsNoBieber(s):
    return s.find('sports') != -1 and s.find('bieber') == -1

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

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

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

def find(match):
    print('検索結果')
    print('-' * 50)
    for ad in ads:
        if match(ad):
            print(ad)
    print('-' * 50)

find(sportsNoBieber)
find(sportsOrWorkout)
find(nsTheater)
find(artsTheaterOrDining)

入出力結果(Terminal, IPython)

$ ./sample321.py
検索結果
--------------------------------------------------
William: SBM GSHO likes sports, TV, dining
Josh: SJM likes sports, movies and theater
--------------------------------------------------
検索結果
--------------------------------------------------
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
--------------------------------------------------
$

0 コメント:

コメントを投稿