This file contains 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
/* 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 |
This file contains 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
/** | |
* 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. |
This file contains 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 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 |
This file contains 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 Callable | |
def memoize(func: Callable) -> Callable: | |
solutions = {} | |
def new_func(*n): | |
if n not in solutions: | |
solutions[n] = func(*n) | |
return solutions[n] |
This file contains 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 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): |
This file contains 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 itertools import count | |
from collections import defaultdict | |
def eratosthenes(): | |
""" | |
Infinitely generates primes using the sieve of Eratosthenes. | |
""" | |
yield 2 | |
candidate_factorizations = defaultdict(set) |
This file contains 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 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: |
This file contains 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, 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: |
This file contains 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
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 |
This file contains 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 itertools import count; [print("fizz" * (not x % 3) + "buzz" * (not x % 5) or x) for x in count(1)] |
OlderNewer