Skip to content

Instantly share code, notes, and snippets.

@jbochi
Created November 21, 2012 23:45
Show Gist options
  • Save jbochi/4128594 to your computer and use it in GitHub Desktop.
Save jbochi/4128594 to your computer and use it in GitHub Desktop.
Representação de números naturais
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