R

@RealSaitama

Sum of products

Python
3 months ago
from itertools import combinations from math import prod def ok(cs): n = len(cs) for i in range(0, n - 1, 2): if cs[i] % 2 == 1 or cs[i + 1] % 2 == 0: return False if cs[-1] % 2 == 1: return False

Intro VA

Python
3 months ago
from random import randint n = 1000 dct = {"PP": 0, "FF": 0, "PF": 0, "FP": 0} gain_dct = {-2: 0, 6: 0, 2 : 0} gain_total = 0 for i in range(n):

Mean Distance Circle

Python
3 months ago
from random import random from math import pi, exp, cos, sin n = 1000000 c = 0 for _ in range(n): x = random() r = abs(1 - complex(cos(2*pi*x), sin(2*pi*x))) c += r f = c / n

Tonelli Shanks

Python
3 months ago
def solve(a, p): r = pow(a, (p - 1) // 2, p) if (r + 1) % p == 0: return None print("ok", r) if p % 3 == 4: return pow(a, (p + 1) // 4, p) b = bin(p - 1)[2:] s = 0 n = len(b)

Des chiffres et des lettres

Python
3 months ago
from itertools import permutations def solve(ns, target): if len(ns) == 1 and target == ns[0]: return str(ns[0]) if len(ns) == 2: x, y = ns[0], ns[1] if x + y == target: return (True, f"({x}+{y})") elif x > y and x - y == target:

Ruler

Python
3 months ago
def ruler(t): m = 2**(t-1) + 1 lst = [''] * m lst[0] = lst[m - 1] = '-' * t def rec(a, b, s): m = (a + b) // 2 lst[m] = '-' * (s - 1) if s > 2: rec(a, m, s - 1)

Lady Bug

Python
4 months ago
from random import randint N = 6 n = 10000 def nxt(x, N): sgn = randint(0, 1) * 2 - 1 y = (x + sgn) % N return y if y != 0 else N

3d walk

Python
4 months ago
import numpy as np from scipy.integrate import quad from scipy.special import ive def G_func(point): x, y, z = np.abs(point) # The integrand is stable using ive (exponentially scaled Bessel) def integrand(t): return ive(x, t/3) * ive(y, t/3) * ive(z, t/3)

SAT codewars

Python
4 months ago
import sys # Increase recursion depth for large puzzles sys.setrecursionlimit(2000) def solve(cells): n = len(cells) # We map values 1...n to indices 0...n-1 # Graph: Left side (cells 0 to n-1), Right side (values 0 to n-1)

Attribute Counter Access

Python
4 months ago
def track_attributes(cls): class TrackedWrapper: def __init__(self, value, count): self.__dict__['_value'] = value self.count = count def __getattr__(self, name): return getattr(self._value, name) # Basic "Classical" representation

Hyper Sum

Python
4 months ago
M = 1_000_003 def f(n, k): i = pow(pow(n, k, M) - 1 + M, M - 2, M) return (n - pow(n, k, M) - pow(n, (1 + k), M) + pow(n, (k *(1 + n)), M)) * (pow(i, 2, M)) def g(n, k): return (n - n**k -n**(1+k)+n**(k*(1+n))) //(1-n**k)**2 def _solve(n): m = pow(n, 1 + n, M)

Max Clique

Python
5 months ago
from random import randint def ramsey(G): if not G: return set(), set() idx = randint(0, len(G) - 1) nodes = list(G.keys()) node = nodes[idx] neighbors = (nbr for nbr in G[node] if nbr != node)

Astar Algorithm

Python
5 months ago
from heapq import heappush, heappop from itertools import count DIRS = ((1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (1, -1), (-1, 1), (-1, -1)) def distance(a, b): return sum(abs(x - y) for x, y in zip(a, b)) def astar_path(G, source, target, heuristic): n, m = len(G), len(G[0])

Pick Until Match (Codewars)

Python
5 months ago
from functools import cache, reduce from math import gcd from fractions import Fraction lcm = lambda xs: reduce(lambda acc, x: acc * x // gcd(acc, x), xs) @cache def dp(tup): m = len(tup) idx = next((i for i in range(m) if tup[i] == 0), None) if idx is not None:

Networkx

Python
5 months ago
from collections import deque from heapq import heappush, heappop demand, capacity, weight = 'demand', 'capacity', 'weight' def _dijkstra(G, source, get_weight, pred=None, paths=None, cutoff=None, target=None): G_succ = G.succ if G.is_directed() else G.adj push = heappush pop = heappop dist = {} # dictionary of final distances

AL KINDI 2023

Python
5 months ago
d={'A':'P','B':'E','C':'I','D':'G','E':'V','F':'U','G':'K','H':'X','I':'A','J':'S','K':'N','L':'Z','M':'Y','N':'J','O':'M','P':'F','Q':'H','R':'C','S':'B','T':'R','U':'O','V':'T','W':'L','X':'D','Y':'W','Z':'Q'} d={'A':'T', 'B':'K','C':'F','D':'R','E':'U','F':'D','G':'V','H':'X','I':'L','J':'M','K':'G','L':'C','M':'Q','N':'A','O':'S','P':'I','Q':'B','R':'P','S':'E','T':'N','U':'O','V':'Y','W':'Z','X':'J','Y':'W','Z':'H'} d = {v:k for k, v in d.items()} def sub(c, k): ch = c for _ in r

Look and Say

Python
6 months ago
s = "11" cnt = {'1':2} from collections import defaultdict def _next(c): _c = dict(c) for k, v in c.items(): for a in str(v): _c[a] = _c.get(a, 0) + 1 _c[k] = _c.get(k, 0) + 1

Break The pieces

Python
6 months ago
def scale_hrz(s): scaled = [] for i, row in enumerate(s): r = "" for j in range(len(row) - 1): if row[j] == '+' and row[j + 1] == '+': r += '+-' elif row[j] == '+' and row[j + 1] == ' ': r += '+ ' elif row[j] == '+' and row[j + 1] == '-':

AL KINDI 2024

Python
6 months ago
s = "CRKQDEIVPIMPAIXZNONYTJUSWIKSLUSEAQGFWMAYBOHUUVAMZTVICKYQURVKJHIUTEFHIATEYLBEDXCMWRPOIAYZXALIRGESSSTOLPBEARYPEPHNJTZEYLUXHJHHDIOMQUYUTYXIGNSHOTTEANOCEMFXOBELMWDNUFUGHJINUCPIOHSGJTXONAIPCWWPFDRIXOPMBPOVEWOPYILQLGATOZEUDXXHIUVQUZQPFJ" ds = ('ZERO', 'UN', 'DEUX', 'TROIS', 'QUATRE', 'CINQ', 'SIX', 'SEPT', 'HUIT', 'NEUF') # for x in range(13): # for y in range(16): # for z in range(19): # r = "" # for i, c in enumerate(s): # if (i + 1) % 13 =

JS SAT solver

NodeJS
6 months ago
/* * sat.js * (C) 2012, all rights reserved, * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful,