Skip to content

Instantly share code, notes, and snippets.

View GZShi's full-sized avatar
🎯
Focusing

Guozhong Shi GZShi

🎯
Focusing
View GitHub Profile
@GZShi
GZShi / stringifyJSON.js
Created March 19, 2014 03:22
Stringing JSON for stupid browsers
function stringifyJSON(obj) {
var keyValuePair = [];
switch(Object.prototype.toString.call(obj)) {
case '[object Object]':
for(var i in obj) {
if(obj.hasOwnProperty(i)) {
keyValuePair.push('"' + i + '":' + stringifyJSON(obj[i]));
}
}
@GZShi
GZShi / convert.py
Created March 18, 2014 07:40
批量GBK转UTF8
import os, sys
def convert(filepath, in_encode = "GBK", out_encode = "UTF8"):
try:
in_content = open(filepath).read()
out_content = in_content.decode(in_encode).encode(out_encode)
open(filepath, 'w').write(out_content)
print "[OK] Convert " + filepath
except:
print "[ERROR] Convert " + filepath
@GZShi
GZShi / assert.js
Last active August 29, 2015 13:57
function assert(expectation, fn) {
var args = Array.prototype.slice.call(arguments, 2);
var ret = fn.apply(fn, args);
if(ret === expectation) {
return ret;
} else {
throw new TypeError("调用" + fn.name +
"函数时,返回值" + ret + "与预期的" + expectation + "不同");
}
@GZShi
GZShi / benchmark.js
Last active August 29, 2015 13:57
不服跑分。for-loop VS. Array.map VS. Array.every
function benchmark (times) {
var index = Math.floor(Math.random()*times);
function fn (elem, i, array) {
array[i] = Math.sqrt(array[i]);
return true;
}
var a = [];
var b = [];
@GZShi
GZShi / function_cache.js
Created February 28, 2014 08:35
Function caching in JavaScript
function fibonacci (n) {
if (n < 2) {
return n;
} else if (fibonacci.cache)
fibonacci.cache[n] = fibonacci.cache[n] || fibonacci(n-1) + fibonacci(n-2);
return fibonacci.cache[n];
} else {
return fibonacci(n-1) + fibonacci(n-2);
}
}
@GZShi
GZShi / eight_queens_puzzle.js
Last active August 29, 2015 13:56
One solution to the eight queens puzzle in JavaScript
var solves = [];
var ans = [];
function solve_eight_queens (ans, line) {
if(line >== 8) {
solves.push(ans);
return true;
} else {
for (var row = 0; row < 8; ++row) {
var fine = true;
@GZShi
GZShi / checkChineseId.js
Created November 8, 2013 10:24
根据 GB11643-1999 的规则对身份证号码正确性进行校验
/*
* 根据GB11643-1999的规则对身份证号码正确性进行校验
* 如果身份证中的日期信息在系统时间之后,也不能通过校验
*/
function checkChineseID(id) {
id += '';
if(/^\d{15}(\d\d[0-9X])?$/.test(id)) {
var year = 0,
month = 0,
@GZShi
GZShi / RotationMatrix.html
Last active December 26, 2015 00:58
旋转矩阵
<html>
<head>
<title>test rotation</title>
<meta charset='utf-8'>
<script>
var PI_2 = Math.PI * 2;
var alpha = Math.PI / 180;
var cx = 400;
var cy = 300;
var ctrlRotation = 1;
@GZShi
GZShi / bezier.js
Last active December 20, 2015 19:19
绘制贝塞尔曲线
(function(){
var canvas = document.getElementById('c');
var ctx = canvas.getContext('2d');
var mouse = {
x: 0,
y: 0,
fx: 0,
fy: 0,
left: false,
@GZShi
GZShi / chrome_arc.html
Created August 7, 2013 14:01
Chrome arc函数采用贝塞尔曲线拟合圆
<html>
<head>
<title>chrome arc</title>
<meta charset='utf-8'>
</head>
<body>
<canvas id='c' width='2000' height='1500'></canvas>