Skip to content

Instantly share code, notes, and snippets.

View brendanashworth's full-sized avatar
🐦
bunting

Brendan Ashworth brendanashworth

🐦
bunting
View GitHub Profile
@brendanashworth
brendanashworth / script.pl
Created June 17, 2014 01:10
Collatz conjecture for Perl
#!/usr/bin/perl
# This script finds the number of cycles necessary to get to 1 via the
# Collatz conjecture.
use strict;
use warnings;
unless (defined $ARGV[0]) {
print("Supply the integer as the first argument.");
exit;
}
@brendanashworth
brendanashworth / str_count.c
Last active August 29, 2015 14:02
Count occurrences of a string inside a string (C)
int str_count(char *string, char *delim) {
int count = 0;
int i;
for (i = 0; i < strlen(string); i++) {
if (strncmp(&string[i], delim, strlen(string))) {
count++;
}
}
return count;
@brendanashworth
brendanashworth / str_split.c
Last active August 29, 2015 14:02
Split a string by a delimiter into a string array
// NOTE: This gist requires https://gist.github.com/boboman13/956405a90345f3ce477c.
char **str_split(char *string, char *delim) {
uint8_t size = str_count(string, "") + 1, i;
char** split = malloc(size * sizeof(char*) );
for (i = 0; i < size; i++) {
if (i == 0)
split[i] = strtok(string, delim);
else
split[i] = strtok(NULL, delim);
@brendanashworth
brendanashworth / main.sql
Created June 27, 2014 01:45
MySQL command reference
# Select, insert, update, delete
SELECT * FROM `my_table` WHERE `my_field` = 'my_value';
INSERT INTO `my_table` (my_field, another_field) VALUES ('myFieldValue', 'AnotherFieldValue');
UPDATE `my_table` SET `my_field` = 'aNewValue';
DELETE FROM `my_table` WHERE `my_field` = 'a_special_value';
# Get the schema of sorts for a table
@brendanashworth
brendanashworth / main.md
Last active August 29, 2015 14:04
My favorite programming talks
@brendanashworth
brendanashworth / main.c
Created July 17, 2014 22:24
Basic example multithreading in C
#import <stdio.h>
#import <pthread.h>
void *threadFunction(void *arg) {
// To be executed when called...
}
int main(void) {
pthread_t *thread;
@brendanashworth
brendanashworth / main.sh
Created July 23, 2014 23:18
Create a .tar.gz archive from git, no external deps
#!/bin/bash
BRANCH=master
FORMAT=tar.gz
EXPORT_LOCATION=/tmp/backup.tar.gz
git archive $BRANCH -o $EXPORT_LOCATION --format $FORMAT
@brendanashworth
brendanashworth / main.sh
Created August 12, 2014 22:38
Install php 5.5 on Ubuntu 12.04
#!/bin/bash
# Credits to http://askubuntu.com/a/343567
sudo apt-get install python-software-properties -y
sudo add-apt-repository ppa:ondrej/php5 -y
sudo apt-get update -y
sudo apt-get install php5 -y
echo ""
@brendanashworth
brendanashworth / debug.h
Created October 12, 2014 20:35
C debug macro
/* From https://github.com/brendanashworth/wookie
* src/util.h
* licensed under MIT license
*/
/**
* A debug macro that prints out file, line number, then message.
* @param msg The debug message to print.
*/
#define DEBUG(msg) printf("[DEBUG] %s:%d; %s\n", __FILE__, __LINE__, msg)
var util = require('util');
var events = require('events');
// Create the object constructor
function Cat(name) {
this.name = name;
}
// Inherit the EventEmitter
util.inherits(Cat, events.EventEmitter);