2012年2月26日日曜日

開発環境

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

4.

コード(TextWrangler)

#!/usr/bin/env python
#encoding: utf-8

class Set:
 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 Set(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: ' + `self.data`

入出力結果(Terminal)

$ python
Python 2.7.2 (default, Feb 12 2012, 23:50:38) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_program import Set
>>> a=Set([1,2,3,4,5,6,7,8,9,10])
>>> b=Set([6,7,8,9,10,11,12,13,14,15])
>>> a & b
Set: [6, 7, 8, 9, 10]
>>> a | b
Set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> x=Set('Hello, World!')
>>> x
Set: ['H', 'e', 'l', 'o', ',', ' ', 'W', 'r', 'd', '!']
>>> x[1]
'e'
>>> x[4]
','
>>> for i in x:
...     print i
... 
H
e
l
o
,
 
W
r
d
!
>>> s='Hello, Python!'
>>> x & s
Set: ['H', 'e', 'l', 'o', ',', ' ', '!']
>>> x | s
Set: ['H', 'e', 'l', 'o', ',', ' ', 'W', 'r', 'd', '!', 'P', 'y', 't', 'h', 'n']
>>> ^D
$

b, c: Setクラスの__getitem__メソッドが呼び出される。

e: Setのサブクラスを作成。

コード(TextWrangler)

#!/usr/bin/env python
#encoding: utf-8

class Set:
 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 Set(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: ' + `self.data`

class SetSub(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(*others):
  res = []
  for other in others:
   for x in other:
    if not x in res:
     res.append(x)
  return Set(res)

入出力結果(Terminal)

$ python
Python 2.7.2 (default, Feb 12 2012, 23:50:38) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_program import SetSub
>>> a=SetSub([1,2,3,4,5])
>>> b=SetSub([3,4,5,6])
>>> c=SetSub([6,7,8,9,10,1,2,3])
>>> a.intersect(b,c)
Set: [3]
>>> a.union(b,c)
Set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> ^D
$

Setクラスにリストとしての機能の連結を追加。

コード(TextWrangler)

#!/usr/bin/env python
#encoding: utf-8

class Set:
 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 Set(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: ' + `self.data`
 def __add__(self, other):
  res = self.data[:]
  for x in other:
   res.append(x)
  return Set(res)

class SetSub(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(*others):
  res = []
  for other in others:
   for x in other:
    if not x in res:
     res.append(x)
  return Set(res)

入出力結果(Terminal)

$ python
Python 2.7.2 (default, Feb 12 2012, 23:50:38) 
[GCC 4.2.1 Compatible Apple Clang 3.0 (tags/Apple/clang-211.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from python_program import Set
>>> a=Set([1,2])
>>> b=Set([2,3,4])
>>> a+b
Set: [1, 2, 3, 4]
>>> ^D

こんな感じでいいのかな。

0 コメント:

コメントを投稿