Skip to content

Instantly share code, notes, and snippets.

View ZechCodes's full-sized avatar
🏄‍♂️
Surfing on a space-time bubble

Zech Zimmerman ZechCodes

🏄‍♂️
Surfing on a space-time bubble
View GitHub Profile
@ZechCodes
ZechCodes / fancy_enums.py
Last active September 20, 2023 15:04
Experimenting with creating enums with auto values dynamically without having to assign `auto()`. This is a terrible idea and results in code that at times could be ambiguous. A fun excersize.
import inspect
from enum import Enum, auto, EnumType
class EnumCaptureDict(dict):
def __getitem__(self, item):
if item in self or (item.startswith("__") and item.endswith("__")):
return super().__getitem__(item)
frame = inspect.currentframe().f_back
from typing import Type, TypeVar
class Sentinel:
def __new__(cls):
instance = super().__new__(cls)
cls.__new__ = lambda _: instance
return instance
@ZechCodes
ZechCodes / unordered_tree.py
Created May 21, 2023 13:35
A simple unordered tree implementation in Python for the weekly coding prompt on the Beginner.Codes Discord server. Join now https://beginner.codes/discord
from typing import Generic, TypeVar
T = TypeVar("T")
class Node(Generic[T]):
def __init__(self, value: T, parent: "Node[T] | None" = None):
self._children = tuple()
self._parent = parent
@ZechCodes
ZechCodes / linked-list.py
Created April 30, 2023 13:35
A simple linked list implementation in Python for the weekly coding prompt on the Beginner.Codes Discord server. Join now https://beginner.codes/discord
from typing import Generic, TypeVar
T = TypeVar("T")
class Node(Generic[T]):
def __init__(self, value: T, head: "Node | None" = None, next_node: "Node | None" = None):
self.head = self if head is None else head
self.next = next_node
@ZechCodes
ZechCodes / counting_bs_after_as.py
Created August 15, 2022 02:51
A crazy little async state machine that's intended to read like english as much as possible. Just download both file and run the `count_bs_after_as.py` to see it count all the B's it finds after A's in the string.
import asyncio
from state_machine import StateMachine, State
class CountBsAfterAs(StateMachine):
def __init__(self):
super().__init__()
self.num_bs_after_as = 0
@State
@ZechCodes
ZechCodes / mutation_descriptors.py
Last active March 20, 2022 15:43
Apply Mutations on Dataclass Fields
from __future__ import annotations
import json
from dataclasses import dataclass, MISSING, field as _field
from typing import Any, Callable
class MutationDescriptor:
def __init__(self, mutator: Callable[[Any], Any]):
self._mutator = mutator
@ZechCodes
ZechCodes / set_vs_list.md
Last active May 2, 2021 16:52
Sets vs Lists in Python

Sets vs Lists in Python

This is just a fun demo of how lists and sets handle the in lookup.

Code

class Testing:
    def __init__(self, name):
        self.name = name
@ZechCodes
ZechCodes / challenge_175.md
Created February 2, 2021 02:26
Challenge 175 - Syncopated Rhythm

Challenge 175 - Syncopated Rhythm

Syncopation means an emphasis on a weak beat of a bar of music; most commonly, beats 2 and 4 (and all other even-numbered beats if applicable).

s is a line of music, represented as a string, where hashtags # represent emphasized beats. Create a function that returns if the line of music contains any syncopation.

Examples

has_syncopation(".#.#.#.#") ➞ True
# There are Hash signs in the second, fourth, sixth and
@ZechCodes
ZechCodes / challenge_174.md
Created January 30, 2021 16:43
Challenge 174 - Revised Julian Calendar

Challenge 174 - Revised Julian Calendar

Background

The Revised Julian Calendar is a calendar system very similar to the familiar Gregorian Calendar, but slightly more accurate in terms of average year length. The Revised Julian Calendar has a leap day on Feb 29th of leap years as follows:

  • Years that are evenly divisible by 4 are leap years.
  • Exception: Years that are evenly divisible by 100 are not leap years.
  • Exception to the exception: Years for which the remainder when divided by 900 is either 200 or 600 are leap years.
@ZechCodes
ZechCodes / challenge_173.md
Created January 29, 2021 01:41
Challenge 173 - Replace With Alphabet Position

Challenge 173 - Replace With Alphabet Position

Given a string, replace every letter with its position in the alphabet.

If anything in the text isn't a letter, ignore it and don't include it in the output.

"a" = 1, "b" = 2, etc.

Example