Skip to content

Instantly share code, notes, and snippets.

@adamvr
Created February 15, 2012 00:58
Show Gist options
  • Select an option

  • Save adamvr/1832180 to your computer and use it in GitHub Desktop.

Select an option

Save adamvr/1832180 to your computer and use it in GitHub Desktop.
Junkcode gist
A bunch of useless files.
[ -e $1 ] && echo yes || echo no
[ -f /usr/local/bin/npm ] && echo 'npm!'
class Linked
attr_reader :head, :tail
def initialize
@head = nil
@tail = nil
end
def <<(item)
item = LinkedItem.new(item)
if @head == nil
@head = @tail = item
else
item.next = @head
@head.prev = item
@head = item
end
return self
end
def each
it = @head;
until it == nil
yield it
it = it.next
end
end
def pop
out = @head
@head.next = nil
@head = out.next
out.next = nil
return out
end
end
class LinkedItem
attr_accessor :next, :prev
attr_reader :contents
def initialize(contents)
@contents = contents
@next = nil
@prev = nil
end
def to_s
@contents
end
end
l = Linked.new
l << 'a' << 'b' << 'c'
l.each do |e|
puts e
end
#include <stdio.h>
#include <stdlib.h>
#define POOL_SIZE 10
typedef struct list_node {
int a;
int b;
struct list_node *next;
} list_node;
typedef struct list {
list_node *head;
list_node *tail;
int length;
} list;
list* list_create() {
list *l = (list*) malloc(sizeof(list));
l->head = NULL;
l->tail = NULL;
l->length = 0;
return l;
}
void list_destroy(list *l) {
list_node *it, *next;
for (it = l->head; it; it = next) {
next = it->next;
free(it);
}
free(l);
}
void list_lpush(list *list, int a, int b) {
list_node *new;
new = (list_node *) malloc(sizeof(list_node));
new->a = a;
new->b = b;
if (!list->head) {
list->head = new;
list->tail = new;
} else {
new->next = list->head;
list->head = new;
}
list->length++;
}
void list_rpush(list *list, int a, int b) {
list_node *new;
new = (list_node *) malloc(sizeof(list_node));
new->a = a;
new->b = b;
if (!list->head) {
list->head = new;
list->tail = new;
} else {
list->tail->next = new;
list->tail = new;
}
list->length++;
}
list_node *list_lpop(list *list) {
list_node *ret;
if (!list->length) return NULL;
ret = list->head;
list->head = list->head->next;
ret->next = NULL;
list->length--;
return ret;
}
/* O(n) */
list_node *list_rpop(list *list) {
list_node *ret, *it;
if (!list->length) return NULL;
ret = list->tail;
for(it = list->head; it->next == ret; it = it->next) ;
it->next = NULL;
list->length--;
return ret;
}
/* O(n) */
void list_print(list *list) {
list_node *it;
for(it = list->head; it; it = it->next) {
printf("a: %d, b: %d\n", it->a, it->b);
}
}
/* O(n) */
void list_filter(list *list, int (*filter) (list_node*)) {
list_node *prev = NULL, *curr = NULL, *next = NULL;
if (!list->length) return;
for(curr = list->head; curr; curr = next) {
next = curr->next;
if (filter(curr)) {
if (NULL == prev) {
list->head = next;
} else {
prev->next = curr->next;
}
free(curr);
curr = NULL;
list->length--;
}
prev = curr;
}
}
void list_map(list *list, void (*map) (list_node*)) {
list_node *prev = NULL, *curr = NULL, *next = NULL;
if (!list->length) return;
for(curr = list->head; curr; curr = next) {
next = curr->next;
map(curr);
prev = curr;
}
}
int no_ones(list_node *n) {
if (n->a == 1 || n->b == 1) {
return 1;
} else {
return 0;
}
}
int every(list_node *n) {
return 1;
}
int evenA(list_node *n) {
if (n->a % 2 == 0) {
return 1;
} else {
return 0;
}
}
void dubdub(list_node *n) {
n->a *= 2;
n->b *= 2;
}
int main(int argc, const char *argv[])
{
list *l = list_create();
list *l2 = list_create();
int i;
list_node *n;
for(i = 0; i <= 50; i++) {
list_rpush(l, i, i*i);
}
n = list_rpop(l);
if (n) printf("%d, %d", n->a, n->b);
list_destroy(l);
list_print(l);
/*
list_filter(l, evenA);
list_print(l);
list_filter(l, every);
for(i = 0; i <= 50; i++) {
list_linsert(l, i, i*i);
}
list_print(l);
*/
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
int pid;
/* 0 is read, 1 is write */
int p[2];
char buf[512];
FILE *in, *out;
if (pipe(p) == -1) {
perror("pipe");
}
if (!fork()) {
close(p[1]);
out = fdopen(p[0], "r");
while(fgets(buf, 512, out)) {
printf("%s", buf);
}
} else {
close(p[0]);
dup2(STDOUT_FILEDES, p[1]);
execl("ls", "/", (char*) 0);
}
return 0;
}
var regex = /(\S+) (\S+)/
var x = 'asdf fdsa'.match(regex);
var y = 'nope'.match(regex);
// x !== null
console.dir(x);
// asdf
console.log(x[1]);
// fdsa
console.log(x[2]);
// y === null
console.dir(y);
console.log('"asda"'.replace(/"/g, '\\\"'));
var express = require('express');
var app = express.createServer();
app.get('/', function(req, res, next) {
req.danger = 'danger!'
next();
});
app.get('/', function(req, res) {
res.end(req.danger);
});
app.listen(4321);
#include <stdio.h>
int main(int argc, const char *argv[])
{
int a,b;
printf("X> ");
scanf("%d%d", &a, &b);
printf("%d, %d", a, b);
return 0;
}
#!/bin/sh
exec 3>&1 2>/dev/null
for i in a b c
do
(
sleep 5 && echo $i >&3
) </dev/tty >/dev/tty &
PIDS="$PIDS $!"
done
trap 'tidy' INT HUP QUIT TERM
tidy() {
echo $PIDS
for pid in $PIDS
do
kill $pid
done
}
for pid in $PIDS
do
wait $pid
done
#include <stdio.h>
#include <string.h>
#define max(a,b) ((a > b) ? (a) : (b))
int a[][3] = {{1,2},{1,2,3},{2,3}};
int main(int argc, const char *argv[])
{
enum { a, b, c, d = 0, e };
printf("%d, %d, %d, %d, %d", a, b, c, d, e);
}
int r(int num)
{
if(num == 0 || num == 1) {
return 1;
} else {
if (num % 2 == 0) {
return r((num / 2) + 2);
} else {
return r((num-1) + 3);
}
}
}
class BSTree
attr_reader :root
def initialize
@root = nil
end
def insert(key)
@root = _insert @root, new
end
def _insert(node, key)
if node == nil
return TreeNode.new key
else if node.key == key
return TreeNode.new
end
end
def preorder
_preorder @root
end
def _preorder(root)
if root == nil
return
end
yield node.key
node.left && (preorder node.left)
node.right && (preorder node.right)
end
end
class TreeNode
attr_accessor :parent, :left, :right, :key
def initialize(key)
@key = key
end
end
h = BSTree.new
h.insert '5'
h.insert '6'
h.insert '7'
h.insert '8'
h.preorder do |key|
puts key
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment