Created
July 19, 2023 17:38
-
-
Save tjarratt/d46b3d03bbe98c1116b98b7c3687ecf0 to your computer and use it in GitHub Desktop.
A set that can be iterated and knows its size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListSet: | |
def __init__(self): | |
self._values = [] | |
self.__index = 0 | |
def add(self, value): | |
if value in self._values: | |
return | |
self._values.append(value) | |
def __iter__(self): | |
return self | |
def __next__(self): | |
if self.__index >= len(self._values): | |
raise StopIteration() | |
val = self._values[self.__index] | |
self.__index += 1 | |
return val | |
def __len__(self): | |
return len(self._values) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment