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
| from dataclasses import dataclass | |
| class HasConsistentHash: | |
| ''' | |
| A mixin to enforce that classes have hash methods that are consistent | |
| with thier equality checks. | |
| ''' | |
| def __eq__(self, other: object) -> bool: | |
| ''' | |
| post: implies(__return__, hash(self) == hash(other)) |
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
| def make_bigger(n: int) -> int: | |
| ''' | |
| post: __return__ != 0 | |
| ''' | |
| return 2 * n + 10 | |
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
| def make_bigger(n: int) -> int: | |
| ''' | |
| post: __return__ %2 == 0 | |
| ''' | |
| sum = 0 | |
| for i in range(n): | |
| sum += 2 | |
| return sum |
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
| from typing import List | |
| def average(numbers: List[float]) -> float: | |
| ''' | |
| pre: len(numbers) > 0 | |
| post: min(numbers) <= __return__ <= max(numbers) | |
| ''' | |
| return sum(numbers) / len(numbers) |
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
| from dataclasses import dataclass | |
| class HasConsistentHash: | |
| ''' | |
| A mixin to enforce that classes have hash methods that are consistent | |
| with thier equality checks. | |
| ''' | |
| def __eq__(self, other: object) -> bool: | |
| ''' | |
| post: implies(__return__, hash(self) == hash(other)) |
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
| def make_bigger(n: int) -> int: | |
| ''' | |
| post: __return__ % 2 == 0 | |
| ''' | |
| sum = 0 | |
| for i in range(n): | |
| sum += 2 | |
| return sum |
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
| def make_bigger(n: int) -> int: | |
| ''' | |
| post: __return__ != 0 | |
| ''' | |
| return 2 * n + 10 | |
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
| import dataclasses | |
| from typing import List | |
| @dataclasses.dataclass | |
| class AverageableQueue: | |
| ''' | |
| A queue of numbers with a O(1) average() operation. | |
| inv: self._total == sum(self._values) | |
| ''' | |
| _values: List[int] |
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
| import dataclasses | |
| from typing import List | |
| @dataclasses.dataclass | |
| class AverageableQueue: | |
| ''' | |
| A queue of numbers with a O(1) average() operation. | |
| inv: self._total == sum(self._values) | |
| ''' | |
| _values: List[int] |
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
| def make_bigger(n: int) -> int: | |
| ''' | |
| post: __return__ != 0 | |
| ''' | |
| return 2 * n + 10 | |
NewerOlder