Skip to content

Instantly share code, notes, and snippets.

View dchentech's full-sized avatar
🎯
Focusing

David Chen dchentech

🎯
Focusing
View GitHub Profile
console.log num for num in [10..1]
@dchentech
dchentech / weight_n_height.R
Created April 15, 2013 01:17
《Exploring Everyday Things with R and Ruby》学习记录
height <- c(58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72) weight <- c(115, 117, 120, 123, 126, 129, 132, 135, 139, 142, 146, 150, 154,
159, 164) print(mean(height))
print(sd(weight)) print(cor(weight, height)) plot(weight, height)
@dchentech
dchentech / .gitignore
Last active December 15, 2015 12:58
Ruby-China相似性文章测试
*.log
*.lock
@dchentech
dchentech / Ackermann.ss
Last active December 15, 2015 04:58
《计算机编程的构造和解释》学习记录
; 1.2
; http://en.wikipedia.org/wiki/Ackermann_function
; TODO 了解
(define (A x y)
(cond ((= y 0) 0)
((= x 0) (* 2 y))
((= y 1) 2)
(else (A (- x 1)
(A x (- y 1))))))
@dchentech
dchentech / trie_vs_hash_performance.rb
Last active December 21, 2020 15:55
Trie和Hash察看单个字符串性能比较
%w[benchmark algorithms open-uri nokogiri].map &method(:require)
# 摘自wikipedia的trie词条
# 在计算机科学中,trie,又称前缀树,是一种有序树,用于保存关联数组,其中的键通常是字符串。与二叉查找树不同,键不是直接保存在节点中,而是由节点在树中的位置决定。一个节点的所有子孙都有相同的前缀,也就是这个节点对应的字符串,而根节点对应空字符串。一般情况下,不是所有的节点都有对应的值,只有叶子节点和部分内部节点所对应的键才有相关的值。
# 摘自wikipedia的Hash词条
# 散列函数(或散列算法,英语:Hash Function)是一种从任何一种数据中创建小的数字“指纹”的方法。散列函数把消息或数据压缩成摘要,使得数据量变小,将数据的格式固定下来。该函数将数据打乱混合,重新创建一个叫做散列值的指纹。散列值通常用来代表一个短的随机字母和数字组成的字符串。好的散列函数在输入域中很少出现散列冲突。在散列表和数据处理中,不抑制冲突来区别数据,会使得数据库记录更难找到。
# 用同样的字符串数组来各自构造一万以上元素的Hash和Trie
string_array_for_insert = Nokogiri(open('http://www.douban.com').read).text.split("\n").map(&:strip).uniq
string_array_for_insert = (string_array_for_insert*100).map {|i| i.split(//).shuffle.join }
@dchentech
dchentech / cache_control.markdown
Created December 13, 2012 10:08
Unicorn和Nginx部署Rails应用的css, js, img等静态资源缓存使用总结

接上文 Rails动态文件添加ETag缓存 来自动生成304 Not Modified告诉客户端来直接使用本地缓存,但是在这种情况下会出现图片闪动(重新加载渲染),影响用户体验。除了Etag这种通过和服务器进行HTTP头部比对来实现缓存的策略外,还可以采用Cache-control来通知浏览器在一段时间内不必重新请求服务器而直接本地缓存。配置如下:

nginx.conf

使用root用户来避免文件权限问题而导致403错误

user root nginx;

在server { } 里加入Cache-Control声明

location ~ ^/(assets)/ {
gzip_static on;
expires 24h;
add_header Cache-Control public;

@dchentech
dchentech / html5shiv.haml
Created December 10, 2012 02:17
HAML引用html5shiv.js
/[if lt IE 9]
%script{:src=>"html5shiv/dist/html5shiv.js"}
@dchentech
dchentech / css.markdown
Created December 5, 2012 09:24
css hack技巧

div height in percent, and unwanted scrollbar http://stackoverflow.com/a/10607261/595618

if you want to use % as height you have to put that in every parent element:

maybe you could set the container absolute, but that not really a proper coding style.

页面加载时整个页面瞬间闪动

把外引js加载放到页面最下方即可

@dchentech
dchentech / etag.rb
Created November 30, 2012 09:12
Rails动态文件添加ETag缓存
# 项目提供动态的文件下载,由于文件系统里的位置因加了自定义的目录分级,而与和URL有偏差,所以需要经过控制器来重写文件路径来读取,在此也保证了当前请求静态文件在磁盘上只有一份以节约空间。
# 为了避免单个客户端多次请求同一个文件,在该控制器方法里加上HTTP头部的ETag来进行304缓存。
def file
response.headers['Etag'] = Digest::SHA1.hexdigest(@your_file_instance)
send_file @your_file_instance.path
end
@dchentech
dchentech / php.markdown
Created November 30, 2012 07:15
osx搭建think php解决方案

10.6 osx 安装php

  1. 配置apache虚拟主机信息, 往 /etc/apache2/extra/httpd-vhosts.conf 写入项目目录配置 <VirtualHost *:80> ServerAdmin [email protected] DocumentRoot "/Users/mvj3/eoemobile/code/eoe" ServerName edu.eoe.lo ErrorLog "/private/var/log/apache2/dummy-host2.example.com-error_log" CustomLog "/private/var/log/apache2/dummy-host2.example.com-access_log" common
2. 配置apache, 编辑 /etc/apache2/httpd.conf