Skip to content

Instantly share code, notes, and snippets.

View chris3k's full-sized avatar

kg chris3k

  • Oracle
View GitHub Profile
@chris3k
chris3k / pseudo_random_iter.cpp
Created January 23, 2024 13:29
get every number in [0, n) range only once, in pseudo-random order
#include <iostream>
#include <numeric>
//
// `ax + b mod n`, where `[0,n)`
// `a` must be a coprime with `n`,
// `gcd(a, n) = 1`
// `a=1` is bad for obvious reasons, so we gonna pick `a` from `[n/2, n)`.
//
@chris3k
chris3k / udping.py
Last active October 19, 2016 23:17
Send UDP packet over linux raw socket
#!/usr/bin/env python2.7
import socket
import struct
import random
def int_to_ip(i):
return ".".join(map(lambda x: str(x & 0xff), (i >> 24, i >> 16, i >> 8, i)))
@chris3k
chris3k / wcpp.cpp
Created October 18, 2016 21:57
word count c++
#include <algorithm>
#include <fcntl.h>
#include <iostream>
#include <string.h>
#include <unistd.h>
using namespace std;
constexpr int BUFFER_SIZE = 1024 * 1024;
size_t line_count(const char *filename) {
@chris3k
chris3k / special_rsa.py
Created March 19, 2016 16:32
Special RSA 200
import os, sys
from key import k, random_r
import msgpack
N = 23927411014020695772934916764953661641310148480977056645255098192491740356525240675906285700516357578929940114553700976167969964364149615226568689224228028461686617293534115788779955597877965044570493457567420874741357186596425753667455266870402154552439899664446413632716747644854897551940777512522044907132864905644212655387223302410896871080751768224091760934209917984213585513510597619708797688705876805464880105797829380326559399723048092175492203894468752718008631464599810632513162129223356467602508095356584405555329096159917957389834381018137378015593755767450675441331998683799788355179363368220408879117131L
def egcd(a, b):
if a == 0:
return (b, 0, 1)
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import sys
import re
import struct
import json
def findndx(str, data):
return [m.start() for m in re.finditer(str, data)]
@chris3k
chris3k / fibonacci_coding.py
Created March 15, 2015 11:08
Fibonacci coding
def fibGenMaxVal(maxnum):
"""
>>> fibGenMaxVal(30)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
>>> fibGenMaxVal(150)
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
"""
fibnums = [0, 1, 1]
@chris3k
chris3k / deduplicate.cpp
Last active August 29, 2015 14:12
hexify
// cl /EHsc /nologo /W4 /MTd deduplicate.cpp /I boost-1.46.1\include /link /libpath:boost-1.46.1\lib bcrypt.lib
#pragma warning(disable: 4512) // assignment operator could not be generated
#include <stddef.h>
#include <fstream>
#include <ios>
#include <iostream>
#include <ostream>
#include <regex>