Created
November 21, 2012 23:45
-
-
Save jbochi/4128594 to your computer and use it in GitHub Desktop.
Representação de números naturais
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 Natural(object): | |
def __init__(self, anterior): | |
self.anterior = anterior | |
def __str__(self): | |
return str(self.anterior) + " + 1" | |
def __add__(self, other): | |
return self.anterior + other.sucessor() | |
def __sub__(self, other): | |
if other.isZero(): | |
return self | |
return self.anterior - other.anterior | |
def __eq__(self, other): | |
return self.anterior == other.anterior | |
def sucessor(self): | |
return Natural(self) | |
def isZero(self): | |
return False | |
class Zero(Natural): | |
def __init__(self): | |
pass | |
def __str__(self): | |
return "0" | |
def __add__(self, other): | |
return other | |
def __lsub__(self, other): | |
return other | |
def __eq__(self, other): | |
return other.isZero() | |
def isZero(self): | |
return True | |
zero = Zero() | |
um = zero.sucessor() | |
dois = um.sucessor() | |
tres = dois.sucessor() | |
print dois + um == tres | |
print dois - dois == Zero() | |
print tres - dois == um |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment