Skip to content

Instantly share code, notes, and snippets.

View sing1ee's full-sized avatar
🏠
Working from home

zhangcheng sing1ee

🏠
Working from home
View GitHub Profile
@sing1ee
sing1ee / gist:3074072
Created July 9, 2012 03:30
read Cassandra index file with workerpool
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import struct
import os
import redis
import mmh3
import sys
import workerpool
import logging
@sing1ee
sing1ee / gist:3087394
Created July 11, 2012 01:40
practise for pycassa and multiprocessing
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import pycassa
from multiprocessing import Pool
cassa = pycassa.ConnectionPool('test', server_list=['10.1.1.41'])
cf = pycassa.ColumnFamily(cassa, 'data')
@sing1ee
sing1ee / gist:3087416
Created July 11, 2012 01:44
double stack queue
# !/usr/bin/python
# -*- encoding: utf-8 -*-
class DoubleStackQueue:
def __init__(self):
self.first_stack = []
self.second_stack = []
def en_queue(self, el = None):
@sing1ee
sing1ee / gist:3087422
Created July 11, 2012 01:45
median of medians select
#include <iostream>
using namespace std;
int select(int array[], int left, int right, int k) {
int len = right - left;
if (len <= 10) {
for (int i = left + 1; i < right; ++i) {
for (int j = i; j > left; --j) {
if (array[j] < array[j - 1]) {
@sing1ee
sing1ee / gist:3087430
Created July 11, 2012 01:47
quick sort
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
def quick_sort_1(array, start, end):
if start > end:
return
t = array[start]
t_index = start
@sing1ee
sing1ee / gist:3087500
Created July 11, 2012 02:08
permutation dfs
# !/usr/bin/python
# -*- encoding: utf-8 -*-
string_list = ['a', 'b', 'c', 'd', 'e']
used = [0, 0, 0, 0, 0]
def dfs_permutation(permu, l):
if l == 3:
print permu
for i in range(len(string_list)):
if 0 == used[i]:
@sing1ee
sing1ee / gist:3087515
Created July 11, 2012 02:15
implement dict by murmur hash
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import mmh3
class MurmurDict:
def __init__(self, capacity = 16, load_factor = 0.75):
self.capacity = capacity
self.load_factor = load_factor
self.table = []
@sing1ee
sing1ee / gist:3087523
Created July 11, 2012 02:18
(()()) recursive implement
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
array = list(sys.argv[1])
def n_pair(array):
if len(array) == 0:
return True
@sing1ee
sing1ee / gist:3087543
Created July 11, 2012 02:23
Dynamic programming implementation for (()())
# !/usr/bin/python
# -*- encoding: utf-8 -*-
import sys
array = list(sys.argv[1])
l = len(array)
dp = []
for i in range(l):
dp.append([])
@sing1ee
sing1ee / gist:3087549
Created July 11, 2012 02:26
put odd before even
# !/usr/bin/python
# -*- encoding: utf-8 -*-
array = [1, 2, 3, 4 ,5 ,6, 7, 8, 9]
odd_pos = 0
for index in range(len(array)):
if array[index] & 1 == 1: