Last active
August 29, 2015 13:57
-
-
Save valdergallo/9914955 to your computer and use it in GitHub Desktop.
Classe para um sorteio rápido :D
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
# -*- coding: utf-8 -*- | |
""" | |
Classe para um sorteio rápido :D | |
run test: python -m doctest sorteio.py | |
>>> s = DoorPrize([1,2,3]) | |
>>> s[0] | |
1 | |
>>> s.shuffle() != [1,2,3] | |
True | |
>>> len(s) | |
3 | |
>>> s.winner() in [1,2,3] | |
True | |
>>> len(s) | |
2 | |
>>> s[0] in [1,2,3] | |
True | |
>>> s.winner() in [1,2,3] | |
True | |
>>> len(s) | |
1 | |
>>> s[1] | |
Traceback (most recent call last): | |
... | |
IndexError: list index out of range | |
>>> s.winner() in [1,2,3] | |
True | |
>>> len(s) | |
0 | |
>>> s[0] | |
Traceback (most recent call last): | |
... | |
IndexError: list index out of range | |
>>> sorted(s.winners) == [1, 2, 3] | |
True | |
>>> s = DoorPrize([1,2,2,2,3]) | |
>>> s.list_to_sort == [1,2,3] | |
True | |
>>> [i for i in s] # test interator list_to_sort | |
[1, 2, 3] | |
""" | |
from random import shuffle | |
class DoorPrize(object): | |
def __init__(self, list_to_sort=[]): | |
#remove duplicity | |
try: | |
self.list_to_sort = list(set(list_to_sort)) | |
except TypeError: | |
raise TypeError("Invalid list type ", list_to_sort) | |
self.winners = [] | |
def __getitem__(self, key): | |
return self.list_to_sort[key] | |
def __setitem__(self, key, value): | |
self.list_to_sort[key] = value | |
def __len__(self): | |
return len(self.list_to_sort) | |
def winner(self): | |
self.shuffle() | |
poped = self.list_to_sort.pop() | |
self.winners.append(poped) | |
return poped | |
def shuffle(self): | |
return shuffle(self.list_to_sort) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment