Skip to content

Instantly share code, notes, and snippets.

View cosmicproc's full-sized avatar

Cosmic Process cosmicproc

View GitHub Profile
@cosmicproc
cosmicproc / simplegpt.py
Last active September 10, 2025 14:06
Simple GPT
import lightning as L
import torch
from datasets import concatenate_datasets, load_dataset
from lightning.pytorch.callbacks.early_stopping import EarlyStopping
from lightning.pytorch.callbacks.model_checkpoint import ModelCheckpoint
from tokenizers import Tokenizer
from tokenizers.models import BPE
from tokenizers.pre_tokenizers import Whitespace
from tokenizers.trainers import BpeTrainer
@cosmicproc
cosmicproc / tic_tac_toe.py
Created February 11, 2025 10:36
Tic Tac Toe (Minimax)
size = 4
winning_pairs = [
*[[(i, j) for j in range(size)] for i in range(size)],
*[[(j, i) for j in range(size)] for i in range(size)],
[(i, i) for i in range(size)],
[(i, (size - 1) - i) for i in range(size)],
]
possible_moves = [(i, j) for j in range(size) for i in range(size)]
@cosmicproc
cosmicproc / det.py
Created December 10, 2024 10:44
Python cofactor expansion determinant
from random import randint
def row(m, i):
return m[i]
def col(m, i):
return [j[i] for j in m]
@cosmicproc
cosmicproc / det.ts
Created December 10, 2024 10:40
TypeScript concise and fast determinant
type vector = number[];
type matrix = vector[];
function interchange(m: matrix, from: number, to: number) {
[m[to], m[from]] = [m[from], m[to]];
return m;
}
function addition(m: matrix, from: number, to: number, c: number) {
m[to] = m[to].map((e, i) => e + c * m[from][i]);
@cosmicproc
cosmicproc / smartquotes.py
Created February 20, 2024 17:40
Smart quote conversion for text files
import re
import sys
def smart_quotes(content):
content = re.sub(r"\"(.*?)\"", r"“\1”", content)
content = re.sub(r"'(.*?)'", r"‘\1’", content)
content = re.sub(r"'", r"’", content)
return content
@cosmicproc
cosmicproc / extract_variables.py
Last active January 24, 2024 09:25
Extract variable names from a file
from pathlib import Path
from sys import argv
def main():
if len(argv) < 2:
print("Please specify a file path as an argument.")
exit(1)
elif len(argv) > 2:
print("Unknown options: " + " ".join(argv[2:]))
@cosmicproc
cosmicproc / countlines.py
Last active January 22, 2024 12:23
Count lines in a file or directory
from sys import argv
from pathlib import Path
def main():
if len(argv) < 2:
print("Please specify the directory or file to count the lines of as an argument.")
exit(1)
elif len(argv) > 2:
print("Unknown options: " + " ".join(argv[2:]))