-
-
Save stephengruppetta/e0ad609a6e19042f15a9956b96c2f5f5 to your computer and use it in GitHub Desktop.
This file contains 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
import random | |
class RandomWalk: | |
def __init__(self, iterable): | |
self.values = list(iterable) | |
def __iter__(self): | |
return RandomWalkIterator(self) | |
class RandomWalkIterator: | |
def __init__(self, random_walk): | |
self.random_walk = random_walk | |
self.random_indices = list( | |
range(len(random_walk.values)) | |
) | |
random.shuffle(self.random_indices) | |
self.idx = 0 | |
def __next__(self): | |
output = self.random_walk.values[ | |
self.random_indices[self.idx] | |
] | |
self.idx += 1 | |
return output | |
captains = RandomWalk( | |
[ | |
"Pike", | |
"Kirk", | |
"Picard", | |
"Sisko", | |
"Janeway", | |
"Riker", | |
] | |
) | |
for captain in captains: | |
print(captain) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment