Skip to content

Instantly share code, notes, and snippets.

View pschanely's full-sized avatar

Phillip Schanely pschanely

View GitHub Profile
@pschanely
pschanely / main.py
Created April 6, 2025 21:10
Shared via CrossHair Playground
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],
@pschanely
pschanely / main.py
Created April 1, 2025 17:40
Shared via CrossHair Playground
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)
@pschanely
pschanely / main.py
Created April 1, 2025 17:39
Shared via CrossHair Playground
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)
@pschanely
pschanely / main.py
Created April 1, 2025 16:44
Shared via CrossHair Playground
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))
@pschanely
pschanely / main.py
Created March 31, 2025 11:21
Shared via CrossHair Playground
import time
def age() -> int:
'''
post: __return__ >= 0
'''
return int(time.time())
@pschanely
pschanely / main.py
Created March 30, 2025 02:23
Shared via CrossHair Playground
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
@pschanely
pschanely / main.py
Created March 29, 2025 17:51
Shared via CrossHair Playground
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
@pschanely
pschanely / main.py
Created March 27, 2025 12:07
Shared via CrossHair Playground
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
@pschanely
pschanely / main.py
Created March 27, 2025 00:38
Shared via CrossHair Playground
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)
@pschanely
pschanely / main.py
Created March 25, 2025 21:03
Shared via CrossHair Playground
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