Wednesday, March 24, 2010

Duplicate character removal

Today, I found there is a interesting post in Stack Overflow, duplicate character removal. This is mean, there is string like “sueijdesyebhsdewqw”, the ‘w’ is the duplicate character, so it would be removed. This is a easy question, the purpose of this post is to give a faster, shorter and more elegant solution for it.

In .NET, LINQ is a useful and powerful tool, sometimes you may ignore it, just use it to manipulate the XML,Database or List. But do not forget that the string is also a Enumerable class. You can leverage it to manipulate the string also.

C#

var str = "serrfguikntedbctqes";
var duplicate = str.Where(ch => str.Count(c => c == ch) > 1);
var result = new string(str.Except(duplicate).ToArray());

And you can use the Group to group the character in the string and pick out the suitable group from string.

C#

StringBuilder sb = new StringBuilder();
foreach (var s in str.ToArray().GroupBy(c => c))
{
   
if (s.Count() == 1)
        sb
.Append(s.Key);
}

If you are not familiar with the LINQ, the first idea comes out of your brain may be similar with the one below,

C#

var h1 = new HashSet<char>();
var h2 = new HashSet<char>();
foreach (var c in str)
{
   
if (!h1.Add(c))
   
{
        h2
.Add(c);
   
}
}

h1
.ExceptWith(h2);
result
= new string(h1.ToArray());

If you are fan of Javascript, you may like it

Javascript

s.split('').filter(function (o,i,a) a.filter(function(p) o===p).length <2 ).join('');

Don’t forget the Perl which is very good at the text process and regular express.

Perl

perl -pe's/$1//gwhile/(.).*\1/'

You may wonder where is the C language, be patient.

C language

#ifdef NICE_LAYOUT
#include <stdio.h>

/* global variables are initialized to 0 */
int char_count[256];                          /* k in the other layout */
int char_order[999999];                       /* o ... */
int char_index;                               /* p  */

int main(int ch_n_loop, char **dummy)         /* c  */
                                             
/* variable with 2 uses */
{

 
(void)dummy; /* make warning about unused variable go away */

 
while ((ch_n_loop = getchar()) >= 0) /* EOF is, by definition, negative */
 
{
   
++char_count[ ( char_order[char_index++] = ch_n_loop ) ];
   
/* assignment, and increment, inside the array index */
 
}
 
/* reuse ch_n_loop */
 
for (ch_n_loop = 0; ch_n_loop < char_index; ch_n_loop++) {
   
(char_count[char_order[ch_n_loop]] - 1) ? 0 : putchar(char_order[ch_n_loop]);
 
}
 
return 0;
}
#else
k
[256],o[999999],p;main(c){while((c=getchar())>=0)++k[o[p++]=c];for(c=0;c<p;c++)k[o[c]]-1?0:putchar(o[c]);}
#endif

But the most amazing solution for it is using the LabVIEW 7.1

LabView

Actually, there are lots of interesting solutions for this problem, but some of them are too tricky to use in practice. Anyway, you still get some tips and fun from these solution.
Reference 
link1,link2


Customize SharePoint Web Part Toolbar

 

Sometimes, you may not be content with the default toolbar provided OOTB, which only contains a link title. Actually, you add customize toolbar for your own web part. Reza gave two possible ways to implement it.

  • Load the default Toolbar controls from /_controltemplate, and add it to your web part. For this control, there is a property named Template_Buttons, you create your own ITemplate to replace it. Please referhere for demo code and detail.
  • Use the RepeatedControl which is used by SharePoint to achive a consistent look and feel to create similar HTML for various toolbars across all the OOTB web parts and pages in SharePoint. Please referhere  for detail.

Tuesday, March 23, 2010

呦,你也谈人性

在这次谷歌离开中国的事件中,很多人民群众是这样语重心长的教导我们,“不要神化和美化西方世界,人性都是一样丑陋的。”
《Friends》里有这么一集,Rose交了一个很漂亮的女朋友,但是那个女生的家里乱的就像垃圾场,还有老鼠跑来跑去,Rose忍受不了,就邀请那个女生到他家去,结果那个女生嫌Rose家有股怪味道。其实相对于境外的一些网站上的那些关于党国的耸人听闻的报道,对于大多数人来说,所谓的权利被侵犯,并不是那么明显,切身体会不是那么的深重。大家照样早起上班,晚上回家吃饭,如果要说有什么地方被侵犯,无非就是收的税高点,看病贵点,房价高点,发帖被追捕,聊天被监控,偶尔得点肾结石,周末吃点地沟油,再时不时的看看影帝仰天长望背背古诗词。
中国人当奴才当了几千年,身上的奴性是大于人性的。上个世纪初,稍微的抬了抬头,接着又趴下了,直到今天还没有爬起来。现在没有什么明目张胆的奴才了,开始当人,又开始拿人性说事了。而且一说说到外国去了。背着手,晃着脑袋,说什么“西方的月亮不一定比中国的月亮圆”。你还别说,如今西方的月亮就是比中国的月亮圆。黑暗的社会其实并不可怕,如今的社会还未达到那种人人自危,道路侧目的地步。网络的发展,让这个社会的人们有了前所未有的手段去了解这个未知的世界,去汲取无尽的思想与知识。这个社会一定会改变,只是时间的长短而已。生活在这种社会中不悲哀,悲哀的是生活在这种社会中还自愿为奴。没有正常人在神化和美化西方世界,我们只是在与一些正常的社会做比较,就被视为神化西方世界。那么我们的社会是什么?地狱吗?如果地狱有18层,我们在几层?人性确实是一样丑陋的,所以西方世界相信的是制度,相信的是宪法,只有健全的体制,才不会人亡政息,而不是影帝的眼泪,和那些所谓的青天大老爷。
谷歌事件,对于中国大陆民众,绝不仅仅是一个商业事件,更重要的是,我们的党国已经在使劲浑身解数在进一步控制着民众的公共空间,国进民退进一步加剧。政府的触角伸入到民众的各个生活空间,无论是现实的还是虚拟的。民众的权利越来越得不到保障,而付出却在不断的加剧。在通往朝鲜的道路上,我们都在添砖加瓦。如果那一天再次出现,希望那些傻逼们还能够背着手,晃着脑袋说“西方的月亮不一定比中国的月亮圆”。

悼词

谷歌中国在喧嚣一段时间后,终于退出了中国大陆,栖身于香港。对于那些憋着劲看谷歌笑话的人来说,这不啻为一个好消息。国内的主流媒体与网站,一如既往的体现着光明伟大正确的调子。新闻联播腔调式的新闻已经不是什么亮点,亮点总是出现在评论之中。从那些洋洋洒洒的评论来看,谷歌确实不得人心,陷入的人民群众的汪洋大海之中了。一个个怒目圆睁,歇斯底里的喊着,谷歌滚出去。有的质疑谷歌的圆滑,香港不也是中国的领土,不是说退出中国,怎么还呆在香港,这个死不要脸的东西。有的继续着谷歌又想当婊子,又想赚钱的论调。还有的情绪稳定,只是表示很恶心。还有的继续将此事件上升到党,国家,民族,人类,宇宙的高度。在此之际,送上林语堂的一句话,“中国就有这么一群奇怪的人,本身是最底阶层, 利益每天都在被损害,却具有统治阶级的意识。在动物世界里找这么弱智的东西都几乎不可能。”中国不是有这么一群奇怪的人,而是有很大一群天生骨骼迥异,异与常人的傻逼。

Thursday, March 18, 2010

Building a simple compiler series(5)

LR

LR is a buttom-up parsing algorithm,the L indicate the input is processed from left, and the R indicates that a rightmost derivation is processed from left to right. A buttom-up parser has  two possible actions: the first one is Shift( a terminal from input string to the top of the stack ), the other is Reduce( a string a at the top of the stack to a nonterminal A, given the BNF choice A->a ). So a buttom-up parser is thus sometimes called shift-reduce parser.


LR(0) item

A LR(0) item of a context-free grammar is a production choice with a distinguished position in its right side. In most of books, it will indicate this distinguished position by a period. For eample A->XYZ would generate four produces. A->.XYZ A->X.YZ A->XY.Z and A->XYZ. .  A->.XYZ means we expect a string that can be derivate from XYZ. A->X.YZ means we already get a string that can be derivate from X, and now we expect a string that can be derivate from YZ.


LR(0) item closure

If I is a item collection for grammar G, then CLOSURE(I) follow these two rules to construct item collection from I:

  • At first, add all items in I to CLOSURE(I)
  • If A->a.Bb in CLOSURE(I), B->c is a production and B->.c is not in the CLOSURE(I), then add it. Repeat it until no one can be added.

GOTO(I,X), I is a item collection and X is a grammar symbol. GOTO(I,X) indicate all the items of [A->aX.b]’s closure corresponding to  all items whose format is [A->a.Xb] in the I .


LR Parsing Alogrithm

Input:A input string and a LR parsing table which describes grammar G , Action function and GOTO function.

Output: If w in the L(G), then it gives the derivation, otherwise it gives a error.



Wednesday, March 17, 2010

卢安克

卢安克,是幸运的,因为他没有出生在中国;卢安克,又是不幸的,因为他来到了中国;卢安克,还是不幸的,因为他想加入中国国籍;卢安克,还是幸运的,因为有关当局没有批准。
38岁德国人卢安克,22岁时来华旅游,爱上中国,于是到中国留学。从1999年起,他只身一人深入中国山村从事义务教育。2001年开始,更是一头沉入广西最穷最偏僻的东兰县坡拉乡广拉村,一个不通电话、不通公路、只会讲壮族土语的山村,在那里他教导村童讲普通话、学文化。 他和村民相处愉快,帮村民犁田、割禾、打谷,为村民设计脱粒机,并将德国家人汇给他的钱用来为当地修路,终于使山村有了一条宽0.6米、长不足300米的水泥小路。他还教村民改造居住环境,人畜居所分开。 
卢安克的行为初时一直不获理解。他曾因未办“就业证”而在内地义务教德文,被公安罚款3000元人民币;他借住的村屋房东偷走他的钱;他教孩子的方式被官方斥为“不入流”;甚至有人指他是“负有特别使命的间谍”。不过,他都不为所动,终赢得民众和舆论尊敬。 
不久前,协助卢安克进入中国的德国“华德福教育基金”广西办事处因到期未获当局延续而关门,他为能继续留在中国,于是向中国驻德大使馆申请加入中国籍。不过,按中国法例,只有在国家一级机构工作4年以上,或为中国作出特殊贡献的外国人,才合乎入籍条件,官方表示“爱莫能助”。今年8月,签证到期的他只好暂返德国。

去年12月,柴静的《面对面》对卢安克进行了一次采访,他在中国极少接受公开采访,这是极少数的采访之一。他不愿意接受采访,不愿意人们资助他,也不愿意人们感谢他。他不接受当地政府给他的补助,只用远在德国父母给他寄的生活费和他翻译书的稿费生活,并用这些并不多的钱资助当地的村民和孩子。当柴静问他一个月的生活费是多少的时候,他说出了一个在现在中国很多地区的人们都难以相信的一个数字。
我们都知道有白求恩这样的一个人物,在今天的中国,同样的一个人,在中国生活了近10年的时间,却没有得到世人的关注。盛世中国下的光辉,是不允许有阴影存在的。偏僻贫穷落后愚昧的山区,是被中国政府刻意遗忘的地区,一个德国人却在这里杂了根,奉献了他的10年光阴,得到的是当地民众的尊敬和孩子们的喜爱,换来的是有关部门对他的驱逐和罚款。卓琳仅仅就因为是邓小平的夫人,就荒唐的得到了所谓的“感动中国”的奖项,毛新宇这种智商50以下的奇才,就因为毛血旺而即将得到少将军衔,而卢安克得到的是官方的“爱莫能助”。
柴静的采访中有这么一个片段,一个学生在卢安克的影响下,知道了保护环境的重要性,劝阻那些乱砍树木的人,结果得到了他人的嘲笑。也许,这就是卢安克十年光阴换来的成果之一。多年来,在他的陪伴下,这里的孩子,不再那么好斗,不再那么的“野蛮”,开始讲道理,愿意倾听,这是对中国教育的莫大讽刺,更是对“大脚印”下的政府的莫大讽刺。
当今年两会记者招待会上,温家宝慢条斯理的说出“亦余心之所向兮,虽九死其尤未悔”的时候,当三五千代表拿到所谓为了省纸的笔记本电脑的时候,这些上位者们是否想到过,中国有越多的卢安克,卢安克们的人性越光辉的时候,就是中国社会越黑暗扭曲的时候。正义与公平的光辉比太阳还要耀眼,可惜中国的太阳不是正午时的苍白,也不是初升时的金黄,也不是下山时的红日。天涯上有篇帖子,大意是,别那么快赞美卢安克,先搞清楚他是不是恋童癖。我只能说,当一个社会扭曲畸形的这个地步的时候,什么事情都可能发生。或者用一个更为专业的解释,“这是在中国”,This is China。盛世下的中国是欢乐的,欢乐到陈冠中需要盗版自己的《盛世中国 2013》,来免费提供给内地的读者。五月的魔都上海,又即将群魔乱舞。当冯正虎先生蜗居在日本成田机场的时候,表示想回国参加世博会,上海政府后来表示,冯正虎是否参加世博会,要看其表现。好一个当婊子的上海政府,你比日本的A片演员还不如,连点专业精神都没有。
我们应该这样对卢安克说,中国太危险,你还是回国去吧。非洲还有很多小朋友等着你教,缅甸泰国也有很多小朋友等着你,就算是德国,也有小朋友啊,何必呆在危机四伏的中国呢?你在中国是没有出头之日的,哪怕你一颗红心向太阳,我们也还是不会要你的。因为有你的存在,就暴露了我们的无耻与下流,无能与无赖。
千年的乌龟,万年的鳖,天安门前的山呼万岁犹然在耳。还是小兵张嘎说的好,“别看你今天闹的欢,小心明天拉清单”。