This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def spiral(M): | |
| """ | |
| Given a matrix M (mxn) print out the elements in spiral order | |
| Ex: | |
| 30 42 30 55 31 | |
| 21 30 60 24 38 | |
| 11 22 33 44 55 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def longest_common_substring(s1, s2): | |
| """ | |
| lcs[i,j] = if match lcs[i-1, j-1] + 1 | |
| lcs[i,j] = if not match 0 | |
| """ | |
| m = len(s1) | |
| n = len(s2) | |
| # Hash table not be more memory efficient. Here we have n*m | |
| # entries guaranteed. In a hash table all entries that are 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def match(s, pairs): | |
| stack = [] | |
| closers = set(pairs.values()) | |
| for c in s: | |
| if c in pairs: | |
| stack.append(pairs[c]) | |
| elif c in closers: | |
| # we can end early if either is true | |
| if not stack or c != stack.pop(): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Simple pagerank implementation | |
| # M - transition matrix | |
| # r - initial rankings | |
| # \beta - the taxation cost, (1-\beta) is the teleport prob | |
| # iter - iterations to run for | |
| function pagerank(M::Matrix{Float64}, r::Vector{Float64}, β::Float64, iter::Int64) | |
| c = (1-β)/length(r) | |
| v_prime = β*M*r + c | |
| for i = 1:iter | |
| v_prime = β*M*v_prime + c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Copyright 2012 The Go Authors. All rights reserved. | |
| # Use of this source code is governed by a BSD-style | |
| # license that can be found in the LICENSE file. | |
| # git gofmt pre-commit hook | |
| # | |
| # To use, store as .git/hooks/pre-commit inside your repository and make sure | |
| # it has execute permissions. | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #![feature(macro_rules)] | |
| macro_rules! loop_x { | |
| ($e: expr) => { | |
| // $e will not interact with this 'x | |
| // refers to the 'x in the outside loop | |
| 'x: loop { | |
| println!("Hello!"); | |
| $e | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Pretend this is an actual big struct in which it would be | |
| // expensive to copy by value. | |
| struct BigStruct { | |
| one: int, | |
| two: int, | |
| // etc | |
| one_hundred: int, | |
| } | |
| // An antipattern would be for the return type to be |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Resize Linux partition without any disks/cds/usb things! | |
| Works on Ubuntu 14.04, probably fine on others | |
| 1. | |
| sudo losetup /dev/loop0 | |
| 2. /path/to/file is found with 1., xxx is the number of MiBs to allocate, note: 1MiB ~ 1MB | |
| sudo dd if=/dev/zero bs=1MiB of=/path/to/file conv=notrunc oflag=append count=xxx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "bytes" | |
| "fmt" | |
| "io/ioutil" | |
| "net" | |
| "os/user" | |
| "time" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| blahhing away |