Skip to content

Instantly share code, notes, and snippets.

@juliusgeo
juliusgeo / longest_palindromic_substring.c
Last active January 11, 2021 18:14
Implementing longest palindromic substring algorithm using pthreads as practice
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
int sum;
struct input {
char* str;
int i;
};
int NUM_THREADS;
from dataclasses import dataclass
from functools import reduce
@dataclass
class bff:
"""
An implementation of binary finite field arithmetic on fields of order 2^n using integers and bitwise operators.
Adaptation of algorithms in:
@juliusgeo
juliusgeo / pi_arb.py
Created January 17, 2023 21:10
Pi with arbitrary precision using Bernoulli numbers (the slow way)
from functools import reduce
from math import factorial, comb
from decimal import getcontext, Decimal as Dec
from timeit import timeit
def bernoulli(n):
bs = [Dec(1)]
for m in range(1, n+1):
bs.append(1 - sum(comb(m, k)*b / (m - k + 1) for k, b in zip(range(m), bs)))
from functools import reduce as red; from math import \
(factorial as fact, comb);import sys;from decimal import \
(getcontext as c,Decimal as dc);(a:=range,
b:=int(sys. argv[1]));c\
().prec=b; ber=lambda\
e,f=[dc(1)]: [f.append(
1-sum(comb(h ,g)*f[g]/(h
- g + 1) for g in a(h)))for
h in a(1, e + 1)] and abs(
f[-1]);print ((2 * fact(b) /
l=lambda\
x0, y, x1, y1: \
((dx := x1 - x0, dy :=
y1-y,yi:=1,yi:=-1 if dy<0 else yi,
dy:=-dy if dy<0 else dy,d:=(2*dy)-dx,ps:=
set(),[(ps.add((x,y)),y:=y+yi,d:=d+2*(dy-dx))if d>
0 else(d:=d+2*dy)for x in range(x0,x1)]),ps)[1];pp=lambda \
ps:list(map(print, [''.join(["-"if(x, y)in ps else" "for x in range
(127)])for y in range(10)]));pp(set.union(*[l(0, 0, i, 9) for i in range(37, 127)]))
@juliusgeo
juliusgeo / ioccc_pi.py
Last active March 10, 2023 00:43
IOCCC 1988 Underscore Pi Python Recreation
print(''.join(b:=open(__file__).readlines()[1:]).count("#")/len(b)/4)
#######
#################
#######--------########
######-------------#######
######-----------------######
#####------------------######
######-------------------######
#####--------------------######
#####---------!\---------######
@juliusgeo
juliusgeo / mongo_pi.py
Created March 10, 2023 22:16
Pi In MongDB Aggregations
from pymongo import MongoClient
from decimal import localcontext
from math import sqrt, log2
from bson import Decimal128
from bson.decimal128 import create_decimal128_context
coll = MongoClient().db.coll
coll.drop()
def Dec(x):
with localcontext(create_decimal128_context()) as ctx:
return Decimal128(ctx.create_decimal(x))
@juliusgeo
juliusgeo / typyngtest.py
Last active April 16, 2023 02:35
A pretty nice typing test in 80 lines of Python.
import os,select,tty,sys
from time import time
from functools import partial
# Need this because Unix-esque OSes are different from Windows.
try:
from msvcrt import getch as cf
except ImportError:
tty.setcbreak(sys.stdin.fileno())
cf=partial(sys.stdin.read,1)
n=256;q, t, m, e, p, c, f=(range(n),16,lambda x
,y, r=0:m((h:=x <<1,h^283)[
h&n!= 0],y>> 1,(r,r ^x)
[y&1])if y else r,lambda
a,w= 1,p=n -2:e(m(a, a),(w,m
(w,a))[p&1] ,p>> 1)if p
else w, lambda b: list(
map (print,["%.2x "*t%(*b[r:r+t],)for
r in [*q][::t]])),lambda a,i: (a<<i|a>>8-i)&255
,lambda a: (a^c(a,1)^c(a,2)^c(a,3)^c(a,4))^99);
@juliusgeo
juliusgeo / bom.py
Last active May 16, 2023 10:07
Constrict Your Ram With Python
from multiprocessing import Process
import psutil
from time import sleep
def ascii_bomb():
return [
"""
.,-:-,,.