Skip to content

Instantly share code, notes, and snippets.

@tomtung
tomtung / saas-class-homework-ruby-basics.rb
Created March 4, 2012 15:30
SaaS Class Homework: Ruby Basics
# Homework 1 - Part 1
def palindrome?(string)
string = string.downcase.gsub(/\W/, '')
string == string.reverse
end
def count_words(string)
Hash[
string.downcase.scan(/\w+/).
group_by{|s| s}.
@tomtung
tomtung / Rfc822DateTimeParser.cs
Created April 13, 2012 15:49
Rfc822DateTimeParser - Parse Date-Time In RFC-822 Format
//* ------------------------------------------------------------------------
//* Rfc822DateTimeParser - Parse Date-Time In RFC-822 Format
//* Copyright (C) 2009 Tom Dong
//*
//* This library is free software; you can redistribute it and/or
//* modify it under the terms of the GNU Lesser General Public
//* License as published by the Free Software Foundation; either
//* version 2.1 of the License, or (at your option) any later version.
//* ------------------------------------------------------------------------
@tomtung
tomtung / Week1 - Ruby.rb
Created June 13, 2012 08:45
Seven Languages in Seven Weeks
#!/usr/bin/ruby
############# Day 1 #############
# Print the string “Hello, world.”
puts 'Hello, world'
# For the string “Hello, Ruby”, find the index of the word“Ruby”.
puts 'Hello, Ruby'.index('Ruby')
@tomtung
tomtung / gist:3765178
Created September 22, 2012 05:00
a parser and interpreter for the CFWAE language
;; http://www.cs.brown.edu/courses/csci1730/2012/Assignments/Interpreter/
#lang plai
(define-type Binding
[binding (name symbol?) (named-expr CFWAE?)])
(define-type CFWAE
[num (n number?)]
[binop (op procedure?) (lhs CFWAE?) (rhs CFWAE?)]
@tomtung
tomtung / example.scala
Created October 23, 2012 08:13
Forward-backward Algorithm For Part-of-speech Tagging
// Example: distinguishing between vowels and consonants
val corpus =
Seq( // I swear these are all English words
"antidisestablishmentarian", "ablutophobia", "automatonophobia", "autotonsorialist",
"arachibutyrophobia", "automysophobia", "batrachophagous", "ballistocardiograph",
"blandiloquence", "brachydactylous", "bathythermograph", "cacodemomania",
"caesaropapism", "catapedamania", "cheiloproclitic", "chronosynchronicity",
"dendrochronology", "deorsumversion", "dermatoglyphics", "dolichocephalic",
"dysmorphophobia", "eellogofusciouhipoppokunurious", "electroencephalograph", "epiphenomenalism",
@tomtung
tomtung / SRM144-BinaryCode.cs
Created November 12, 2012 08:27
TopCoder Algorithm
using System.Text;
public class BinaryCode
{
public string[] decode(string message)
{
int[] messageInts = new int[message.Length];
{
for (int i = 0; i < message.Length; i += 1)
messageInts[i] = message[i] - '0';
@tomtung
tomtung / learn-vocabulary.scala
Created December 9, 2012 07:50
Unsupervised Chinese Vocabulary Extraction
#!/usr/bin/env scalas
!#
/***
libraryDependencies ++= Seq(
"org.apache.commons" % "commons-lang3" % "3.1",
"com.github.scopt" %% "scopt" % "2.1.0"
)
resolvers += "sonatype-public" at "https://oss.sonatype.org/content/groups/public"
@tomtung
tomtung / CreateInstances.scala
Created February 2, 2013 22:35
This is an example script translated from wekaexamples.core.CreateInstances, which generates an weka.core.Instances object with different attribute types, and prints it out in ARFF format.
import java.util
import weka.core.{DenseInstance, Attribute, Instances}
import scala.collection.convert.wrapAll._
// 1. set up attributes
val atts = {
// numeric
val att1 = new Attribute("att1")
// nominal
val att2 = new Attribute("att2", (1 to 5).map("val" + _))
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
import itertools,sys
class WinterAndPresents:
def getNumber(self, apple, orange):
"""This is an O(N) solution to SRM 601 Div 1 Problem 250.
The derivation is as follows.
Let :math:`a_i` and :math:`o_i` be the number of apples and oranges in bag :math:`i`, respectively.
Let :math:`x_m` denote the largest possible value of :math:`x`.
Given any :math:`x`, the number of different presents :math:`n(x)` can be written as: