Skip to content

Instantly share code, notes, and snippets.

View a10y's full-sized avatar
🇺🇲
DC

Andrew Duffy a10y

🇺🇲
DC
View GitHub Profile
// Solution to the interview question of finding ideal purchase/sale times and profits for
// a history of a single stock, in Go
package main
import "fmt"
// Finds the maximum profit to be had from buying and selling at a given price
func MaxProfit(prices []int) (buy int, sell int, profit int) {
profit = 0
@a10y
a10y / tree-iso.go
Created May 19, 2016 20:56
Tour of Go Tree Isomorpism
package main
import "golang.org/x/tour/tree"
import "fmt"
// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
walk(t, ch)
close(ch)
// should make a SIGSEGV, which can be caught
// Purpose of this is to see how you can use tricks like this
// to create distributed shared memory that is demand faulted
// into memory from off the network.
#include <sys/mman.h>
#include <stdlib.h>
#include <fcntl.h>
#include <strings.h>
#include <string.h>
#include <errno.h>
@a10y
a10y / extractors.scala
Created July 3, 2016 22:47
Scala URL component parsing extractor
object URLExtractor {
// Takes a URL formatted as a string, extracts out the scheme, host and path
def unapply(url: String): Option[(String, String, String)] = {
val (scheme, rest) = url.split("://") match {
case Array(scheme, rest) => (scheme, rest)
case _ => ("", "")
}
if (scheme.isEmpty || rest.isEmpty)
None

Local Publish

When publishing to a personal Git repo instead of to crates.io, you use git URLs to refer to the deps in Cargo.toml.

Example:

[dependencies]
mything = { git = "ssh://[email protected]/andreweduffy/mything.git" }
@a10y
a10y / rotation-point.py
Last active August 29, 2016 10:06
python implementation of finding rotation point
def find_rotation(arr):
"""
Finds the rotation point of a list of length N in time O(lg(N))
Ex: [4,5,6,1,2,3] -> 3
"""
def _find_rotation(arr, start, end):
mid = int(start + (end - start) / 2)
if start == end:
return start
@a10y
a10y / mm-test.c
Created September 24, 2016 21:33
/**
* mm-test: A simple test of using mmap() to manipulate files as if they are
* regions of memory. This example has full error checking, and allows the user
* to specify a file, as well as replacement text and an optional offset value.
*
* Andrew Duffy, 2016
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@a10y
a10y / httpfs.go
Created September 24, 2016 21:37
// httpfs Performs GET requests when reading a file.
// Compile with `go build httpfs.go` and then run with `./httpfs /http`,
// then try doing `cat /http/aduffy.org`
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
@a10y
a10y / flash.rb
Created September 24, 2016 22:13
#!/usr/bin/env ruby
=begin
Flash -- Check a set of sites to see links matching queries
Requirements:
Mechanize
Ruby (I'm on 2.0.0, not a ruby expert)
This script just scrapes a given set of news sources for links matching a query.
"""
Solution to Beautiful Pairs HackerRank challenge.
Challenge: Given two input arrays, A and B, find the maximal number of disjoint pairs (i, j) where A[i] = B[j],
after one element from B is changed. The change can be anything you wish, though it must happen.
Description of Solution:
- Let Ca[k] = number of occurrences of k in A
- Let Cb[k] = number of occurrences of k in B
- for each k in Ca: