Skip to content

Instantly share code, notes, and snippets.

View arlyon's full-sized avatar

Alexander Lyon arlyon

View GitHub Profile
@arlyon
arlyon / OpenSimplexNoise.cs
Created April 1, 2017 14:57 — forked from digitalshadow/OpenSimplexNoise.cs
OpenSimplex Noise Refactored for C#
/* OpenSimplex Noise in C#
* Ported from https://gist.github.com/KdotJPG/b1270127455a94ac5d19
* and heavily refactored to improve performance. */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
namespace NoiseTest
/**
* Base contract that all upgradeable contracts should use.
*
* Contracts implementing this interface are all called using delegatecall from
* a dispatcher. As a result, the _sizes and _dest variables are shared with the
* dispatcher contract, which allows the called contract to update these at will.
*
* _sizes is a map of function signatures to return value sizes. Due to EVM
* limitations, these need to be populated by the target contract, so the
* dispatcher knows how many bytes of data to return from called functions.
@arlyon
arlyon / pseudo.py
Last active September 25, 2017 16:55
def matrix_multiplication(matrix1, matrix2):
# m * n x n * p => m * p
m = matrix1.width
n1 = matrix1.height
n2 = matrix2.width
p = matrix2.height
# if the n's of the matrices don't match then fail
@arlyon
arlyon / memoize_decorator.py
Last active August 21, 2020 12:16
A nice little memoization decorator.
from typing import Callable
def memoize(func: Callable) -> Callable:
solutions = {}
def new_func(*n):
if n not in solutions:
solutions[n] = func(*n)
return solutions[n]
@arlyon
arlyon / read_data_from_file.py
Created November 30, 2017 21:05
Reads data from a file and passes it as an argument to the function.
def read_data_from_file(filename, data_arg):
"""
Reads the data from a file and passes it into the
function as the kwarg defined in the data_arg parameter.
:param filename: The filename to read the data from.
:param data_arg: The argument to pass in the data as.
:return: A new with the data already passed in.
"""
def readfile_decorator(input_func):
@arlyon
arlyon / eratosthenes.py
Last active August 21, 2020 12:14
A 2-cycle fast and pythonic implementation of the sieve or eratosthenes.
from itertools import count
from collections import defaultdict
def eratosthenes():
"""
Infinitely generates primes using the sieve of Eratosthenes.
"""
yield 2
candidate_factorizations = defaultdict(set)
@arlyon
arlyon / composed_serializer.py
Created April 1, 2018 11:12
Allows composing serializers together in DRF.
def composed_serializer(serializer):
"""
A decorator which, when applied to a serializer composed of
multiple other serializers, generates a Meta that merges the fields.
:param serializer: The composed class to generate Meta for.
:return: A new class with correctly defined Meta.
"""
combined_fields = {x for x in itertools.chain(*(x.Meta.fields for x in serializer.__bases__))}
if serializer.Meta:
@arlyon
arlyon / n_closest_points.py
Created April 6, 2018 13:03
Gets the n closest points to a target point.
from typing import List, Tuple
from math import sqrt
from collections import defaultdict
from itertools import chain
def n_closest_points(n: int, target: Tuple[int, int], points: List[Tuple[int, int]]) -> int:
lengths = defaultdict(list)
for point in points:
@arlyon
arlyon / unique_null.py
Created April 8, 2018 11:07
A Django foreign key that modifies None values to -1 so that the unique constraint in MySQL is respected.
class UniqueNullForeignKey(models.ForeignKey):
description = "Foreign Field that stores None as -1 to enforce the unique constraint."
def from_db_value(self, value, expression, connection, context):
"""
Intercepts the data being loaded and replaces -1 with None
for proper use in the application.
"""
return None if value == -1 else value
@arlyon
arlyon / fizzbuzz.py
Created May 3, 2018 17:56
Infinite Legible Fizzbuzz
from itertools import count; [print("fizz" * (not x % 3) + "buzz" * (not x % 5) or x) for x in count(1)]