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 benchmarks[A](f: => A) = | |
inline def memory() = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() | |
val t0 = System.nanoTime() | |
val m0 = memory() | |
val ret = f | |
val t1 = System.nanoTime() | |
val m1 = memory() |
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
import numpy as np | |
import sys | |
sys.set_int_max_str_digits(0) | |
def matrix_power(A: np.matrix, n: int) -> np.matrix: | |
if n == 0: | |
return np.matrix( ((1,0),(0,1)), dtype=object ) | |
elif n%2 == 1: |
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
import time | |
import functools | |
def timer_decorator(func): | |
@functools.wraps(func) | |
def wrapper(*args, **kwargs): | |
start = time.time_ns() | |
res = func(*args, **kwargs) | |
end = time.time_ns() | |
wrapper.last_execution_time = end - start |
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
val fibs: LazyList[BigInt] = BigInt(1) #:: BigInt(1) #:: (fibs zip fibs.tail).map(_+_) | |
val fibs: LazyList[BigInt] = 1 #:: fibs.scan(BigInt(1))(_+_) | |
val lucas: LazyList[BigInt] = 2 #:: fibs.scan(BigInt(1))(_+_) |
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 pathlib import WindowsPath | |
class LongPath(WindowsPath): | |
uncprefix = "\\\\?\\" | |
def __new__(cls, *pathsegments): | |
ins = super().__new__(cls, *pathsegments) | |
abspath = ins.expanduser().absolute() | |
match abspath.parts: | |
case (drive, *parts) if not drive.startswith(cls.uncprefix): |
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, takewhile | |
def calc_pi(error: float = 1e-6) -> float: | |
terms = (1 / (2 * k - 1) for k in count(1)) | |
it = takewhile(lambda x: 2 * x >= error, terms) | |
ts = list(x - y for x, y in zip(it, it)) | |
pi = 4 * sum(ts) | |
calc_pi.error = error |
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
import sys | |
from bisect import bisect, bisect_left | |
from collections.abc import Generator, Iterable, Iterator | |
from functools import singledispatchmethod | |
from itertools import islice | |
from math import isqrt | |
INFINITE = sys.maxsize # una mala aproximación de infinito | |
Prime = int # un alias para los primos |
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
module Fmt exposing (..) | |
import String | |
type FmtPadding | |
= Default | |
| Left Int Char | |
| Right Int Char | |
| Center Int Char |
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
# -*- coding: utf-8 -*- | |
""" | |
Created on Fri Jun 12 18:29:33 2015 | |
@author: chema | |
""" | |
from concurrent.futures import ( | |
Future, ProcessPoolExecutor, ThreadPoolExecutor, TimeoutError, as_completed) | |
from itertools import islice |
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 birthday(n: Int) = { | |
1 - (0 until n).map(1 - BigDecimal(_) / 365).product | |
} | |
assert(birthday(23) == BigDecimal("0.5072972343239854072254172283370325")) |
NewerOlder