Skip to content

Instantly share code, notes, and snippets.

@m1el
m1el / rating.hs
Last active December 26, 2015 23:29
rating function for entries using algorithm described in http://www.evanmiller.org/how-not-to-sort-by-average-rating.html
rating :: Integer -> Integer -> Double
rating pos neg = rating' 1 2.24 (pos+neg) (fromIntegral pos)
-- 2.24 is quantile function for 0.9875, 97.5% certainity
-- {max}-star rating system
rating' :: Double -> Double -> Integer -> Double -> Double
rating' _ _ 0 _ = 0
rating' max z cnt total
| max > 0 && z > 0 && cnt > 0 && total >= 0 =
let cnt' = fromIntegral cnt
@m1el
m1el / walls.js
Created November 1, 2013 16:15
example wall capacity calculator http://jsfiddle.net/WnzmP/3/
var mas = [7,21,5,6,34,67,34,53,6,4,77,42,55];
function calcCapacity(arr) {
var walls = [],
capacity = 0;
arr.forEach(function(height, index) {
var prev, prev2;
while (walls.length && walls[walls.length - 1].height <= height) {
prev = walls.pop();
@m1el
m1el / rnd.js
Created November 7, 2013 11:01
a slightly modified rnd template engine
function htmlEscape(str) {
var set = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
"'": '&#39;',
'"': '&quot;'
};
return ('' + str).replace(/[&<>"']/g, function (char) {
return set[char];
@m1el
m1el / sigmaRejection.hs
Last active December 28, 2015 07:59
sigma rejection algorithm - returns average of values that are not too far from average
import Data.List
sigmaRejection = sigmaRejection' 3
sigmaRejection' distance xs =
mean $ filter (\x-> abs(mean xs - x) < distance * sigma) xs
where
mean xs = realToFrac (sum xs) / (genericLength xs)
sigma = sqrt $ ((sum xs)^2 - (sum $ map (^2) xs)) / genericLength xs
@m1el
m1el / anagram.cs
Created December 19, 2013 11:02
anagram c# tester
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static bool IsAnagram(string word1, string word2) {
if (word1.Length != word2.Length) {
@m1el
m1el / habra-reddit-sort.js
Last active August 19, 2018 09:59
reddit comment sorting algorithm implemented for habr
// License=WTFPLv2
$(function () {
function sortCommentList(parent, recursive) {
var comments = $(parent).find('>.comment_item');
$(parent).append(keySort(comments, extractRating, true));
recursive && comments.find('>.reply_comments:has(div)').each(function (idx, replies) {
sortCommentList(replies, recursive);
});
}
@m1el
m1el / kallman.js
Last active January 4, 2016 17:19
function Kallman(config) {
if (!(this instanceof Kallman)) {
return new Kallman(config);
}
for(var k in config) {
if (config.hasOwnProperty(k)) {
this[k] = config[k];
}
}
this.steps = 1;
lvl1: NOT
eNrt2VEOgjAQRVHK9Ic1sAX/XYv73wihEImkY2ihdIALqW1yfGo0edTgX77vPq57
O9/knAQJEiRIkCDBpwfbDacSnGY3r7RZDS4rbX7OzxG2c61+BBWR6Niv4ZDV+L4v
it5RfVOPLX4jYxONjz/L5TmivMQZ3RTtxE2liaIoWkK1MrT3malKFEVRqhJFUbOq
/0kXa3tOqhJF0TL6v+7MlSFViaJolapcHwm7ymtWJXe7URQtoJk70jL3uA+pyvjY
r1yFURRN1QqNNABUscfB
lvl2: AND
eNrt2U0KgCAQhuF03HiGrtC+s3T/iwSubFKyXytfQ1o8DIXU50RucL2fjB+N644c
nyw02aPwilad/7Q4FFJIIYUUUlitcGODLdnKk1t6E6sa2jmbH0FFJDnPqxU1F9cN
@m1el
m1el / dictcmp.py
Last active August 29, 2015 13:56
recursive dict comparison with self-reference
def dictcmp(a, b, stacka=None, stackb=None):
if a is b:
return True
if type(a) is not type(b):
return False
stacka = stacka or []
stackb = stackb or []
stacka.append(id(a))
stackb.append(id(b))
res = False
// http://nolandc.com/sandbox/fractals/?x=25&y=1&l=8&d=6&sa=80&a=-45&s=F++F++F++F++&r=F,F+D---F++F,D,+F+D---F+D
// space-filling fractal, a modification of Sierpinski curve
function d2xy(d, s) {
var r = {x: 0, y: 0}, p = {x: 0, y: 0};
for (s = s-1; s >= 0; s--) {
p.x *= 2;
p.y *= 2;
var pow = 1 << (s*2);
rot(r, (d & pow*3) >> s*2);
if(s==1){