Skip to content

Instantly share code, notes, and snippets.

View ashelly's full-sized avatar

AShelly ashelly

  • San Francisco, CA
View GitHub Profile
ashelly /
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 /
Created January 19, 2021 04:52
Python UDP Client
import socket
fd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM )
udp_ip = ''
udp_port = 4001
message = input("> ")+"\r"
fd.sendto(message.encode(), (udp_ip, udp_port))
ashelly / tree_encode.c
Created November 4, 2019 17:58
Compact binary tree encoding. Solution to
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct Node_t
int data;
struct Node_t* left;
struct Node_t* right;
ashelly / permutation.c
Created February 1, 2019 04:59
Knuth Algorithm L (Lexicographic Permutation Generation) in C.
static inline void Exchange(int* data, int a, int b) {
int temp = data[a];
//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 / 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 {
for (i=0;i<N;i++) { if (A[i]) { count++; A[i]--;} }
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.
(c) 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 /
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 /
Created September 5, 2014 02:40
Super simple validator for JSON files. Shows line and column of error, launches editor at that point.
import sys,json,re,subprocess
file = open(sys.argv[1])
l =
print "OK"
except Exception as e:
ashelly / CompileTimeOptions.h
Last active August 29, 2015 14:05
Method of easily selecting from multitude of exclusive compile time options.
//set only one of the following to 1.
//Did you follow the rules?
#error("Error configuring options")
ashelly /
Created April 21, 2014 21:43
emulate a serial device with a terminal
socat PTY,link=/dev/ttyFAKE0,raw,echo=0 READLINE