Skip to content

Instantly share code, notes, and snippets.

View hiroshi-maybe's full-sized avatar

Hiroshi Kori hiroshi-maybe

  • San Jose, CA, United States
View GitHub Profile
@hiroshi-maybe
hiroshi-maybe / circular-reference-detect.js
Created February 19, 2013 10:38
Detect circular reference of linked list in Javascript.
/*
* circular-reference-detect.js
* author: Hiroshi Kori
*
* Detect circular reference of linked list
*
* 1) Overview
* - Reverse linked-list until finding a node whose next pointer is null.
* - Chekck the head node if its next pointer is null
* 1) null: the node is not traversed twice. (no circular reference)
@hiroshi-maybe
hiroshi-maybe / suffix_array.js
Last active December 14, 2015 11:38
Extract suffix array from string.
var suffix_array = (function(str){
var create_suffixes = function(arg_str) {
var suffixes = [];
for (var i=0, length=arg_str.length; i<length; i+=1) {
var work="";
for (var j=i; j<length; j+=1) {
work+=arg_str.charAt(j);
}
suffixes[i] = {string:work, index:i};
@hiroshi-maybe
hiroshi-maybe / MyArrayList.rb
Last active December 14, 2015 16:29
Reinvention of array list.
class MyArrayList
def initialize n
if n<0 then
raise "size should be more than 0";
end
@size=n
@next_index=0
@innerArray=Array.new @size
end
@hiroshi-maybe
hiroshi-maybe / recursive_power_set.js
Created March 11, 2013 00:46
Provide power set with recursive function calls.
var power_set = function (set) {
var array=[[]],
sub_set = function(n,m) {
// should return 2 dimensions array
var result = [];
if (m==1) {
result.push([set[n]]);
} else {
for(var i=n, length=set.length; i<length-1; i+=1) {
@hiroshi-maybe
hiroshi-maybe / power_set_generator.js
Created March 11, 2013 02:18
Power set generator.
var power_set_generator = function(set) {
var _index=0, _set=set, _max_size= 1<<set.length;
return Object.create(Object.prototype, {
next : { value : function() {
var result = [], n=_index,i=0;
if (_index >= _max_size) return;
for (; n; n>>=1, i+=1) if (n & 1) result.push(_set[i]);
_index+=1;
return result;
}}
@hiroshi-maybe
hiroshi-maybe / infinite_newton_raphson_list.hs
Created March 23, 2013 10:09
Generate infinite list of Newton-Raphson method
repApply :: (a->a)->a->[a]
repApply f x = x : repApply f (f x)
newton :: Int -> [Double]
newton n = repApply (\x -> (x+(fromIntegral n)/x)/2) 1
@hiroshi-maybe
hiroshi-maybe / split_int.hs
Created March 30, 2013 08:41
Split an integer to a set of integers (i.e. split_int 5 3 -> [1,1,3], [1,2,2])
split_int :: Int -> Int -> [[Int]]
split_int n set_num = split_int_helper n set_num 1
split_int_helper n 1 _ = [[n]]
split_int_helper n set_num start_idx = do
let last_idx = truncate $ (fromIntegral n)/(fromIntegral set_num)
x <- [start_idx..last_idx]
y <- split_int_helper (n-x) (set_num-1) x
return (x:y)
andThen f1 f2 = do f1
f2
--foo bar = (print "Executing foo") `andThen` (print bar) `andThen` (do return (length bar))
-- Container Thing
data Thing a = Thing a deriving Show
value :: Thing a -> a
value (Thing x) = x
import Control.Monad.Fix
------------------------------------------------
-- fact
-- fact is a fixpoint for lambda_fact
fact n = if n == 0 then 1 else n * fact (n-1)
lambda_fact = (\g n -> if n == 0 then 1 else n * g (n-1))
-- fixpoint combinator 'fix' "encode" recursion of fact
@hiroshi-maybe
hiroshi-maybe / isSublist.hs
Created May 1, 2013 08:49
A function to check sublist.
isSublist :: (Eq a) => [a] -> [a] -> Bool
isSublist [] _ = True
isSublist _ [] = False
isSublist (x:xs) ys = case dropWhile (/=x) ys of
[] -> False
(x:rest) -> isSublist xs rest