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 dfs(graph: dict[int, list[int]], curr_node: int, visited: list[bool]) -> None: | |
""" | |
Depth First Search (DFS) traversal on a graph. | |
This function marks all nodes reachable from `curr_node` as visited. | |
:param graph: A dictionary representing the graph, where each key is a node | |
and each value is a list of adjacent nodes. For example: | |
{ | |
1: [2, 3], |
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 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
import time | |
def age() -> int: | |
''' | |
post: __return__ >= 0 | |
''' | |
return int(time.time()) |
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 re | |
from typing import Optional | |
def parse_year(yearstring: str) -> Optional[int]: | |
''' | |
Something is wrong with this year parser! Can you guess what it is? | |
post: __return__ is None or 1000 <= __return__ <= 9999 | |
''' | |
return int(yearstring) if re.match('[1-9][0-9][0-9][0-9]', yearstring) else None |
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 binary_search2(a_list: List[int], search_item: int, low: int, high: int, initial_low=None, initial_high=None) -> int: | |
"""Binary search using recursion with formal verification conditions.""" | |
# Set initial bounds only on the first call | |
if initial_low is None or initial_high is None: | |
initial_low, initial_high = low, high | |
# Preconditions |
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 re | |
from typing import Optional | |
def parse_year(yearstring: str) -> Optional[int]: | |
''' | |
Something is wrong with this year parser! Can you guess what it is? | |
post: __return__ is None or 1000 <= __return__ <= 9999 | |
''' | |
return int(yearstring) if re.match('[1-9][0-9][0-9][0-9]', yearstring) else None |
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 typing import List | |
def average(numbers: List[float]) -> float: | |
''' | |
pre: len(numbers) >= 0 | |
post: min(numbers) <= __return__ <= max(numbers) | |
''' | |
if len(numbers) == 0: | |
return None | |