Skip to content

Instantly share code, notes, and snippets.

View seckcoder's full-sized avatar

Wei Li seckcoder

  • Blablabla
  • San Francisco
View GitHub Profile
#include <iostream>
#include <vector>
using namespace std;
int big_average(const vector<int> &vec) {
// we want sum(vec) / vec.size()
int a = vec.size(), quotient = 0, remainder = 0;
def calc(k,p,alpha):
u = 1.0 - 1.0 / float(1<<31)
p_actual = k / pow(1.0 + u * (pow(k / p, alpha) - 1), 1.0/alpha)
print "p actual:", p_actual
def E_X(r, p):
C_a = alpha * pow(k, alpha) / (1 - (pow(k/p,alpha)))
return (C_a / (r - alpha)) * (pow(p, r-alpha) - pow(k,r-alpha))
def C(ex1, ex2):
return ex2 / ex1 - 1
E_X_1_actual = E_X(1, p_actual)
#lang racket
(require parser-tools/lex
(prefix-in re- parser-tools/lex-sre)
parser-tools/yacc)
(provide (all-defined-out))
(define-tokens a (NUM VAR))
(define-empty-tokens b (+ - EOF LET IN))
(define-lex-trans number
(syntax-rules ()
@seckcoder
seckcoder / api.js
Last active December 10, 2015 02:28 — forked from JacksonTian/api.js
var events = require('events');
var callA = function (callback) {
setTimeout(function () {
callback({name: "a", data: "I am result A"});
}, Math.round(Math.random() * 300));
};
var callB = function (callback) {
setTimeout(function () {
callback({name: "b", data: Math.round(Math.random() * 300) % 2 === 0});
@seckcoder
seckcoder / async_fetch_weibo.py
Last active December 10, 2015 06:58
Crawl weibo(repost timeline) asynchronously in python. It needs gevent and sina python weibo client(https://github.com/michaelliao/sinaweibopy) installed.
import math
import gevent
import gevent.greenlet
from gevent import monkey
from functools import partial
monkey.patch_all()
from weibo import APIClient
import time
class ActionCounter:
@seckcoder
seckcoder / gist:5575284
Last active December 17, 2015 07:48
从一个字符串中找到从右往左数第3个下划线的index
import itertools
a = "abc_d_e_f"
print list(itertools.islice((i for i,c in enumerate(reversed(a)) if c == '_'), 3))[2]
def take(n, iterable):
return list(itertools.islice(iterable, n))
print take(3, (i for i,c in enumerate(reversed(a)) if c == '_'))[2]
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
@seckcoder
seckcoder / gist:5697196
Last active December 18, 2015 00:29
redis transaction
import redis
client = redis.StrictRedis()
client.flushdb()
def incr(pipe):
value = pipe.get('key')
value = int(value) + 1
pipe.multi()
pipe.set('key', value)
@seckcoder
seckcoder / gist:5782977
Created June 14, 2013 15:54
A demo for redis transation's strong exception guarantee.
"""
A demo for redis transation's strong exception guarantee.
"""
import logging
import redis
client = redis.StrictRedis(host="127.0.0.1",
port=6379,
db=1)
# Copyright 2013 Jike Inc. All Rights Reserved.
# Author: liwei@jike.com
from gevent import monkey
monkey.patch_all()
from gevent.pool import Pool
import time
from weibo_offline_base.ttypes import PlatForm
from hbase_rabbitMQ_interface import HbaseRabbitMQ