Created
October 5, 2012 19:36
-
-
Save arturfsousa/3841883 to your computer and use it in GitHub Desktop.
Solução lista_2 do curso OOPY - Turma 0
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 | |
from random import shuffle | |
class Tombola(object): | |
'''Sorteia itens sem repetir''' | |
def carregar(self, seq): | |
self.itens = list(seq) | |
def misturar(self, misturadora=None): | |
'''Adiciona um param para demonstrar a Injeção de Dependência''' | |
shuffle(self.itens) if not misturadora else misturadora(self.itens) | |
def sortear(self): | |
return self.itens.pop() | |
def carregada(self): | |
return bool(self.itens) | |
def __iter__(self): | |
''' | |
==================================================== | |
Generator expressions are another honking great idea | |
==================================================== | |
(2.2, Bônus) As iterações são dependentes do attr de instância | |
self.itens, pois, a cada iteração um ítem de self.itens é removido | |
pelo .pop() através do método sortear() da tôbola logo no meu caso | |
o seguinte teste deve passar:: | |
>>> from tombola import Tombola | |
>>> t = Tombola() | |
>>> t.carregar([1, 2, 3]) | |
>>> for i in t: | |
... print i | |
... break | |
3 | |
>>> for i in t: | |
... print i | |
2 | |
1 | |
''' | |
return (self.sortear() for bola in range(len(self.itens))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment