Skip to content

Instantly share code, notes, and snippets.

@onlytiancai
onlytiancai / RemoveMultipleSpaces.py
Created March 31, 2012 05:29
模拟微面试之去空格-python解法
#题目要求:https://gist.github.com/2227226/b133a8833acbdc37a48e5fc7d91ad56bb5b8729d
def RemoveMultipleSpaces(baseStr):
findSpace, count, space = False, 0, ' '
for i, ch in enumerate(baseStr):
if findSpace and ch == space: continue
findSpace = ch == space
baseStr[count] = ch
count = count + 1
@onlytiancai
onlytiancai / laozhaoq.cs
Created March 28, 2012 00:06
老赵微博的题目撒
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LaozhaoQ
{
// Please write an sequence list implements the interface with the required
// time complexity described in the comments. The users can add the same
// element as many times as they want, but it doesn't support the null item.
@onlytiancai
onlytiancai / good_or_bad.md
Created March 8, 2012 08:59
哪段儿代码更优雅,可读性更高,更Pythonic?

哪段儿代码更优雅,可读性更高,更Pythonic?

题目要求

找出一个锯齿数组里长度大于5的子数组

在符合要求的子数组里的数据里找出所有偶数

如果数据小于10的话乘以2,大于10的除以2

最后统计符合要求的数据的和

@onlytiancai
onlytiancai / get_http_status.py
Created February 23, 2012 10:13
求重构:获取一个服务器HTTP状态
def get_http_status(ip, port, host, path='/',ssl=False):
'''
根据指定IP,端口等信息返回该服务器的http状态,应答码,错误原因,响应时间
代码写的很难看,求重构
'''
newstatus, status_code, status_desc = config.Unknow, -99, '未知状态'
starttime = datetime.now()
try:
status_code = int(http((ip, port),host, path, ssl))
@onlytiancai
onlytiancai / dabblet.css
Created February 17, 2012 06:35
Untitled
.section {
background-color:#eee;
padding:10px;
margin:10px;
width:600px;
clear:both;
overflow:hidden;
}
.w100 {
width:100px;
@onlytiancai
onlytiancai / grep3.cs
Created February 7, 2012 09:40
linq版本grep
static void Main(string[] args)
{
if (args.Length != 2) return;
string searchPattern = args[0], dir = Path.GetDirectoryName(args[1]), filePattern = Path.GetFileName(args[1]);
var files = from file in Directory.EnumerateFiles(dir, filePattern, SearchOption.AllDirectories)
from line in File.ReadLines(file).Select((text, i) => new { Text = text, Index = i })
where new Regex(searchPattern).IsMatch(line.Text)
select new { File = file, LineText = line.Text, LineNo = line.Index };
Parallel.ForEach(files, (f) => { Console.WriteLine("{0}({1}){2}", f.File, f.LineNo, f.LineText); });
}
@onlytiancai
onlytiancai / grep2.cs
Created February 5, 2012 03:53
紧凑版的grep
/*
紧凑版的grep,相对于对象健身操的实现,这段代码只是原版本的三分之一,个人感觉可读性也不是太差。
原文地址:https://gist.github.com/1738383
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading.Tasks;
@onlytiancai
onlytiancai / grep.cs
Created February 4, 2012 15:04
对象健身操演练-grep
/*
## 对象健身操
http://www.infoq.com/cn/minibooks/thoughtworks-anthology
###规范
1. 方法只使用一级缩进。
1. 全部符合要求
1. 大部分通过方法提取做到,有的带yield的语句块不好提取,幸好一些Linq方法可以用来抽象一些集合操作。
1. 所有方法都尽量控制在5行以内,不包含空行和括号单独占的行
@onlytiancai
onlytiancai / fenbushi.txt
Created September 23, 2011 01:42
分布式系统设计
基本体系结构
这个系统设计有三个独立进程。输入进程,控制进程,和输出进程。输入和输出进程由控制进程产生,和控制。三者的基本功能为:
输入进程。采集数据,将数据存储,并通知控制进程有需要处理的新数据。
控制进程。控制输入与输出进程的生命周期。系统信息的接受,和发出控制指令,以及处理返回结果等。系统错误处理,例如终结无响应进程,对不可恢复错误进行报错等。
输出进程。在接到控制程序指令后传输,和返回传输结果给主程序。
这些进程间通信使用系统的IPC。当输入有输入是通知控制进程,控制进程转达给输出进程。完全分离输入和输出单位。输出的结果和错误处理由控制进程完成。
这样做的原因是:
一)分离功能和分离可控和不可控的模块。尤其是输出进程的通讯部分牵涉与远程计算单元的网络通讯,应当和主控制系统分离。
二)提高整体系统与环境交互的响应。控制进程完全使用非等待式的I/O处理。这个进程的运算以发送控制信令为主,要短暂,一个功能不可阻塞其它功能。
三)安全考虑。控制进程可能需要一些特权权限。而其它进程则可以在提权限用户下执行。控制进程只接受本系统内的输入于输出,降低受外部使用输入攻击的风险。
@onlytiancai
onlytiancai / splid_words.html
Created August 30, 2011 03:54 — forked from onlytiancai/splid_words.html
javascript实现的正向最大匹配机械分词
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>test</title>
</head>
<body>
<script type="text/javascript">
/*
* @description 原始词库
*/