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 / 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 / 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 / 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 / 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 / insertionSort.js
Created February 3, 2013 09:57
Insertion sort implemented with Javascript.
var insertionSort = function(array) {
for(var i=1, length=array.length; i<length; i+=1) {
var insertion = array[i], j=i;
if (insertion < array[i-1]) {
do {
array[j] = array[j-1];
j-=1;
} while (array[j] > insertion && j > 0)
array[j] = insertion;
}
@hiroshi-maybe
hiroshi-maybe / selectionSort.js
Created February 3, 2013 09:25
Selection sort implemented with Javascript.
var selectionSort = function(array) {
for(var i=0, length=array.length; i<length; i+=1) {
var min_i = i;
for(var j=i+1; j<length; j+=1) {
if (array[j] < array[min_i]) {
min_i = j;
}
}
var small = array[min_i];
array[min_i] = array[i];
@hiroshi-maybe
hiroshi-maybe / combSort.js
Created February 3, 2013 09:13
Comb sort implemented with Javascript.
var combSort = function (array) {
var interval = Math.floor(array.length/1.3);
while (interval > 0) {
for(var i=0; i+interval<array.length; i+=1) {
if (array[i] > array[i+interval]) {
var small = array[i+interval];
array[i+interval] = array[i];
array[i] = small;
}
}
@hiroshi-maybe
hiroshi-maybe / bubbleSort.js
Created January 26, 2013 11:57
Bubble sort implemented with Javascript.
var bubbleSort = function(array) {
for (var i=0, length=array.length; i<length; i+=1) {
for (var j=0; j<array.length-i-1; j+=1) {
if (array[j]>array[j+1]) {
var smallNum = array[j+1];
array[j+1]=array[j];
array[j]=smallNum;
}
}
}
@hiroshi-maybe
hiroshi-maybe / sleepSort.js
Created January 26, 2013 11:09
Sleep sort implemented with Javascript.
var sleepSort = function(array){
for (var i=0, length=array.length; i<length; i+=1) {
setTimeout((function(n){
return function(){
console.log(n);
};
})(array[i]), array[i]*100);
}
};
@hiroshi-maybe
hiroshi-maybe / todo.hs
Created December 29, 2012 11:07
Refined todo.hs in Chap 9 of "Learn You a Haskell for Great Good!": http://learnyouahaskell.com/
import System.Environment
import System.Directory
import System.IO
import Data.List
import Control.Exception
dispatch :: String -> [String] -> IO ()
dispatch "add" = add
dispatch "view" = view
dispatch "remove" = remove