{{ message }}

Instantly share code, notes, and snippets.

# AShelly ashelly

• San Francisco, CA
Created Jan 19, 2021
Python UDP Client
View udpclient.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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:
Created Nov 4, 2019
Compact binary tree encoding. Solution to https://codegolf.stackexchange.com/q/339/152
View tree_encode.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #include #include #include typedef struct Node_t { int data; struct Node_t* left; struct Node_t* right;
Created Feb 1, 2019
Knuth 7.2.1.2: Algorithm L (Lexicographic Permutation Generation) in C.
View permutation.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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) {
Created Nov 19, 2018
A 1 dimensional implementation of beadsort. O(MN) where M is maxint.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #include #include #include void sort(int A[], int N) { int i,count; int *R = calloc(N,sizeof(int)); do { count=0; for (i=0;i
Last active Jun 15, 2020
Fast Weighted Random Sampling from discrete distributions. i.e Selecting items from a weighted list.
View WRS.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 /* 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,
Last active Aug 29, 2015
interactive single-char input in Python (no newline needed)
View interactive.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 import sys,os import termios import tty from contextlib import contextmanager import signal """ Allow single-key keyboard input (no needed) Sample Usage:
Created Sep 5, 2014
Super simple validator for JSON files. Shows line and column of error, launches editor at that point.
View jsonvalidate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 #!/usr/bin/python import sys,json,re,subprocess try: file = open(sys.argv[1]) l = file.read() try: json.loads(l) print "OK" except Exception as e:
Last active Aug 29, 2015
Method of easily selecting from multitude of exclusive compile time options.
View CompileTimeOptions.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 //set only one of the following to 1. #define OPTION_PRIMARY 1 #define OPTION_ALTERNATE 0 #define OPTION_UNLIKELY 0 //Did you follow the rules? #if ((OPTION_PRIMARY+OPTION_ALTERNATE+OPTION_UNLIKELY)!=1) #error("Error configuring options") #endif
Created Apr 21, 2014
emulate a serial device with a terminal
View fake0.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters