Skip to content

Instantly share code, notes, and snippets.

@zhjgithub
zhjgithub / v2ray
Created June 3, 2019 22:57
centos 6 v2ray
#!/bin/sh
#
# v2ray Startup script for v2ray
#
# chkconfig: - 24 76
# processname: v2ray
# pidfile: /var/run/v2ray.pid
# description: V2Ray proxy services
#
@zhjgithub
zhjgithub / multi-reader-single-writer-lock
Last active December 10, 2018 14:12
Multiple readers and single writer lock
Mutex counter_mutex;
Condition read_phase, write_phase;
int resource_counter = 0;
// READERS
Lock(counter_mutex) {
while(resource_counter == -1)
Wait(counter_mutex, read_phase);
resource_counter++;
} // unlock
@zhjgithub
zhjgithub / requestAnimationFrame-callback-with-lastTime.js
Created October 23, 2018 13:05
requestAnimationFrame callback with lastTime
let cat = document.querySelector("img");
let angle = Math.PI / 2;
function animate(time, lastTime) {
if (lastTime != null) {
angle += (time - lastTime) * 0.001;
}
cat.style.top = (Math.sin(angle) * 20) + "px";
cat.style.left = (Math.cos(angle) * 200) + "px";
requestAnimationFrame(newTime => animate(newTime, time));
}
@zhjgithub
zhjgithub / matrix-iterable.js
Created October 16, 2018 14:25
matrix, iterator and symmetric matrix
class Matrix {
constructor(width, height, element = (x, y) => undefined) {
this.width = width;
this.height = height;
this.content = [];
for(let y = 0; y < height; y++) {
for(let x = 0; x < width; x++) {
this.content[y * width + x] = element(x, y);
}
}
@zhjgithub
zhjgithub / phi-coefficient.js
Created October 15, 2018 08:43
To compute the measure of correlation between two Boolean variables. The output of the formula is a number between -1 and 1 that describes the correlation.
function phi([n00, n01, n10, n11]) {
return (n11 * n00 - n10 * n01) /
Math.sqrt((n10 + n11) *
(n00 + n01) *
(n01 + n11) *
(n00 + n10));
}
@zhjgithub
zhjgithub / FizzBuzz.js
Created October 14, 2018 05:45
FizzBuzz
for (let n = 1; n <= 100; n++) {
let output = '';
if(n % 3 === 0) output += 'Fizz';
if(n % 5 === 0) output += 'Buzz';
console.log(output || n);
}
package main
import "golang.org/x/tour/pic"
func Pic(fun func(x, y int) int) func(dx, dy int) [][]uint8 {
f := func(dx, dy int) [][]uint8 {
p := make([][]uint8, dy)
for i := 0; i < dy; i++ {
p[i] = make([]uint8, dx)
for j := 0; j < dx; j++ {
@zhjgithub
zhjgithub / sqrt.go
Last active January 24, 2018 13:16
compute square root using formula z -= (z*z - x) / (2*z) called Newton's method
package main
import (
"fmt"
)
func Sqrt(x float64) float64 {
z := 1.0
for i := 0; i < 10; i++ {
z -= (z*z - x) / (2 * z)
// ==UserScript==
// @name Udacity Chinese courses video playback controller
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Enable forward/backward and play/pause for current mouse hovering video if more than one video tag on the page.
// @author organism <neuqzhj@gmail.com>
// @source https://gist.github.com/zhjgithub/38bff8b658af77b7c102a69e37b2f120
// @match https://classroom.udacity.com/*
// @run-at document-end
// @grant none
@zhjgithub
zhjgithub / README-Template.md
Created December 29, 2017 14:47 — forked from PurpleBooth/README-Template.md
A template to make good README.md

Project Title

One Paragraph of project description goes here

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See deployment for notes on how to deploy the project on a live system.

Prerequisites