Skip to content

Instantly share code, notes, and snippets.

about two years ago, i was taking a class in uni. our last assignment was to implement huffman coding in c. a classmate needed to find some way to represent the path from the root of the tree to a leaf. typically paths are represented as a sequence of 0s and 1s, to determine whether you need to take the left or right child of the node you're at.

the way he represented the path was with a long long. and so there are 255 possible byte values, and a long long is only 64 bytes long. this seems like a bad idea. but he used the digits of a long long instead, and in his scheme a 1 meant to take the left child and a 2 meant to take the right child of a node. his paths were backwards, so '122' meant right -> right -> left. he traversed the tree by dividing the path by 10, and when his path reached '0', he knew he was at a leaf node.

his code still broke when he was given input with too wide a variety of characters. one of the files the professor used to test our code was moby dick, and it made his program spit out n

@nyux
nyux / tricks.c
Last active August 29, 2015 14:19
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
typedef struct {
char *name;
int health, atk, def;
} character_t;
from __future__ import print_function
import math
math.lg = lambda x : math.log(x, 2)
# rough approximates, completely disregards the coefficients that might be in
# front of these functions. still, illustrative of their rate of growth as
# n -> \inf
for i in range(1, 20):
[trace] Stack trace suppressed: run last assignment/compile:run for the full output.
[error] (assignment/compile:run) No main class detected.
[error] Total time: 173 s, completed Sep 17, 2014 5:42:28 PM
> last assignment/compile:run
java.lang.RuntimeException: No main class detected.
at scala.sys.package$.error(package.scala:27)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$27$$anonfun$15.apply(Defaults.scala:521)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$27$$anonfun$15.apply(Defaults.scala:521)
at scala.Option.getOrElse(Option.scala:108)
at sbt.Defaults$$anonfun$runTask$1$$anonfun$apply$27.apply(Defaults.scala:521)
def bloo(seq, item):
for index, thing in enumerate(seq):
if thing == item: return index
else: return -1
#include <stdlib.h>
#include <string.h>
char* utility_hex_to_ascii(char *hexstr)
{
size_t hexlen = strlen(hexstr);
/* every two characters in a hex string => one byte of ascii, + '\0' */
size_t ascii_len = hexlen / 2;
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
/* remember: compile with -lgmp flag */
int main(void)
{
char *hex1 = "1c0111001f010100061a024b53535009181c";
char *hex2 = "686974207468652062756c6c277320657965";
#include <stdio.h>
/* does weird behavior in clang. it ignores the char* rule and casts char*
parameters to ints. no clue why. the uncommented version has the fix, and i'm
not sure why it works.
#define abs(x) _Generic((x), \
long int: absl,\
double: absd, \
char*: abss, \
@nyux
nyux / base64.c
Last active August 29, 2015 14:05
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
void base64_convert(char a, char b, char c)
{
int i;
int output[4];
public class Parent {
public int x = 0;
public Parent(int x) { this.x = x; }
public class InnerChild extends Parent {
public int x = 1;
public InnerChild(int x) {
super(2 * x);