Skip to content

Instantly share code, notes, and snippets.

@serg06
serg06 / interval_map.h
Last active January 22, 2024 09:03
[C++] Short IntervalMap data structure implementation
/*
* NOTE 1: This works on VC++ but might need a little extra syntax to work on GCC.
* NOTE 2: It breaks when calling set_interval on the minimum key (std::numeric_limits<K>::lowest()) and maybe on the maximum key too.
*
* OPERATIONS:
*
* N = number of unique intervals. (Neighboring intervals with the same value are joined.)
* Iterators run in key-sorted order. (Or reverse, if you like - they're bidirectional.)
*
* get_min(): O(1)
@serg06
serg06 / main.py
Created December 28, 2022 22:12
Python script for pinging Trusted Traveler Program website for appointments and notifying you by text
import time
from twilio.rest import Client
import requests
from dateutil import parser
# Create client
account_sid = '' # TODO: Grab from Twilio
auth_token = '' # TODO: Grab from Twilio
client = Client(account_sid, auth_token)
@serg06
serg06 / load-script.js
Last active July 2, 2023 01:13
[JS] Load scripts dynamically
// Function to load a JS file
async function loadScript(url) {
return await new Promise((resolve, reject) => {
const script = document.createElement('script');
script.src = url;
script.addEventListener('load', () => resolve());
script.addEventListener('error', () => reject());
document.body.appendChild(script);
});
}
'use strict'
const { pathToFileURL } = require('node:url')
// node_modules/lambda-runtime/dist/node16/UserFunction.js
;(function () {
const __getOwnPropNames = Object.getOwnPropertyNames
const __commonJS = (cb, mod) =>
function __require() {
return (
@serg06
serg06 / main.cpp
Last active December 20, 2022 08:31
cppzmq (libzmq) multi-threaded pub-sub example on Windows 10 VS 2019
#include <future>
#include <iostream>
#include <string>
#include "zmq.hpp"
void PublisherThread(zmq::context_t* ctx)
{
// Prepare publisher
zmq::socket_t publisher(*ctx, zmq::socket_type::pub);
from scipy import ndimage
import numpy as np
from PIL import Image
from random import randint
from copy import deepcopy
# set the file path to wherever your provinces.png is located
im = Image.open(r"colors.png")
print('-------------------------------------------')
# DEBUGGING: simply prints the format, size, and mode of your file
@serg06
serg06 / iprange.py
Last active April 16, 2022 19:57
[Python] Get all IPs in a range
import itertools as it
"""
Given a start/end IP, return all IPs in that range (inclusive)
"""
def ipRange(start_ip: str, end_ip: str):
start = [int(x) for x in start_ip.split('.')]
end = [int(x) for x in end_ip.split('.')]
yield start_ip
@serg06
serg06 / powerMod.ts
Last active April 12, 2022 02:01
[TypeScript] powerMod implementation
// x^p mod m
function powerMod(x: number, p: number, m: number): number {
if (m === 1) {
return 0;
}
x = x % m;
let result = 1;
while (p > 0) {
if (p % 2 === 1) {
@serg06
serg06 / regex.ts
Last active April 9, 2022 05:39
[TypeScript] Types for JavaScript's replacerFunction in String.prototype.replaceAll
export interface ReplacerArgs {
match: string; // the matched substring
groups: string[]; // captured groups
offset: number; // offset of match
string: string; // entire string
named_groups?: Record<string, string>; // named capturing groups
}
function parseReplacerArgsMutating(args: [string, ...any[]]): ReplacerArgs {
const named_groups = typeof args[args.length - 1] === 'object' ? (args.pop() as Record<string, string>) : undefined;
@serg06
serg06 / enums.ts
Last active April 9, 2022 05:39
[TypeScript] Iterating over enums
function isStringEnum<T extends Record<string, string | number>>(t: T): boolean {
return typeof Object.values(t).pop() === 'string';
}
export function enumKeys<T extends Record<string, string | number>>(t: T): (keyof T)[] {
return isStringEnum(t) ? Object.keys(t) : Object.keys(t).slice(0, Object.keys(t).length / 2);
}
export function enumValues<T extends Record<string, string>>(t: T): string[];
export function enumValues<T extends Record<string, string | number>>(t: T): number[];