Skip to content

Instantly share code, notes, and snippets.

@josephok
josephok / string_demo.c
Created August 15, 2021 05:30
c string implementation
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
const size_t INITIAL_SIZE = 16;
typedef struct String
{
size_t len;
@josephok
josephok / .jshintrc
Created March 7, 2017 07:11 — forked from zhenyong/.jshintrc
jshint配置中文注释
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details
//jshint options document: [http://jshint.com/docs/options/#strict](http://jshint.com/docs/options/#strict)
"maxerr" : 50, // {int} 超过{int}个错误后,停止检测错误
// Enforcing
"bitwise" : false, // true: 禁止使用位逻辑符 (&, |, ^, ...)
"camelcase" : false, // true: 变量名必须是驼峰风格
@josephok
josephok / daemon.c
Last active December 2, 2016 10:51
daemon process
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <fcntl.h>
int my_daemon() {
int maxfd, fd;
@josephok
josephok / ip.py
Created November 3, 2016 12:11
python socket
import socket
BUF_SIZE = 4096
HOST = 'httpbin.org'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, 80))
req_msg = [
b'GET /ip HTTP/1.1',
@josephok
josephok / pid.py
Created November 2, 2016 14:22
python fcntl文件锁
import fcntl
import time
pidfile = "1.pid"
with open(pidfile, "w") as f:
try:
fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
@josephok
josephok / sqrt.js
Created October 25, 2016 04:36
牛顿法求平方根
// 牛顿法求 平方根
// 如果要求s(s>1)的平方根, 选取1< X0 <s
// Xn+1 = (1/2) * (Xn + S / Xn)
const print = console.log
function sqrt(s) {
let [x0, i] = [1, 0]
while (i++ < 10) {
x = (1/2.0) * (x0 + s/x0)
@josephok
josephok / hltv.js
Last active January 22, 2017 04:38
hltv results score
const request = require('request')
const cheerio = require('cheerio')
const r = request.defaults({'proxy': 'http://127.0.0.1:8087'})
const URL = 'http://www.hltv.org'
const print = console.log
r(`${URL}/results/`, function (error, response, body) {
if (!error && response.statusCode == 200) {
print("----------------------------------")
@josephok
josephok / cheerio.js
Created October 23, 2016 07:41
request use nodejs
const request = require('request'),
cheerio = require('cheerio'),
colors = require('colors')
const log = console.log
const URL = 'https://nqdeng.github.io/7-days-nodejs/'
request(URL, (error, response, body) => {
if (!error && response.statusCode == 200) {
@josephok
josephok / dns.js
Created October 23, 2016 07:03
dns query use nodejs
const print = console.log
const dns = require('dns')
const hostname = 'v2ex.com'
Promise.prototype.success = Promise.prototype.then
Promise.prototype.error = Promise.prototype.catch
function lookup(hostname) {
return new Promise(function (resolve, reject) {
@josephok
josephok / freemin.awk
Created August 28, 2016 04:11
The smallest free ID problem
function swap(A, left, right, t) {
t = A[left]
A[left] = A[right]
A[right] = t
}
function qsort(A, left, right, pivot, i, j) {
if (left < right) {
# select a pivot element
pivot = left