This file contains 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 Generators | |
module Utils | |
def self.included(other) | |
other.extend(ModuleMethods) | |
end | |
module ModuleMethods | |
def arbitrary | |
@arbitrary ||= {}.tap do |o| | |
o[Integer] = ->{Random.rand(-1e+10..1e+10).to_i} |
This file contains 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
var Iterator = function(m, n) { | |
this.start = m; | |
this.end = n || Infinity; | |
this.step = 1; | |
this.current = this.start; | |
this.queue = new IteratorQueue(); | |
this.next = function() { | |
var c = this.current; | |
this.current += this.step; |
This file contains 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
function diff(a, b) { | |
return _operation(a, b, [], diff, true) | |
} | |
function union(a, b) { | |
return _operation(a, b, b, union, true) | |
} | |
function intersect(a, b) { | |
return _operation(a, b, [], intersect) |
This file contains 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
prime_factorization = function(n) { | |
var i = 2; | |
var factors = []; | |
while (n > 0 && i <= n) { | |
while (n % i == 0) { | |
factors.push(i); | |
n = n / i; | |
} | |
i++; | |
} |
This file contains 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
Queens = function(num) { | |
function range(n) { | |
return Array.apply(null, new Array(n)).map(function(_, i) { return i; }); | |
}; | |
function is_safe(q, qs) { | |
if(qs.length == 0) return true; | |
return qs.filter(function(x, i) { | |
return Math.abs(q - x) != Math.abs(qs.length - (qs.length - (i + 1))); |
This file contains 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
// O(n) | |
Array.prototype.submax = function() { | |
var max = sum = 0; | |
for(var i = 0, l = this.length; i < l; i++) { | |
sum = Math.max(sum + this[i], 0); | |
max = Math.max(max, sum); | |
} | |
return max; | |
} |
This file contains 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
// O(n log n) | |
Array.prototype.submax = function() { | |
if(this.length === 0) return 0; | |
if(this.length === 1) return this[0]; | |
var m = Math.floor(this.length / 2); | |
var max_left = find_max(this, m - 1, 0); | |
var max_right = find_max(this, m, this.length); |
This file contains 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
(function($) { | |
$.fn.zoomblo = function(full) { | |
$(this).css({ | |
position: 'relative', | |
overflow: 'hidden' | |
}); | |
var org = { |
This file contains 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
(function($) { | |
$.event.special.tripleclick = { | |
clicked: 0, | |
time: 0, | |
threshold: 80, | |
setup: function() { | |
$(this).on('click', $.event.special.tripleclick.handler); |
This file contains 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
Array.prototype.multimap = function(f) { | |
return this.length == 0 ? [] : [(this[0] instanceof Array ? this[0].multimap(f) : f(this[0]))].concat(this.slice(1).multimap(f)); | |
} | |
Array.prototype.multireduce = function(f, z) { | |
return this.length == 0 ? z : f((this[0] instanceof Array ? this[0].multireduce(f, z) : this[0]), this.slice(1).multireduce(f, z)); | |
} |
NewerOlder