class Set:def __init__(self):self._theElements = list()def __len__(self):return len(self._theElements)def __contains__(self, el):ndx = self._findPosition(el)return ndx < len(self) and self._theElements[ndx] == eldef add(self, element):if element not in self:ndx = self._findPosition(element)self._theElements.insert(ndx, element)def remove(self, element):assert element in self, "The element must be in the set."ndx = self._findPosition(element)self._theElements.pop(ndx)def isSubsetOf(self, setB):for element in self:if element not in setB:return Falsereturn Truedef __eq__(self, other):if len(self) != len(other):return Falseelse:for i in range(len(self)):if self._theElements[i] != other._theElements[i]:return Falsereturn Truedef __iter__(self):return _SetIterator(self._theElements)def _findPosition(self, element):low = 0high = len(self) - 1while low <= high:mid = (low + high) // 2if element == self._theElements[mid]:return midelif element < self._theElements[mid]:high = mid - 1else :low = mid + 1return lowdef union(self, setB):newSet = Set()a = 0b = 0while a < len(self) and b < len(setB):valueA = self._theElements[a]valueB = setB._theElements[b]if valueA < valueB:newSet._theElements.append(valueA)a += 1elif valueA > valueB:newSet._theElements.append(valueB)b += 1else :newSet._theElements.append(valueA)a += 1b += 1while a < len(self):newSet._theElements.append(self._theElements[a])a += 1while b < len(setB):newSet._theElements.append(setB._theElements[b])b += 1return newSetdef isSubsetOf(self, other):"""is subset of other set."""selfNdx = 0otherNdx = 0while selfNdx < len(self) and otherNdx < len(other) :# same value plus 1 each indxif self._theElements[selfNdx] == other._theElements[otherNdx]:selfNdx += 1otherNdx += 1# value bigger than others valuse plus one other ndxelif self._theElements[selfNdx] > other._theElements[otherNdx]:otherNdx += 1# value lower than others do nothingelse :passif selfNdx == len(self):return Trueelse:return Falsedef intersect(self, other):intersection = Set()selfNdx = 0otherNdx = 0while selfNdx < len(self) and otherNdx < len(other):curSelfValue = self._theElements[selfNdx]curOtherValue = other._theElements[otherNdx]if curSelfValue == curOtherValue:intersection.add(curSelfValue)selfNdx += 1otherNdx += 1elif curSelfValue > curOtherValue:otherNdx += 1else:selfNdx += 1return intersectiondef difference(self, other):diffSet = Set()selfNdx = 0otherNdx = 0while selfNdx < len(self) and otherNdx < len(other):curSelfValue = self._theElements[selfNdx]curOtherValue = other._theElements[otherNdx]if curSelfValue == curOtherValue:selfNdx += 1otherNdx += 1elif curSelfValue > curOtherValue:otherNdx += 1if otherNdx < len(other) or \(otherNdx < len(other) and \curSelfValue < other._theElements[otherNdx]):diffSet.add(curSelfValue)else:selfNdx += 1diffSet.add(curSelfValue)return diffSetclass _SetIterator:def __init__(self, elements):self._elements = elementsself._ndx = 0def __iter__(self):return selfdef __next__(self):if self._ndx < len(self._elements) :el = self._elements[self._ndx]self._ndx += 1return elelse :raise StopIteration
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。