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
//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 |
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
/* | |
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 |
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
/* 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 [email protected] | |
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 |
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
socat PTY,link=/dev/ttyFAKE0,raw,echo=0 READLINE | |
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 |
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: |
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 <enter> needed) | |
Sample Usage: |
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, |
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 <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]--;} } |
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) | |
{ |
OlderNewer