Python和数据结构学习 --- 4

额,睡不着

第二章的尾部还有个关于写翻转棋的,暂时先不写.

下面就是直接上Set和Map了,这里使用的是最简单的方法.也就是内部使用的list

先上Set

不过看上去复杂都很高的.

class Set:
    def __init__(self):
        self._theElements = list()
        
    def __len__(self):
        return len(self._theElements)
    
    def __contains__(self,element):
        return element in self._theElements
        
    def add(self,element):
        if element not in self:
            self._theElements.append(element)
            
    def remove(self,element):
        assert element in self,"The element must be in the set"
        self._theElements.remove(item)
        
    def __eq__(self,setB):
        if len(self) != len(setB):
            return False
        else:
            return self.isSubsetOf(setB)
        
    def isSubsetOf(self,setB):
        for element in self:
            if element not in setB:
                return False
        return True
    
    def union(self,setB):
        newSet = Set()
        newSet._theElements.extend(self._theElements)
        for element in setB:
            if element not in self:
                newSet._theElements.append(element)
        return newSet
        
    def interset(self,setB):
        newSet = Set()
        for element in setB:
            if element in self:
                newSet._theElements.append(element)
        return newSet
        
    def difference(self,setB):
        newSet = Set()
        for element in self:
            if element not in setB:
                newSet._theElements.append(element)
        
        return newSet
        
    def __iter__(self):
        return iter(self._theElements)

插入$O(n)$,求并,交.差,相等$O(n^2)$,当然后面有通过排序来进行优化的.

 

下面就是Map了,不过其中额外使用了一个简单的保存key,value. 这个类似于stl中的 std::pair.

代码如下:

class Map:
    def __init__(self):
        self._entryList = list()
    
    def __len__(self):
        return len(self._entryList)
    
    def __contains__(self,key):
        ndx = self._findPosition(key)
        return ndx is not None
    
    def add(self,key,value):
        ndx = self._findPosition(key)
        if ndx is not None:
            self._entryList[ndx].value = value
            return False
        else:
            entry = _MapEntry(key,value)
            self._entryList.append(entry)
            return True
    
    def valueOf(self,key):
        ndx = self._findPosition(key)
        assert ndx is not None,"Invalid map key"
        return self._entryList[ndx].value
        
    def remove(self,key):
        ndx = self._findPosition(key)
        ndx = self._findPosition(key)
        assert ndx is not None,"Invalid map key"
        self._entryList.pop(ndx)
    
    def __iter__(self):
        return _MapIterator(self._entryList)
    
    
    def _findPosition(self,key):
        for i in range(len(self)):
            if self._entryList[i].key == key:
                return i
        return None
    
class _MapEntry:
    def __init__(self,key,value):
        self.key = key
        self.value = value
        
class _MapIterator:
    def __init__(self,mapRef):
        self._curI = 0
        self._mapRef = mapRef
    
    def __iter__(self):
        return self
    
    def next(self):
        if self._curI < len(self._mapRef):
            entry = self._mapRef[self._curI]
            self._curI += 1
            return entry.key,entry.value
        else:
            raise StopIteration

嗯.都是很简单的.

睡觉拉..

 

posted @ 2012-11-30 00:17  zhuangzhuang1988  阅读(357)  评论(0编辑  收藏  举报