标签:ESS size order 并且 style 运行时 des col 一个
""" File: arraysortedbag.py
""" from arraybag import ArrayBag
class ArraySortedBag( ArrayBag ): """An array-based sorted bag implementation."""
#Constructor def __init__( self, sourceCollection = None ): """Sets the initial state of self, which includes the contents of sourceCollection, if it‘s present.""" ArrayBag.__init__( self, sourceCollection ) |
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Lijunjie
""" File: arraysortedbag.py
""" from arraybag import ArrayBag
class ArraySortedBag( ArrayBag ): """An array-based sorted bag implementation."""
#Constructor def __init__( self, sourceCollection = None ): """Sets the initial state of self, which includes the contents of sourceCollection, if it‘s present.""" ArrayBag.__init__( self, sourceCollection )
#Accessor method def __contains__( self, item ): """Return True if item is in self, or False otherwise""" left = 0 right = len( self ) - 1 while left <= right: midpoint = ( left + right ) // 2 if self._items[midpoint] == item : return True if self._items[midpoint] > item : right = midpoint - 1 else: left = midpoint + 1 return False |
#Mutator method def add( self, item ): """Adds item to self.""" #Empty or last item, call ArrayBag.add if self.isEmpty() or item >= self._items[len( self ) - 1]: ArrayBag.add( self, item ) else: #Resize the array if it is full here #Search for the first item > = new item targetIndex = 0 while item > self._items[targetIndex]: targetIndex += 1 #open a hole for a newitem for index in range( len( self ), targetIndex, -1 ): self._items[index] = self._items[index - 1] #insert item and update size self._items[targetIndex] = item self._size += 1 |
""" File: abstractbag.py Author: Lijunjie """
class AbstractBag( object ): """An abstract bag implementation."""
#Constructor def __init__( self, sourceCollection = None ): """Sets the initial state of self, which includes the contents of sourceCollection, if it‘s present.""" self._size = 0 if sourceCollection: for item in sourceCollection: self.add( item ) |
""" File: arraybag.py Author: Lijunjie """
from arrays import Array from abstractbag import AbstractBag
class ArrayBag( AbstractBag ): """An array-based bag implementation."""
#Class variable DEFAULT_CAPACTIY = 10
#Constructor def __init__( self, sourceCollection = None ): """Sets the initial state of self, which includes the contents of sourceCollection, if it‘s present.""" self._items = Array( ArrayBag.DEFAULT_CAPACTIY ) AbstractBag.__init__( self, sourceCollection ) |
def __add__( self, other ): """Return a new bag containing the contents of self and other""" result = type( self )( self ) for item in other: result.add( item ) return result |
def __eq__( self, other ): """Return True if self equals other, otherwise False.""" if self is other: return True if type( self ) != type( other ) or len( self ) != len( other ): return False otherIter = iter( other ) for item in self: if item != next( otherIter ): return False return True |
标签:ESS size order 并且 style 运行时 des col 一个
原文地址:https://www.cnblogs.com/lijunjie9502/p/9892541.html