Skip to content

Instantly share code, notes, and snippets.

View binary_search_symbol_table.rb
#!/usr/bin/env ruby
class BSST
def initialize(capacity = 2)
@keys = Array.new(capacity)
@vals = Array.new(capacity)
@n = 0
end
def is_empty?
@n == 0
@yiyizym
yiyizym / calculate_kodomo.rb
Last active Dec 14, 2016
从 1% 的样本中评估总体的平均值
View calculate_kodomo.rb
#!/usr/bin/env ruby
#生成 10_000 个呈正态分布的范围在0-4之间的随机数
#计算他们的平均值
#从中随机抽取100个数
#通过有放回重抽法尝试 10_000 次
#每次抽 100 个数
#计算这100个数的平均值
#计算这 10_000 次的平均值
@yiyizym
yiyizym / bst.rb
Last active Dec 19, 2016
binary search tree
View bst.rb
#!usr/bin/env ruby
class BST
def initialize
end
def get key
node = _get(root, key)
node ? node.val : node
end
View particle_system.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>particle</title>
<style>
body {
margin: 0;
padding: 0;
}
@yiyizym
yiyizym / migrate.rb
Created Feb 3, 2017
migrate from hexo to jekyll
View migrate.rb
# encoding: UTF-8
#!/usr/bin/env ruby
Dir.chdir('_posts') do
Dir.glob('*.markdown') do |filename|
File.open(filename, mode: 'r+:UTF-8') do |file|
content = file.read(file.size)
# date = content.match(/date: (\d+-\d+-\d+)/)
# if date.nil?
# puts filename
View deep_merge.js
'use strict';
function deepMerge(){
let args = [].slice.call(arguments);
let target = args.shift();
let sources = args;
if(sources.length == 0){
return target;
}
else {
@yiyizym
yiyizym / flatten_array.js
Created Feb 17, 2017
flatten array in javascript
View flatten_array.js
var flatten = (function(){
var result = [];
var _innerFlatten = function(arr){
if(arr.length === 0){
return [];
}
arr.forEach(item => {
console.log(item)
console.log('>>>')
if(isArray(item)){
View isArray.js
function isArray(obj){
if(Array.prototype.isArray){
return Array.prototype.isArray(obj)
}
return Object.prototype.toString.call(obj) === '[object Array]'
}
@yiyizym
yiyizym / formatString.js
Created May 25, 2017
一道字符串面试题
View formatString.js
var str = 'abc';
var nStr = str.replace(/\w/g,function(m,offset,s){
return (offset ? '-' : '') + m.repeat(offset+1).replace(/(\w)/,(m)=>m.toUpperCase());
})
console.log(nStr);
// A-Bb-Ccc