Skip to content

Instantly share code, notes, and snippets.

Avatar

Michael G. Schwern schwern

View GitHub Profile
View gist:146345bcc53beacb2d9eecff28fef3f6
test=> select count(*) from orders;
count
---------
1003116
(1 row)
test=> select count(distinct articleid) from orders;
count
--------
999921
@schwern
schwern / permutation.go
Last active Aug 31, 2021
A permutation library based on Paul Hankin's from https://stackoverflow.com/a/30230552/14660
View permutation.go
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
This is why whitespace should not be syntax. One is a syntax error, one is not. Can you guess?
View foo.go
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 {
View strcat_ex.c
#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 Dec 11, 2016
fgets vs fgetc, strlen vs strcpsn for stripping newlines off input lines
View test.c
#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 Apr 29, 2016
Demonstrate it's safe to use a PRNG to seed more PRNGs with a GOOD PRNG.
View gist:5d0d1ea3c1817fbba0d0d50c1d7b636b
/* 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);
View gist:32b809c1eec1487d5506
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
View gist:167e56de0c9d5e3aa33b
#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;
View gist:1a4482bfcd5feee0812c
#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 Jan 26, 2016
$RE{URI} from Regexp::Common
View gist:39c100327f322caa9b5c
(?:(?:(?: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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