Skip to content

Instantly share code, notes, and snippets.

View austinzheng's full-sized avatar

Austin Zheng austinzheng

View GitHub Profile
@austinzheng
austinzheng / az1.c
Last active December 14, 2015 09:48
AZ 3/1/2013 Question 1. I used Daniel's method and one list traversal (O(n)). A slight modification of my solution from Wednesday is using the do-while loop instead of the while loop, so that the last node in the original list is not omitted. This code can be compiled. In linux, compile with "gcc az1.c -o example1", then run with "./example1". I…
#include <stdio.h>
struct n {
int v;
struct n* next;
};
struct n* split_list(struct n* head, int pivot) {
struct n* cur = head;
struct n* lh = NULL; struct n* lt = NULL;
@austinzheng
austinzheng / min_stack.c
Created March 6, 2013 06:11
A very simple min-stack implementation using arrays to store signed integers. The stack is created with a user-specified size. Supports a pop, push, and current-minimum operation, all of which run in O(1) time. The tradeoff is that twice as much space is required as an equivalent implementation supporting a current-minimum operation running in O…
// min_stack.c
// Austin Zheng
#include <stdio.h>
#include <stdlib.h>
struct min_stack {
int* v; // This array represents the main stack, and contains actual values.
int* m; // This array represents the min stack, and contains an index into the v array.
unsigned int v_ptr; // This is a pointer to the next free main stack slot.
@austinzheng
austinzheng / mergesort.c
Last active December 15, 2015 03:28
My quick and dirty C implementation of mergesort, based on first principles. Compile it using gcc and call it with a single numeric argument. The code will generate a random array of ints with the specified length, print it out, sort it, and print out the sorted list (as well as some statistics). This implementation of mergesort uses dynamic mem…
// Mergesort
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int* recombine(int*, int, int*, int);
void print_array(int*, int);
// Debugging globals
static malloc_ctr = 0;
var myClosure: (Int, Int) -> String
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
addSquareToView(size: CGSizeMake(30, 30), color: UIColor.greenColor());
}
func addSquareToView(size s: CGSize, color: UIColor) {
var square: UIView;
let frame = CGRectMake(10, 10, s.width, s.height);
@austinzheng
austinzheng / main.swift
Last active March 7, 2016 20:41
Anonymous class experiment in Swift
import Foundation
protocol SomeDelegateProtocol : class {
func firstFunc() -> String
func secondFunc() -> Bool
func thirdFunc() -> Self
}
class MyClass {
weak var delegate : SomeDelegateProtocol?
@austinzheng
austinzheng / array_structure.swift
Last active August 1, 2018 07:04
A horrendous example of custom pattern matching in Swift
// Playground - noun: a place where people can play
import Cocoa
enum Wildcard : NilLiteralConvertible {
case Single
case FromBeginning
case ToEnd
case Range(Int)
case Literal(Int)
@austinzheng
austinzheng / reify.swift
Last active August 29, 2015 14:05
Reification example
protocol InitProtocol { init() }
struct SomeStruct<T: InitProtocol> {
var x: T? = nil
mutating func instantiate() {
if x == nil {
x = T()
}
}
}
@austinzheng
austinzheng / hkw_transformer.m
Last active August 29, 2015 14:07
Hakawai transformer example
// Text transformer (palindrome)
[self.textView transformSelectedTextWithTransformer:^NSAttributedString *(NSAttributedString *input) {
NSString *rawInput = [input string];
NSMutableString *buffer = [NSMutableString string];
unichar stackC;
for (NSInteger i=[rawInput length] - 1; i>=0; i--) {
stackC = [rawInput characterAtIndex:i];
[buffer appendString:[NSString stringWithCharacters:&stackC length:1]];
}
NSDictionary *attrs = [input attributesAtIndex:0 effectiveRange:NULL];
@austinzheng
austinzheng / hkw_mentions_setup.m
Last active August 29, 2015 14:07
Mentions plug-in setup example
// Set up mentions plug-in
- (void)viewDidLoad {
[super viewDidLoad];
HKWMentionsChooserPositionMode mode = HKWMentionsChooserPositionModeEnclosedTop;
NSCharacterSet *controlCharacters = [NSCharacterSet characterSetWithCharactersInString:@"@+"];
HKWMentionsPlugin *mentionsPlugin = [HKWMentionsPlugin mentionsPluginWithChooserMode:mode
controlCharacters:controlCharacters
searchLength:3];
self.plugin = mentionsPlugin;
mentionsPlugin.delegate = [MentionsManager sharedInstance];