This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |