Skip to content

Instantly share code, notes, and snippets.

(defn find-liberty-helper [board point size]
(filter #(gray? board % size) (find-neighbors point size)))
(defn find-liberty [board points size]
(loop [points points liberty ()]
(let [point (first points)]
(if (= 0 (count points))
(set liberty)
(recur (rest points) (clojure.set/union (find-liberty-helper board point size) liberty))))))
(defn find-enemy [board point color size]
(let [color (if (= white color) black white)
neighbors (find-neighbors point size)]
(set (reduce
#(if (ally? %2)
(merge %1 (find-ally board %2 color size))
%1)
() neighbors))))
@naush
naush / vane.rb
Created January 12, 2012 13:20
Process time table data for vane.
lines = File.open('time_table.csv', 'rb') { |file| file.read }
require 'time'
File.open('result_time_table.csv', 'w') do |csv|
lines.each do |line|
tokens = line.chomp.split(",")
if tokens[1] == ":"
csv.puts tokens.join(",")
next
@naush
naush / PKIXCertPathValidator.java
Created January 19, 2012 16:48
sun.security.provider.certpath.PKIXCertPathValidator
245 try {
246 X509CertImpl firstCertImpl = X509CertImpl.toImpl(firstCert);
247 issuerSelector.parseAuthorityKeyIdentifierExtension(
248 firstCertImpl.getAuthorityKeyIdentifierExtension());
249
250 worthy = issuerSelector.match(trustedCert);
251 } catch (Exception e) {
252 // It is not worth trying.
253 }
class PrimeFactor
def initialize(number)
@factors = Fiber.new do
divisor = 2
while divisor < number
while number % divisor == 0
Fiber.yield divisor
number = number / divisor
end
divisor = divisor + 1
module PrimeFactor
def self.new
@memoized = {}
Hash.new do |h, index|
@memoized[index] ||= memoize(h, index)
h[index] = @memoized[index]
end
end
# multiple exits
if a
code
code
code
if b
code
code
code
return c
1.day.ago # => Sun, 11 Mar 2012 18:58:25 EDT -04:00
24.hours.ago # => Sun, 11 Mar 2012 18:58:28 EDT -04:00
2.days.ago # => Sat, 10 Mar 2012 18:58:35 EST -05:00
48.hours.ago # => Sat, 10 Mar 2012 17:58:38 EST -05:00
(60 * 60).seconds # => 3600 seconds
(60 * 60).seconds.parts # => [[:seconds, 3600]]
(60 * 60).seconds.parts.size # => 1
([1.second] * 60 * 60).sum # => 3600 seconds
([1.second] * 60 * 60).sum.parts.size # => 3600