Skip to content

Instantly share code, notes, and snippets.

View AssortedFantasy's full-sized avatar
😎
Semiconductors are bae

Jehanzeb Mirza AssortedFantasy

😎
Semiconductors are bae
  • Canada
View GitHub Profile
@AssortedFantasy
AssortedFantasy / UnionFind.c
Last active September 15, 2023 22:06
This is useful for Leetcode problems.
// free with free(arr);
static int* new_union(int n) {
int* arr = malloc(sizeof(int)*n);
for (int i=0; i<n; i++) arr[i] = i;
return arr;
}
// Implements path compression
static int get_parent(int* arr, int i) {
if (arr[i] == i) return i;
@AssortedFantasy
AssortedFantasy / ZIG_UPDATE.bat
Last active September 22, 2023 23:12
Automatically update zig binary to zig x86_64 master.
@ECHO OFF
REM Automatic Zig Download and Install Script
REM Add the variable (as a variable) %ZIGPATH% to user path for usage.
REM Requires jq to be installed.
SET zigurl=https://ziglang.org/download/index.json
SET zigjson=.\zigmaster.json
ECHO Fetching Zig Master .json
curl -o %zigjson% %zigurl%
@AssortedFantasy
AssortedFantasy / upper_lower_bound.c
Created June 9, 2023 19:14
C Upper and Lower Bound Implementations
/*
Upper and lower bounds.
Upper bound: First where target < array[i]
Lower bound: First where !(array[i] < target)
Assume a 3 way comparision returns this.
[-1 -1 -1 -1 0 0 0 0 0 1 1 1 1 1]
^ Lower bound. ^
Upper bound.
*/
@AssortedFantasy
AssortedFantasy / triangleindex.cpp
Created May 28, 2023 18:08
Triangular Index Helpers
// Triangle indexes
// if you have a set of N items with indexes.
// 0,1,2,3, ... , N-1
// You want a linear index for every pair i,j, i != j.
// N Choose 2.
inline int triangleSize(int N) {
return N*(N-1)/2;
}
@AssortedFantasy
AssortedFantasy / ndarray.zig
Last active November 26, 2021 20:59
A minimal implementation of higher dimensional slices in Zig.
// Multi Dimensional Slices in Zig
// Sort of akin to ndarrays in Python's numpy
const std = @import("std");
const runtime_safety = std.debug.runtime_safety;
const mem = std.mem;
const NDSliceErrors = error{
InsufficientBufferSize,
ZeroLengthDimensionsNotSupported,