Ad
Code
Diff
  • class UniqueInOrderIterator:
        def __init__(self, iterable):
            self.__iterable = iterable
            self.__currentPosition = 0
            self.__lastElement = None
        
        def __iter__(self):
            return self
        
        def __next__(self):
            self.__pointToNextValidElement()
            result = self.__pointedElement()
            self.__pointToNextElement()
            return result
        
        def __pointToNextValidElement(self):
            while (self.__currentPosition < len(self.__iterable)
                    and self.__iterable[self.__currentPosition] == self.__lastElement):
                self.__pointToNextElement()
        
        def __pointedElement(self):
            return self.__iterable[self.__currentPosition]
        
        def __pointToNextElement(self):
            self.__lastElement = self.__pointedElement()
            self.__currentPosition += 1
            self.__stopIfEndReached()
        
        def __stopIfEndReached(self):
            if self.__currentPosition >= len(self.__iterable):
                raise StopIteration
        
                
    
    def unique_in_order(iterable):
        unique = []
        uniqueInOrderIterator = UniqueInOrderIterator(iterable)
        
        for element in uniqueInOrderIterator:
            unique.append(element)
    
        return unique
    • class UniqueInOrderIterator:
    • def __init__(self, iterable):
    • self.__iterable = iterable
    • self.__currentPosition = 0
    • self.__lastElement = None
    • def __iter__(self):
    • return self
    • def __next__(self):
    • self.__pointToNextValidElement()
    • result = self.__pointedElement()
    • self.__pointToNextElement()
    • return result
    • def __pointToNextValidElement(self):
    • while (self.__currentPosition < len(self.__iterable)
    • and self.__iterable[self.__currentPosition] == self.__lastElement):
    • self.__pointToNextElement()
    • def __pointedElement(self):
    • return self.__iterable[self.__currentPosition]
    • def __pointToNextElement(self):
    • self.__lastElement = self.__pointedElement()
    • self.__currentPosition += 1
    • self.__stopIfEndReached()
    • def __stopIfEndReached(self):
    • if self.__currentPosition >= len(self.__iterable):
    • raise StopIteration
    • def unique_in_order(iterable):
    • iterable = list(iterable)
    • unique = [None]
    • for x in iterable:
    • if x != unique[-1]:
    • unique.append(x)
    • return unique[1:]
    • unique = []
    • uniqueInOrderIterator = UniqueInOrderIterator(iterable)
    • for element in uniqueInOrderIterator:
    • unique.append(element)
    • return unique
Code
Diff
  • class UniqueInOrderIterator:
        def __init__(self, iterable):
            self.__iterable = iterable
            self.__currentPosition = 0
            self.__lastElement = None
        
        def __iter__(self):
            return self
        
        def __next__(self):
            self.__pointToNextValidElement()
            result = self.__pointedElement()
            self.__pointToNextElement()
            return result
        
        def __pointToNextValidElement(self):
            while (self.__currentPosition < len(self.__iterable)
                    and self.__iterable[self.__currentPosition] == self.__lastElement):
                self.__pointToNextElement()
        
        def __pointedElement(self):
            return self.__iterable[self.__currentPosition]
        
        def __pointToNextElement(self):
            self.__lastElement = self.__pointedElement()
            self.__currentPosition += 1
            self.__stopIfEndReached()
        
        def __stopIfEndReached(self):
            if self.__currentPosition >= len(self.__iterable):
                raise StopIteration
        
                
    
    def unique_in_order(iterable):
        unique = []
        uniqueInOrderIterator = UniqueInOrderIterator(iterable)
        
        for element in uniqueInOrderIterator:
            unique.append(element)
    
        return unique
    • class UniqueInOrderIterator:
    • def __init__(self, iterable):
    • self.__iterable = iterable
    • self.__currentPosition = 0
    • self.__lastElement = None
    • def __iter__(self):
    • return self
    • def __next__(self):
    • self.__pointToNextValidElement()
    • result = self.__pointedElement()
    • self.__pointToNextElement()
    • return result
    • def __pointToNextValidElement(self):
    • while (self.__currentPosition < len(self.__iterable)
    • and self.__iterable[self.__currentPosition] == self.__lastElement):
    • self.__pointToNextElement()
    • def __pointedElement(self):
    • return self.__iterable[self.__currentPosition]
    • def __pointToNextElement(self):
    • self.__lastElement = self.__pointedElement()
    • self.__currentPosition += 1
    • self.__stopIfEndReached()
    • def __stopIfEndReached(self):
    • if self.__currentPosition >= len(self.__iterable):
    • raise StopIteration
    • def unique_in_order(iterable):
    • unique = []
    • uniqueInOrderIterator = UniqueInOrderIterator(iterable)
    • for i in range(len(iterable)):
    • if (i == 0):
    • unique.append(iterable[i])
    • elif iterable[i] != unique[-1]:
    • unique.append(iterable[i])
    • for element in uniqueInOrderIterator:
    • unique.append(element)
    • return unique
Code
Diff
  • def unique_in_order(iterable):
        unique = []
        
        for i in range(len(iterable)):
            if (i == 0):
                unique.append(iterable[i])
            elif iterable[i] != unique[-1]:
                unique.append(iterable[i])
    
        return unique
    • def unique_in_order(iterable):
    • iterable = list(iterable)
    • unique = [None]
    • for x in iterable:
    • if x != unique[-1]:
    • unique.append(x)
    • return unique[1:]
    • unique = []
    • for i in range(len(iterable)):
    • if (i == 0):
    • unique.append(iterable[i])
    • elif iterable[i] != unique[-1]:
    • unique.append(iterable[i])
    • return unique