Skip to content

Instantly share code, notes, and snippets.

View Moligaloo's full-sized avatar

Moligaloo Moligaloo

  • ByteDance
  • Shanghai, China
View GitHub Profile
@Moligaloo
Moligaloo / hex2bin.io
Last active August 29, 2015 13:55
Convert hex characters to binary data
#!/usr/bin/env io
File standardInput contents split(" ") map(fromBase(16) asCharacter) prepend("" asMutable) reduce(appendSeq) println
@Moligaloo
Moligaloo / Levenshtein.io
Last active August 29, 2015 13:55
Get the Levenshtein distance follow the algorithm at : http://en.wikipedia.org/wiki/Levenshtein_distance
#!/usr/bin/env io
Matrix := Object clone do(
with := method(row, column,
self clone do(
row ::= 0
column ::= 0
) setRow(row) setColumn(column) do(
data := list() preallocateToSize(size)
size repeat(data push(nil))
@Moligaloo
Moligaloo / installpg.sh
Created February 10, 2014 04:23
Install gp gem module under Mac OS X Mavericks
#!/bin/sh
# download Postgres app at http://postgresapp.com/ and put it in /Applications folder and rename it to Postgres.app
sudo env ARCHFLAGS="-arch x86_64" gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/MacOS/bin/pg_config
@Moligaloo
Moligaloo / FindMinimunInLoopAscendingSequence.io
Last active August 29, 2015 13:56
Finding minimum of a loop ascending sequence
#!/usr/bin/env io
// finding minimum of a loop ascending sequence
findmin := method(seq,
left := 0
right := seq size -1
loop(
leftValue := seq at(left)
@Moligaloo
Moligaloo / LetterRecognition.rb
Created February 27, 2014 06:13
Example of letter recognition by comparing the similarity of continous point track (track database is saved in JSON format)
#!/usr/bin/env ruby
require 'JSON'
class Point
attr_reader :x,:y
def initialize(x,y)
@x = x
@y = y
end
@Moligaloo
Moligaloo / isNarcissistic.io
Last active August 29, 2015 13:56
Check a number is a narcissistic number or not, and print the narcissistic number in [0,1000] the detailed definition of a narcissistic number is at http://zh.wikipedia.org/wiki/%E6%B0%B4%E4%BB%99%E8%8A%B1%E6%95%B0
#!/usr/bin/env io
Number isNarcissistic := method(base,
base ifNil(base = 10)
digits := self toBase(base) asList map(fromBase(base))
self == digits map(** digits size) sum
)
# print the narcissistic number in range [0, 1000] (decimal)
Range 0 to(1000) asList select(isNarcissistic) println
@Moligaloo
Moligaloo / kNN.io
Last active August 29, 2015 13:57
kNN algorithm implemented by Io, first example in "Machine Learning in Action"
#!/usr/bin/env io
kNN := method(inX, dataSet, k,
dataSet map(x, list(x first, x second distanceTo(inX))) \
sortBy(block(a, b, a second < b second)) setSize(k) map(first) \
uniqueCount sortBy(block(a, b, a second > b second)) first first
)
dataSet := list(
list("A", vector(1, 1.1)),
@Moligaloo
Moligaloo / id3.io
Last active August 29, 2015 13:57
ID3 Decision Tree making implemented with Io
#!/usr/bin/env io
# calculate the information gain(entropy) by Claude Shannon
# input like list("a", "b", "a", "c", ...)
List shannonEntroy := method(
self uniqueCount map(last / self size) map(p, p * (- p log2)) sum
)
# => 0.97....
@Moligaloo
Moligaloo / bayes.io
Last active August 29, 2015 13:57
Naive Bayes classification, example comes from http://www.ruanyifeng.com/blog/2013/12/naive_bayes_classifier.html
#!/usr/bin/env io
# naive bayes classification
# 性别 身高(英尺) 体重(磅)  脚掌(英寸)
## M 6 180 12
## M 5.92 190 11
## M 5.58 170 12
## M 5.92 165 10
## F 5 100 6
@Moligaloo
Moligaloo / stat.rb
Last active August 29, 2015 13:58
Extract statistics (currently includes area, population and GDP(PPP) average) of countries all over the world, data from wikipedia: http://en.wikipedia.org/wiki/Lists_of_countries_and_territories and output json code to standard output, you can use http://chris.photobooks.com/json/default.htm to view the generated result
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'json'
def get_doc(url, localfile)
filename = File.exists?(localfile) ? localfile : url
Nokogiri::HTML(open(filename))
end