Skip to content

Instantly share code, notes, and snippets.

@fangdingjun
fangdingjun / nqueen_recusive.go
Created October 14, 2015 03:01
N queens problem implement code in golang
package main
import (
"fmt"
"log"
"os"
"strconv"
)
const (
@fangdingjun
fangdingjun / pascal_triangle.go
Last active October 14, 2015 03:07
This code print a Pascal triangle
package main
import (
"fmt"
)
func main() {
var N = 12
for i := 0; i <= N; i++ {
for j := 0; j <= i; j++ {
@fangdingjun
fangdingjun / mouse_in_maze.go
Created October 14, 2015 03:09
This code implement the Mouse In a Maze problem
package main
import (
"fmt"
)
var maze [][]int = [][]int{
{2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 0, 0, 0, 2, 0, 0, 0, 2, 2},
{2, 0, 2, 0, 2, 0, 2, 2, 0, 2},
@fangdingjun
fangdingjun / dutch_nation_flag.go
Last active October 14, 2015 03:13
This code implement the Dutch Nation Flag problem, use three-way partition algorithms
package main
import (
"fmt"
)
var flags = []int{1,2,3,3,2,1,1,2,3}
func main() {
var i, j, n int
@fangdingjun
fangdingjun / tower_of_hanoi.go
Created October 14, 2015 03:19
Tower of Hanio problem code in golang
package main
import (
"fmt"
"os"
"strconv"
)
var count int
func hanoi(n int, a, b, c string){
if n == 1{
count++
@fangdingjun
fangdingjun / inet_pton.c
Created October 29, 2015 03:57
inet_pton, inet_ntop implements for windowns
#ifdef WIN32
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
int inet_pton4(const char *src, char *dst);
int inet_pton6(const char *src, char *dst);
int inet_pton(int af, const char *src, char *dst)
@fangdingjun
fangdingjun / limit_webbench.sh
Last active November 27, 2015 07:03
使用iptables防止webbench工具的攻击
#限制单个ip的tcp的并发连接数为30
iptables -A INPUT -p tcp -m connlimit --connlimit-above 30 -j DROP
#限制连接web服务的连接速度, 单个ip在10s内只允许建立10个新连接
iptables -A INPUT -p tcp -m multiport --dport 80,443 -m state --state NEW --set --name web
iptables -A INPUT -p tcp -m multiport --dport 80,443 -m state --state NEW --update --name web \
--hitcount 10 --seconds 10 -j REJECT
@fangdingjun
fangdingjun / t.py
Last active December 19, 2015 02:10
from twisted.internet import reactor, protocol
class EchoClient(protocol.Protocol):
def connectionMade(self):
reactor.callLater(0, self.send_data, "1111")
def send_data(self, data):
self.transport.write(data)
reactor.callLater(0, self.send_data, "1111")
@fangdingjun
fangdingjun / checknet.go
Created February 22, 2016 09:54
check network connection and re-dial when connection is down
package main
import (
//"net"
//"net/http"
"fmt"
"log"
//"os"
"flag"
"os/exec"
@fangdingjun
fangdingjun / unzip.py
Created May 5, 2016 01:34
extract a zip file with non-utf8 encoded filenames under linux
#!python
import zipfile
import os
def unzip(filename, output_dir=".", filename_encode="gbk"):
fp = zipfile.ZipFile(filename, "r")
# get name list