R

@RealSaitama

Hilbert Curve

Python
1 year ago
xs = [' ', '│', '─', '┌', '┐', '┘', '└'] lt = {0:0,1:2,2:1,3:4,4:5,5:6,6:3} rt = {0:0,1:2,2:1,3:6,4:3,5:4,6:5} def rotate_left(xs): n, m = len(xs), len(xs[0]) return [[lt[xs[n - 1 - i][j]] for i in range(n)] for j in range(m)] def rotate_right(xs): n, m = len(xs), len(xs[0])

Closure set generator

Python
1 year ago
xs = [2, 7] import heapq as hq def closure_gen(xs): xs = sorted(set(xs)) # Ensure no duplicates and ascending order seen = set(xs) # Track seen elements to avoid duplicates heap = list(xs) # Initialize the heap with the base elements hq.heapify(heap) # Convert list to a min-heap print(heap)

Complexes Maths expertes

Python
1 year ago
def conj(u): return (u[0], -u[1]) z1 = (3, -2) z2 = (1, -4) print(conj(z1)) def add(u, v): return (u[0] + v[0], u[1] + v[1]) print(add(z1, z2))

Binary encoding of strings

Python
1 year ago
from fractions import Fraction def frac_to_binary(f): x = Fraction(2, 1) while f > 0: r = f * x if r.numerator >= r.denominator: f -= Fraction(1, x) yield '1' else:

Recherche Streetview

Python
1 year ago
s=''' Acacias (Rue des) Albert Camus (Rue) Alexandre Volta (Rue) Alsace (Rue d') Ampere (Rue) Anemones (Rue des) Aristide Briand (Rue) Beethoven (Rue) Berlioz (Rue)

Pareto Front

Python
1 year ago
xs = [ (120, 50), (80, 75), (150, 100) ] def solve(lst): n = len(lst) rem = set() for i in range(n): for j in range(n): if i != j and all(x <= y for x, y in zip(lst[i], lst[j])) and any(x < y for x, y in zip(lst[i], lst[j])): rem.add(i) break return set(lst) - set([lst[x] for x in rem])

Dichotomy II

Python
1 year ago
from math import exp def f(x): return 1 + (-4 * x**2 - 10 * x + 8) * exp(-0.5 * x) a, b = -4, -2 k = 1 while b - a > 10**(-k): m = (a + b) / 2 p = f(a) * f(m)

Dichotomy I

Python
1 year ago
from math import exp def f(x): return x * exp(x) def dicho(a, b, eps): while b - a > eps: m = (a + b) / 2 if f(m) > 1: b = m

Maximize earnings

Python
1 year ago
def max_sum_with_breaks(arr, k): n = len(arr) dp = [-float('inf')] * n # Initialize dp with -inf to find the maximum dp[0] = arr[0] # Base case: first element can only be taken by itself for i in range(1, n): current_sum = 0 # Consider taking up to k successive elements ending at index i for j in range(k): if i - j >= 0: # Ensure we're within bounds

Even Gray Code

Python
1 year ago
from itertools import combinations def solve(n): seq = [[0] * n] while 1: m = (1 << n) - 1 for p in range(1, n // 2 + 1): l = 2 * p for c in combinations(range(n), r=l): lst = seq[-1][:]

Spiral

Python
1 year ago
pts = {} pts[(0, 0)] = 0 x, y = 0, 0 dx, dy = 1, 0 i, c = 0, 0 while c < 10**2: for _ in range(2): for j in range(i + 1): c += 1 x += dx

Chunky strings II

Python
1 year ago
def f(s): if s == "": return s elif len(set(s)) == 1: return s[0] n = len(s) // 2 l, r = s[:n], s[n:] return f"[{f(l)}{f(r)}]" def g(s, k):

Gaussian Jordan Elimination

NodeJS
1 year ago
function gauss(a){ let arr = a.map(r => r.slice()); let [n, m] = [arr.length, arr[0].length]; let [h, k] = [0, 0]; while(h < n && k < m){ let idx = h; while(idx < n && arr[idx][k] == 0) idx++; if(arr[idx][k] == 0){ k++; } else {

Gauss Elimination

Python
1 year ago
from fractions import Fraction tests = ('1 2 0 0 7\n0 3 4 0 8\n0 0 5 6 9', '1 5/2 1/2 0 4 1/8\n0 5 2 -5/2 6 2', '1/20 -10/3 -10/9 -13\n-29 8 -27/4 0\n-26 -14 25 10/7', '1 1 2\n 2 1 -1\n1 -1 0', '0 0 1 2 1\n1 2 1 3 1\n1 2 2 5 2' ) def gauss(arr):

Unix Timestamp

Python
1 year ago
xs = [9999, 12, 31, 23, 59, 59] import datetime def time_stamp(t): return datetime.datetime(*t).timestamp() def get_days(y, m, d): def is_leap(year): return (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0) D1 = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]

Markov

Python
1 year ago
from random import randint N = 10**4 n = 3 for m in range(n, 10): e = 0 for _ in range(N): xs = [None] * n cnt = 0 while any(x is None for x in xs):

Latex To Markdown

Python
1 year ago
arr = [] while 1: try: arr.append(input()) except: break s = '\n'.join(arr) import re xs = (

Max Rectangle Area

Python
1 year ago
def solve(n, m, a, p): g = [[1 if (i, j) in set(p) else 0 for j in range(m)] for i in range(n)] dp = [[0] * (m + 1)] for i in range(1, n + 1): r = [0] for j in range(1, m + 1): r.append(dp[i - 1][j] + r[j - 1] - dp[i - 1][j - 1] + g[i - 1][j - 1]) dp.append(r) for r in dp: print(r)

Maximum number of divisors

Python
1 year ago
import requests from math import prod from collections import Counter import bisect req = requests.get("https://oeis.org/A002182/b002182.txt") data = req.text seq = [] for row in data.split('\n'): _, v = map(int, row.split())

Python Minesweepers

Python
1 year ago
class Constraint: def __init__(self, cells, total): self.cells = cells self.total = total def __repr__(self): return f"cells : {self.cells}\ntotal: {self.total}" def length(self): return len(self.cells)