Skip to content

Instantly share code, notes, and snippets.

View vincentchu's full-sized avatar

Vincent Chu vincentchu

View GitHub Profile
import scala.actors.Actor
import scala.actors._
import scala.collection.mutable.Map
import java.io._
import java.net.{InetAddress,ServerSocket,Socket,SocketException}
class Coordinator extends Actor {
var clients = Map[String, Client]()
var numClients = 0
@vincentchu
vincentchu / boyer_moore.rb
Created February 12, 2012 04:46
Boyer Moore implemented in Ruby
class BoyerMoore
attr_accessor :text
attr_reader :pattern, :matches
def initialize( opts )
@text = opts[:text]
end
def search( pattern_str )
@pattern = Pattern.new( pattern_str )
@vincentchu
vincentchu / boyer_moore.scala
Created February 12, 2012 04:45
Implementation of BoyerMoore in Scala
import scala.collection.mutable.ArrayBuffer
class Pattern(val pattern: String) {
val length = pattern.length
def shiftBy(badChar: Char, pos: Int): Int = {
scala.math.max(badCharShift(badChar, pos), goodSuffixShift(pos))
}
# Find k-smallest integers from an array of N integers
#
# Given an array of N integers in no particular order, what is the fastest
# way to determine which are the smallest k of them?
#
# Solution: Use a heap!
#
# First, the solution of this problem seems easy. Just sort the N-Array, and
# take the first K-values. Indeed, in Ruby, this can be done as a simple
# one-liner:
# >> Heap
#
# A Heap or Max heap is a data structure based on a tree that satisfies the
# Heap property:
#
# If K is a child of J, then val(J) >= val(K)
#
# In other words, the value of the parent node is always greater than or equal
# to the value of its children. Because of this property, the root of the tree
# will *always* be the largest value stored in the heap.
#!/usr/bin/ruby
binwidth = ARGV[0].to_f
filename = ARGV[1]
rand_file_name = "rand_file-#{(rand*1000000).to_i}.data"
nums = File.open(filename, "r") {|f| f.readlines}.collect {|n| n.to_f }
binned_data = {}
nums.each do |n|
@vincentchu
vincentchu / handlersocket.md
Created November 14, 2011 19:15
HandlerSocket + Ruby!

Build libhsclient libs

git clone https://github.com/ahiguti/HandlerSocket-Plugin-for-MySQL.git
cd HandlerSocket-Plugin-for-MySQL
./autogen.sh
./configure --disable-handlersocket-server
make
sudo make install

Build handlersocket ruby client

>> Loofah::Helpers.strip_tags("<script>var foo;</script>")
=> "<script>var foo;</script>"
>>> num = 5.0/3
>>> print "num = %.2f" % (num)
num = 1.67
>>> print "num = %.3f" % (num)
num = 1.667
>>> print "num = %6.3e" % (num)
num = 1.667e+00
# For multi-line blocks, use do. E.g.:
users.each do |user|
user.name = "foo"
user.save
end
# For single-line blocks, use {}
users.each {|user| user.destroy }