Skip to content

Instantly share code, notes, and snippets.

View twfarland's full-sized avatar

Tim Farland twfarland

  • Farland Digital
  • Auckland, NZ
View GitHub Profile
@twfarland
twfarland / hot_score.sql
Created February 18, 2014 20:35
Hot score function mysql implementation
/* hot score */
/* http://bibwild.wordpress.com/2012/05/08/reddit-story-ranking-algorithm/ */
drop function if exists hot;
delimiter $$
create function hot(upvotes int, downvotes int, created_time int unsigned) returns double
begin
declare s int;
declare ord double;
declare secs int;
declare sign int;
@twfarland
twfarland / confidence.sql
Last active August 29, 2015 13:56
Confidence score function mysql implementation (with negative score when downvotes > upvotes)
/* confidence score */
/* http://www.evanmiller.org/how-not-to-sort-by-average-rating.html */
drop function if exists confidence_score;
delimiter $$
create function confidence_score(upvotes int, downvotes int) returns double
begin
declare n int;
declare z double;
declare p double;
declare l double;
@twfarland
twfarland / relative_date.js
Last active August 29, 2015 13:56
Relative date function
// Returns a text description of a past time relative to now, e.g: 1 minute ago, 3 hours ago, 2 years ago.
var intervals = [['year', 31556926], ['month', 2629744], ['week', 604800], ['day', 86400], ['hour', 3600], ['minute', 60], ['second', 1]];
// time is in seconds
function textDate (time) {
var diff = new Date().valueOf() * 1000 - time, i, v, n;
for (i = 0; i < intervals.length; i++) {
@twfarland
twfarland / pals.go
Last active December 18, 2015 14:29
longest palindrome
package main
import (
"fmt"
"io/ioutil"
)
// " \t\r\n"
var spaces = map[byte]bool{ 32: true, 9: true, 10: true, 13: true }
@twfarland
twfarland / missionaries-cannibals-snakes.rkt
Created November 14, 2012 19:49
missionaries and cannibals + snake cube puzzle solutions
#lang racket
(require "../connive/connive.rkt")
(require "./searchpaths.rkt") ; http://gist.github.com/4074309
; missionaries and cannibals puzzle solution
(:= (m-c)
;; represent placement of missionaries, cannibals, and the boat (all fields are integers)
(struct mc (m1 c1 b1 m2 c2 b2) #:transparent)
@twfarland
twfarland / shortetpath.rkt
Created November 14, 2012 19:47
shortest path racket version
#lang racket
(require "../connive/connive.rkt")
;; :: (state -> [state]), (state -> Bool), state -> [state]
(:= (blocking-path get-successors goal? start)
;; :: (set state), [state] -> #f | [state]
(:== solution (blocked path)
(_ (cons (list state info) prevs))
(?? (goal? (list state info)) (reverse path)
@twfarland
twfarland / eth.peg
Created August 7, 2012 17:08
Experimental peg.js grammar for Ethpretho
start
= seq
validchar
= [0-9a-zA-Z_?!+\-=@#$%^&*/.]
atom
= chars:validchar+ { return chars.join(""); }
symbol
@twfarland
twfarland / searchSolutions.hs
Created May 13, 2012 16:17
Search problem solutions using generalised search functions
import SearchPath -- https://gist.github.com/2667320
-- Missionaries and Cannibals
mc = let start = ((3, 3, 1, 0, 0, 0), "")
isGoal (state, _) = state == (0, 0, 0, 3, 3, 1)
action m c = (take m (repeat 'M')) ++ (take c (repeat 'C'))
@twfarland
twfarland / SearchPath.hs
Created May 12, 2012 16:08
Generalised search functions
module SearchPath (shortestPath, directedPath) where
import Data.Set (empty, member, insert, Set)
shortestPath :: (Ord s) => ((s, a) -> [(s, a)]) -> ((s, a) -> Bool) -> (s, a) -> [(s, a)]
shortestPath sucsOf isGoal start = searchFron empty [[start]]
where searchFron _ [] = []
searchFron expl (path : fron) = searchSucs expl fron path (sucsOf (last path))
@twfarland
twfarland / pals.py
Created May 3, 2012 19:25
Finding the longest subpalindrome in a piece of text
# Finding the start and finish indexes of the
# longest subpalindrome of a given string.
# My solution - does war & peace in approx 3.2 sec on my 2.4GHz macbook
def longest_subpalindrome_slice(text):
text = text.lower()
textlen = len(text)