R

@RealSaitama

Sequence generator

Python
8 months ago
from itertools import count, islice, product as P def only(s, D=1): # numbers with >= D digits only from s yield from (int("".join(p)) for d in count(D) for p in P(s, repeat=d)) def agen(): # generator of terms aset, an, minan = {0}, 0, 1 while True: yield an an, s = minan, set(str(an))

Strategy

Python
8 months ago
import numpy as np import scipy.stats as st import matplotlib.pyplot as plt def compute_thresholds_normal(n, x_min=-6, x_max=6, dx=0.01): """ Compute optimal stopping thresholds t_i for selecting the maximum of n i.i.d. N(0,1) draws using dynamic programming. """ xs = np.arange(x_min, x_max+dx, dx)

Geometric problem (ladder)

Python
8 months ago
l = 7 c = 2.4 r = (c**2+l**2)**0.5 x = c + r y=c*x print(x, y) d = l**2 -2*c**2-2*c*r print(d, r) x1 = (c+r +d**0.5) /2 x2 = (c+r -d**0.5) /2

Maximizer

Python
8 months ago
import numpy as np import scipy.optimize as opt from scipy.optimize import differential_evolution, basinhopping import itertools from typing import List, Tuple, Dict, Any import time class FunctionOptimizer: """ Optimizer for the function:

Mysterious Cipher

Python
8 months ago
ts = [ ('this will probably not be fun', 'tiks zjop twrggfrf uwz kl pcx'), ('hopefully you will find satisfaction at least', 'hprehxmoc ctx cmrs mqtl zjdqcqjnfsaa nh ztnhj'), ('really this is very tedious', 'rfclnb wlkw lx zkyd bklrvdc') ] 0,1,2, 0,2,3, 1,3,4, 2,4,5, 3,5,6, 4,6,7, 5,7,8, 6,8,9, 7,9,10 def gen(): i = 0 while 1: if i < 3:

Bi-tangents

Python
8 months ago
from math import hypot, acos, cos, sin, pi, atan2 class Segment: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 def length(self): return hypot(self.p1.x - self.p2.x, self.p1.y - self.p2.y)

Euclidean MST (minimum spanning tree)

Python
8 months ago
from math import dist import heapq as hq from scipy.spatial import Delaunay # Requires scipy def bridge(pts): n = len(pts) if n <= 1: return 0 # Compute Delaunay triangulation

Prim Algorithm (Fibonacci Heap)

Python
8 months ago
import math from typing import List, Tuple, Optional, Dict class FibonacciHeapNode: """Node in a Fibonacci heap.""" def __init__(self, key: float, value: int): self.key = key self.value = value self.parent: Optional['FibonacciHeapNode'] = None

Kruskal c++

C++
8 months ago
#include <iostream> #include <vector> #include <algorithm> std::vector<int> parent, rank; void make_set(int v) { parent[v] = v; rank[v] = 0; }

Kruskal (disjoint sets)

Python
8 months ago
class Edge: def __init__(self, u, v, weight): self.u = u self.v = v self.weight = weight def __str__(self): return f"({self.u},{self.v},{self.weight})" def __lt__(self, other):

Minimum Spanning Tree (Kruskal)

Python
8 months ago
class Edge: def __init__(self, u, v, weight): self.u = u self.v = v self.weight = weight def __str__(self): return f"({self.u},{self.v},{self.weight})" def __lt__(self, other):

Trampoline Code Optimization

NodeJS
8 months ago
// Transform function definitions to support tail call optimization via trampolines function transformToTCO(functionDefs) { const functions = new Map(); // Parse function definitions functionDefs.forEach(([name, params, body]) => { functions.set(name, { params, body }); }); // Transform each function body to use trampolines

Max sum contiguous Submatrix (Kadane)

NodeJS
8 months ago
tests = [ [ [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ], 45 ], [ [

Switch Bulbs (Hamitonian Path)

Python
8 months ago
# Claude AI Hamiltonian path finder class HamiltonianPathSolver: def __init__(self, graph): self.graph = [list(graph[k]) for k in sorted(graph.keys())] # adjacency list representation self.n = len(graph) self.forced_start = None self.forced_end = None def find_hamiltonian_path(self): """Main function to find Hamiltonian path with preprocessing"""

Jordan

Python
8 months ago
from fractions import Fraction arr = [[1, 1, 1, 1, 0, 0, 1, 0, 0, 0] , [1, 1, 1, 0, 1, 0, 0, 1, 0, 1] , [1, 1, 1, 0, 0, 1, 0, 0, 1, 0] , [1, 0, 0, 1, 1, 1, 1, 0, 0, 1] , [0, 1, 0, 1, 1, 1, 0, 1, 0, 1] , [0, 0, 1, 1, 1, 1, 0, 0, 1, 1] , [1, 0, 0, 1, 0, 0, 1, 1, 1, 0] , [0, 1, 0, 0, 1, 0, 1, 1, 1, 1] ,

Advanced binary toggling puzzle

Python
8 months ago
tests = [ [[ [1, 0, 1], [0, 0, 0], [1, 0, 1] ],[(1, 1)]], [[ [0, 1, 0], [1, 0, 0], [0, 0, 1]

Segment Tree

Python
8 months ago
op = lambda x, y: x + y tree = {} def build(node, L, R): if L == R: tree[node] = xs[L] else: mid = (L + R) // 2 build(2 * node, L, mid) build(2 * node + 1, mid + 1, R)

Largest Palindromic product

Python
8 months ago
arr = [] a, b = 1234, 4321 # for x in range(1, a + 1): # for y in range(1, b + 1): # arr.append((x * y, x, y)) # arr.sort(key=lambda x: -x[0]) def makePalindrome(number): num_str = str(number) digits = len(num_str)

Number concatenation

Python
9 months ago
tests = [ (4748, 47), (123, 122), (5347, 3475), (121, 12), (213, 12), (321, 132), (7234723, 7234), (122, 21), (12, 1),

Associative operation

Python
9 months ago
xs = [2, 1, 5, 3, 4] op = lambda x, y: x + y tree = {} def build(node, L, R): if L == R: tree[node] = xs[L] else: mid = (L + R) // 2 build(2 * node, L, mid)