Created
December 11, 2012 02:11
-
-
Save guilhermebr/4255220 to your computer and use it in GitHub Desktop.
OOPY3 Exercicio1
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
# coding: utf-8 | |
""" | |
Código inicial usado na Lista de Exercícios 1 do curso | |
"Objetos Pythonicos" de Luciano Ramalho, Oficinas Turing. | |
""" | |
class Contador(object): | |
def __init__(self): | |
self.ocorrencias = {} | |
def incrementar(self, item): | |
qtd = self.ocorrencias.get(item, 0) + 1 | |
self.ocorrencias[item] = qtd | |
def contagem(self, item): | |
return self.ocorrencias[item] | |
class ContadorAmigavel(Contador): | |
def contagem(self, item): | |
try: | |
return self.ocorrencias[item] | |
except KeyError: | |
return 0 | |
class ContadorTotalizador(Contador): | |
def __init__(self): | |
Contador.__init__(self) | |
self.total = 0 | |
def incrementar(self, item): | |
Contador.incrementar(self, item) | |
self.total += 1 | |
def porcentagem(self, item): | |
return (self.contagem(item)/float(self.total))*100 | |
class ContadorTotalizadorAmigavel(ContadorTotalizador, ContadorAmigavel): | |
pass | |
ct = ContadorTotalizadorAmigavel() | |
for letra in 'banana': | |
ct.incrementar(letra) | |
ct.total | |
ct.contagem('x') | |
ct.porcentagem('a') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment