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
/* 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; |
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
/* 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; |
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
// 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++) { |
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 ( | |
"fmt" | |
"io/ioutil" | |
) | |
// " \t\r\n" | |
var spaces = map[byte]bool{ 32: true, 9: true, 10: true, 13: true } |
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
#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) |
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
#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) |
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
start | |
= seq | |
validchar | |
= [0-9a-zA-Z_?!+\-=@#$%^&*/.] | |
atom | |
= chars:validchar+ { return chars.join(""); } | |
symbol |
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
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')) |
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
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)) |
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
# 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) |