Skip to content

Instantly share code, notes, and snippets.

View ashelly's full-sized avatar

AShelly ashelly

  • San Francisco, CA
View GitHub Profile
@ashelly
ashelly / mediator.c
Created May 28, 2013 20:36
Running Median. Finds the median of the last K inputs in O(lg K). See http://stackoverflow.com/q/5527437/10396 for some details.
//Copyright (c) 2011 ashelly.myopenid.com under <http://www.opensource.org/licenses/mit-license>
#include <stdlib.h>
//Customize for your data Item type
typedef int Item;
#define ItemLess(a,b) ((a)<(b))
#define ItemMean(a,b) (((a)+(b))/2)
typedef struct Mediator_t
@ashelly
ashelly / getopt.c
Last active November 9, 2023 23:22
"Port of GNU getopt() to Win32 for anyone who's tired of dealing with getopt() calls in Unix-to-Windows ports." Ported by Pete Wilson. Recovered from the Internet Archive's snapshot of www.pwilson.net/sample.html.
/* Getopt for GNU.
NOTE: getopt is now part of the C library, so if you don't know what
"Keep this file name-space clean" means, talk to drepper@gnu.org
before changing it!
Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
@ashelly
ashelly / gdb_tricks.md
Last active July 19, 2023 18:44
gdb debugging setup

redirect output to another terminal

  • In target terminal type tty
  • copy the result (should be something like /dev/pts/0
  • in gdb window type tty <pasted-result>

tui mode

  • enable
@ashelly
ashelly / interactive.py
Last active January 5, 2023 20:58
interactive single-char input in Python (no newline needed)
import sys,os
import termios
import tty
from contextlib import contextmanager
import signal
""" Allow single-key keyboard input (no <enter> needed)
Sample Usage:
@ashelly
ashelly / udpclient.py
Created January 19, 2021 04:52
Python UDP Client
import socket
fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM )
fd.settimeout(1)
udp_ip = '10.82.2.254'
udp_port = 4001
while(True):
message = input("> ")+"\r"
fd.sendto(message.encode(), (udp_ip, udp_port))
while(True):
try:
@ashelly
ashelly / WRS.c
Last active June 15, 2020 15:38
Fast Weighted Random Sampling from discrete distributions. i.e Selecting items from a weighted list.
/*
WRS.c
(c) AShelly (github.com/ashelly)
Weighted random sampling with replacement of N items in O(1) time.
(After preparing a O(N) sized buffer in O(N) time.)
The concept is:
Randomly select a buffer index. Each index is selected with probablilty 1/N.
Each index stores the fraction of hits for which this item should be selected,
@ashelly
ashelly / tree_encode.c
Created November 4, 2019 17:58
Compact binary tree encoding. Solution to https://codegolf.stackexchange.com/q/339/152
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct Node_t
{
int data;
struct Node_t* left;
struct Node_t* right;
@ashelly
ashelly / permutation.c
Created February 1, 2019 04:59
Knuth 7.2.1.2: Algorithm L (Lexicographic Permutation Generation) in C.
static inline void Exchange(int* data, int a, int b) {
int temp = data[a];
data[a]=data[b];
data[b]=temp;
}
//generates all permutations of initially sorted array `a` with `n` elements
//returns 0 when no more permutations exist
int genPermutation(int a[], int n)
{
@ashelly
ashelly / beadsort.c
Created November 19, 2018 22:44
A 1 dimensional implementation of beadsort. O(MN) where M is maxint.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void sort(int A[], int N) {
int i,count;
int *R = calloc(N,sizeof(int));
do {
count=0;
for (i=0;i<N;i++) { if (A[i]) { count++; A[i]--;} }
@ashelly
ashelly / subsequence_k.c
Last active December 29, 2015 03:49
Tests that string S is a subsequence of T with no gaps greater than k characters
/*
subsequence_k:
Tests that string S is a subsequence of T with no gaps greater than k characters.
The implementation that uses the idea of a surveyor's chain:
Imagine a series of poles linked by chains of length k.
Place the first pole at the beginning of the string.
Now cary the next pole forward until you find a character match.
Plant that pole. If there is slack, move on to the next character;
else the previous pole has been dragged forward, and you need to go back