Skip to content

Instantly share code, notes, and snippets.

@cuixin
cuixin / EvenPower.java
Created June 15, 2012 07:27
是否是2的N次幂
public class Is2Power {
public static void main(String[] args) {
for (int i = 0;i <= 1024; i++) {
if ((i & -i) == i)
System.out.println(Integer.toBinaryString(i) + ", " + Integer.toBinaryString(-i) + " " + i);
}
}
}
@cuixin
cuixin / gist:3395915
Created August 19, 2012 16:23 — forked from swannodette/gist:3213107
sudoku.clj
(defn distincto
"s is a sequence of sequence of vars.
ensure that vars in each particular sequence
take on distinct values."
[s]
(if (seq s)
(let [vars (first s)]
(all
(distinctfd vars)
(distincto (next s))))
@cuixin
cuixin / crc32.clj
Created August 29, 2012 02:18
crc32
(defn- crc32
"Generate a crc32 checksum for the given string"
[token]
(let [hash-bytes
(doto (java.util.zip.CRC32.)
(.reset)
(.update (.getBytes token)))]
(Long/toHexString (.getValue hash-bytes))))
@cuixin
cuixin / python_test.py
Created October 9, 2012 08:36
mypython examples
def mysum(*args):
return sum(args)
def mytimes(*args):
if not args:
return 0
else:
result = 1
for v in args:
result *= v
@cuixin
cuixin / CsvObjTools.java
Last active December 9, 2015 22:08
一个通过csv文件序列化到类实例的工具,因为这个跟json不太一样,是一行行的读取,中间通过tab分割,第一行实际就是类的字段定义。 通过指定一个索引文件来读取,索引文件负责设定类的名称和配置文件的路径。
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.slf4j.Logger;
@cuixin
cuixin / csv.lua
Created March 14, 2013 07:39
Using lua to parse CSV file to a table.
-- Using lua to parse CSV file to a table.
-- Notice: first line must be data description filed.
-- The separator is '|', change it if you want.
-- Usage: csv = require('csv')
-- tab = csv.load('test.csv', ',')
-- table.foreach(tab[1], print)
-- print(tab[1].you_field)
--encoding=utf-8
@cuixin
cuixin / words.lua
Created May 5, 2013 19:20
写了个用lua解析英文单词和统计的小玩意,方便了解每篇英文文章的单词统计数据。
#!/usr/local/bin/lua
require "io"
local args = {...}
if #args < 1 then
print("Please specify the file path.")
print("./words.lua as.txt")
return
end
@cuixin
cuixin / MaxMultiply.c
Created May 7, 2013 13:35
求解数组中连续一段子数组和的最大数列的乘积
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 问题:
// 求解数组中连续一段子数组和的最大数列的乘积。
//
// 解答:特殊情况,全是负数的时候需要处理
// 先应该判断是否数字大于0,大于0则进行乘积运算
// 然后将运算的结果跟上次运算的结果进行比较,
// 如果大于上次结果就保存,直到遍历结束.期间增加
@cuixin
cuixin / MaxSum.c
Created May 7, 2013 13:46
求解数组中连续一段子数组和的最大值。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// 问题:
// 求解数组中连续一段子数组和的最大值。
//
// 解答:特殊情况,全是负数的时候需要处理
// 先判定是否上次计算的和是否大于0
// 如果大于0则继续累计求和,否则就重
// 新取值,然后再跟已经记录过的最大和
@cuixin
cuixin / bsearch.c
Created May 8, 2013 01:47
Binary Search
#include <stdio.h>
int bsearch(int *a, int n, int v)
{
int l = 0, r = n - 1, m = 0;
while (l <= r) {
m = l + ((r - l) >> 1);
if (a[m] > v) {
r = m - 1;