Created
November 13, 2010 02:16
-
-
Save hoheinzollern/675020 to your computer and use it in GitHub Desktop.
This is what the result should be
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
@invariant(lambda self: self.value >= 0) | |
class PositiveInt(object): | |
@post(lambda self: self.num_changes == 0 and self.value == 0) | |
def __init__(self): | |
self.value = 0 | |
self.num_changes = 0 | |
@post(lambda new, old: new.num_changes == old.num_changes + 1) | |
def add(self, x): | |
self.value += x | |
if self.value < 0: self.value = 0 | |
self.num_changes += 1 | |
@pre(lambda self: self.value < 100) # We don't want to compute big numbers :-) | |
def fact(self): | |
n = 1 | |
for i in range(1, self.value+1): n *= i | |
return n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment