2013年1月7日月曜日

開発環境

『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7) のVI部(オブジェクト指向プログラミング)のまとめ演習5.(Setクラス)を解いてみる。

その他参考書籍

5.(Setクラス)

コード(BBEdit)

sample.py

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

class Set(list):
    def __init__(self, value=[]):
        self.data = []
        self.concat(value)
    def intersect(self, other):
        res = []
        for x in self.data:
            if x in other:
                res.append(x)
        return res
    def union(self, other):
        res = self.data[:]
        for x in other:
            if not x in res:
                res.append(x)
        return Set(res)
    def concat(self, value):
        for x in value:
            if not x in self.data:
                self.data.append(x)
    def __len__(self): return len(self.data)
    def __getitem__(self, key): return self.data[key]
    def __and__(self, other): return self.intersect(other)
    def __or__(self, other): return self.union(other)
    def __repr__(self): return "Set:{0}".format(self.data)
    def __getattr__(self, name):
        return getattr(self.data, name)
    def __setattr__(self, name, value):
        self.__dict__[name] = value
    def __add__(self, other):
        return Set(self.data + other)

class SubSet(Set):
    def intersect(self, *others):
        res = []
        for x in self:
            for other in others:
                if not x in other: break
            else:
                res.append(x)
        return Set(res)
    def union(self, *others):
        res = self[:]
        for other in others:
            for x in other:
                if not x in res:
                    res.append(x)
        return Set(res)

if __name__ == '__main__':
    s1 = Set([0,1,2,3,4,5,6])
    s2 = Set([9,8,7,6,5])
    print("{0} {1} {2}: {3}".format(s1, "&", s2, s1 & s2))
    print("{0} {1} {2}: {3}".format(s1, "|", s2, s1 | s2))
    s3 = Set("python") # __init__メソッドとその中のconcatメソッドが呼び出される
    for x in s3: # __getitem__メソッドが呼び出される
        print(x)
    js = "javascript"
    print(s3 & js)
    try:
        print(js & s3)
    except Exception as err:
        print(err)
    print(s3 | js)
    try:
        print(js | s3)
    except Exception as err:
        print(err)
    ss1 = SubSet([0, 1, 2, 3, 4, 5, 6])
    ss2 = SubSet([5, 6, 7, 8, 9])
    ss3 = SubSet([0, 1, 5, 6, 10])
    for x in [ss1, ss2, ss3]:
        print(x)
        for y in [ss1, ss2, ss3]:
            print("  {0}".format(y))
            for z in [ss1, ss2, ss3]:
                print("    {0}".format(z))
                print("        &: {0}".format(x.intersect(y, z)))
                print("        |: {0}".format(x.union(y, z)))
    l = [5, 6, 7, 8, 9]
    print("{0} + {1} = {2}".format(s1, l, s1 + l))

入出力結果(Terminal)

$ ./sample.py
Set:[0, 1, 2, 3, 4, 5, 6] & Set:[9, 8, 7, 6, 5]: []
Set:[0, 1, 2, 3, 4, 5, 6] | Set:[9, 8, 7, 6, 5]: Set:[0, 1, 2, 3, 4, 5, 6]
['p', 't']
unsupported operand type(s) for &: 'str' and 'Set'
Set:['p', 'y', 't', 'h', 'o', 'n', 'j', 'a', 'v', 's', 'c', 'r', 'i']
unsupported operand type(s) for |: 'str' and 'Set'
Set:[0, 1, 2, 3, 4, 5, 6]
  Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
  Set:[5, 6, 7, 8, 9]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
  Set:[0, 1, 5, 6, 10]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[0, 1, 2, 3, 4, 5, 6]
Set:[5, 6, 7, 8, 9]
  Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
  Set:[5, 6, 7, 8, 9]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
  Set:[0, 1, 5, 6, 10]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[5, 6, 7, 8, 9]
Set:[0, 1, 5, 6, 10]
  Set:[0, 1, 2, 3, 4, 5, 6]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
  Set:[5, 6, 7, 8, 9]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
  Set:[0, 1, 5, 6, 10]
    Set:[0, 1, 2, 3, 4, 5, 6]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
    Set:[5, 6, 7, 8, 9]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
    Set:[0, 1, 5, 6, 10]
        &: Set:[]
        |: Set:[0, 1, 5, 6, 10]
Set:[0, 1, 2, 3, 4, 5, 6] + [5, 6, 7, 8, 9] = Set:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
$

0 コメント:

コメントを投稿