Skip to content

Instantly share code, notes, and snippets.

View schwern's full-sized avatar

Michael G. Schwern schwern

View GitHub Profile
test=> select count(*) from orders;
count
---------
1003116
(1 row)
test=> select count(distinct articleid) from orders;
count
--------
999921
@schwern
schwern / permutation.go
Last active August 31, 2021 14:18
A permutation library based on Paul Hankin's from https://stackoverflow.com/a/30230552/14660
package permutation
// Based on https://stackoverflow.com/a/30230552/14660 by Paul Hankin
// perm := NewPermutation( slice )
// for next := perm.Next(); next != nil; next = perm.Next() {
// ...
// }
//
// or
@schwern
schwern / foo.go
Created May 13, 2017 00:13
This is why whitespace should not be syntax. One is a syntax error, one is not. Can you guess?
for i, j := 0, len(r)-1;
i < len(r)/2;
i, j = i+1, j-1
{
r[i], r[j] = r[j], r[i]
}
for i, j := 0, len(r)-1;
i < len(r)/2;
i, j = i+1, j-1 {
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
char * strcat_ex( const char *s, ... )
{
if ( s == NULL ) {
return NULL;
}
@schwern
schwern / test.c
Created December 11, 2016 00:16
fgets vs fgetc, strlen vs strcpsn for stripping newlines off input lines
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int read_line_fgetc(FILE *fp, char *line, int line_size){
int c;
int i = 0;
while( (c = fgetc(fp)) > 0) {
if( (char)c == '\n' ) {
break;
@schwern
schwern / gist:5d0d1ea3c1817fbba0d0d50c1d7b636b
Created April 29, 2016 22:35
Demonstrate it's safe to use a PRNG to seed more PRNGs with a GOOD PRNG.
/* See http://www.pcg-random.org/using-pcg-c-basic.html */
#include "pcg_basic.h"
#include <stdio.h>
void try_random(unsigned int seed, int initseq) {
pcg32_random_t rng;
pcg32_srandom_r(&rng, seed, initseq);
printf("Seed: %u\n", seed);
t = Process.clock_gettime(Process::CLOCK_MONOTONIC);
puts "%10.10f" % t.prev_float; # 391942.2739719209
puts "%10.10f" % t; # 391942.2739719210
puts "%10.10f" % t.next_float; # 391942.2739719211
#include <stdio.h>
typedef struct date_t {
int year; int month; int day;
} date_t;
#define EPOCH 1970
void date_convert(date_t* date, unsigned int days) {
int year = EPOCH, mo = 0, day = 1;
@schwern
schwern / gist:1a4482bfcd5feee0812c
Created March 4, 2016 09:37
nanosecond time on OS X
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#ifdef __MACH__
#include <mach/mach_time.h>
#define CLOCK_REALTIME 0
#define CLOCK_MONOTONIC 0
int clock_gettime(int clk_id, struct timespec *t){
mach_timebase_info_data_t timebase;
@schwern
schwern / gist:39c100327f322caa9b5c
Created January 26, 2016 06:59
$RE{URI} from Regexp::Common
(?:(?:(?:https?)://(?:(?:(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)|(?:[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)))(?::(?:(?:[0-9]*)))?(?:/(?:(?:(?:(?:(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:/(?:(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))(?:[?](?:(?:(?:[;/?:@&=+$,a-zA-Z0-9\-_.!~*'()]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)))?))?)|(?:(?:file)://(?:(?:(?:(?:(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z]))|(?:[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+))|localhost)?)(?:/(?:(?:(?:(?:[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